Skip to content

Instantly share code, notes, and snippets.

@tkafka
Forked from dmnsgn/listAllEventListeners.js
Last active January 24, 2024 22:52
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save tkafka/1c5174ed5b446de7dfa4c04a3b09c95f to your computer and use it in GitHub Desktop.
Save tkafka/1c5174ed5b446de7dfa4c04a3b09c95f 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);
});
})());
@alex2844
Copy link

@thepeoplesbourgeois
Copy link

I went ahead and made my own implementation. Mutationless, for-free, probably a little slower: https://gist.github.com/thepeoplesbourgeois/66c2eeecc5b264efd41471ca5e354b1a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment