Created
November 26, 2024 17:37
-
-
Save codearachnid/97e8977b1ab20bae87bda3e7df2c250d to your computer and use it in GitHub Desktop.
Javascript - Listing active event listeners on a Web page. Great for use in Chrome console
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
function listAllEventListeners() { | |
const allElements = Array.prototype.slice.call(document.querySelectorAll('*')); | |
allElements.push(document); | |
allElements.push(window); | |
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); | |
}); | |
} | |
// to list in table: | |
console.table(listAllEventListeners()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment