Skip to content

Instantly share code, notes, and snippets.

@linhnobi
Created July 31, 2024 02:13
Show Gist options
  • Save linhnobi/e0cc932acfb60a5618ec252f065a15d1 to your computer and use it in GitHub Desktop.
Save linhnobi/e0cc932acfb60a5618ec252f065a15d1 to your computer and use it in GitHub Desktop.
View all event listeners used in the page, getEventListeners(object) returns the event listeners registered on the specified object
// Get all the binding click events number in page
// Return number
Array.from(document.querySelectorAll('*'))
.reduce(function(pre, dom){
var clks = getEventListeners(dom).click;
pre += clks ? clks.length || 0 : 0;
return pre
}, 0)
// If you want see what events have been bound in all your elements in your page and
// how many of the listeners of each of the events
Array.from(document.querySelectorAll('*'))
.reduce(function(pre, dom){
var evtObj = getEventListeners(dom)
Object.keys(evtObj).forEach(function (evt) {
if (typeof pre[evt] === 'undefined') {
pre[evt] = 0
}
pre[evt] += evtObj[evt].length
})
return pre
}, {})
// The result is like this:
{
touchstart: 6,
error: 2,
click: 3249,
longpress: 2997,
tap: 2997,
touchmove: 4,
touchend: 4,
touchcancel: 4,
load: 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment