Created
August 4, 2017 17:10
-
-
Save stevekinney/a1f9c9157267e649ca9cc22efd690a00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Records every DOM event and then set ups a listener that shows | |
// the full CSS selector path of the target that triggered the event. | |
const getPathForElement = el => { | |
const names = []; | |
while (el.parentNode) { | |
if (el.id) { | |
names.unshift(`#${el.id}`); | |
break; | |
} else { | |
if (el === el.ownerDocument.documentElement) { | |
names.unshift(el.tagName); | |
} | |
else { | |
for ( | |
let count = 1, e = el; | |
e.previousElementSibling; | |
e = e.previousElementSibling, count++ | |
) | |
names.unshift(`${el.tagName.toLowerCase()}:nth-child(${count})`); | |
} | |
el = el.parentNode; | |
} | |
} | |
return names.join(" > "); | |
} | |
const getAllEventNames = () => ( | |
Object | |
.getOwnPropertyNames(document) | |
.concat( | |
Object.getOwnPropertyNames( | |
Object.getPrototypeOf(Object.getPrototypeOf(document)) | |
) | |
) | |
.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(window))) | |
.filter( | |
i => !i.indexOf("on") && | |
(document[i] == null || typeof document[i] === "function") | |
) | |
.filter((elem, pos, self) => self.indexOf(elem) === pos) | |
); | |
const getAllEventListenerNames = () => getAllEventNames().map(eventName => eventName.slice(2)); | |
const events = getAllEventListenerNames(); | |
const logEvent = eventName => e => { | |
if (e.target) console.log(eventName, getPathForElement(e.target)); | |
}; | |
for (const event of events) { | |
document.body.addEventListener(event, logEvent(event)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment