Skip to content

Instantly share code, notes, and snippets.

@hite
Forked from tkafka/listAllEventListeners.js
Created June 15, 2019 17:15
Show Gist options
  • Save hite/2ef4b4a332880f63bafcf0572cb245f9 to your computer and use it in GitHub Desktop.
Save hite/2ef4b4a332880f63bafcf0572cb245f9 to your computer and use it in GitHub Desktop.
List all event listeners in a document
console.table((function listAllEventListeners() {
const allElements = Array.prototype.slice.call(document.querySelectorAll('*'));
allElements.push(document); // we also want document events
const types = [];
for (let ev in window) {
if (/^on/.test(ev)) types[types.length] = ev;
}
let elements = [];
for (let i = 0; i < allElements.length; i++) {
const currentElement = allElements[i];
for (let j = 0; j < types.length; j++) {
if (typeof currentElement[types[j]] === 'function') {
elements.push({
"node": currentElement,
"type": types[j],
"func": currentElement[types[j]].toString(),
});
}
}
}
return elements.sort(function(a,b) {
return a.type.localeCompare(b.type);
});
})());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment