Created
April 2, 2019 14:31
-
-
Save pritam-patil/d07c5c19f7d6bb5a296bee07015153de to your computer and use it in GitHub Desktop.
Lists all event listeners on a page
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
const listeners = (function listAllEventListeners() { | |
let elements = []; | |
const allElements = document.querySelectorAll('*'); | |
const types = []; | |
for (let ev in window) { | |
if (/^on/.test(ev)) types[types.length] = ev; | |
} | |
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, | |
"listeners": [ { | |
"type": types[j], | |
"func": currentElement[types[j]].toString(), | |
}] | |
}); | |
} | |
} | |
} | |
return elements.filter(element => element.listeners.length) | |
})(); | |
console.table(listeners); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment