Skip to content

Instantly share code, notes, and snippets.

@timwburch
Forked from danburzo/README.md
Created April 19, 2017 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timwburch/5b548bd137c5af39d8d63b2b2d51a7e0 to your computer and use it in GitHub Desktop.
Save timwburch/5b548bd137c5af39d8d63b2b2d51a7e0 to your computer and use it in GitHub Desktop.
Get all event listeners on the page in Google Chrome
var items = Array.prototype.slice.call(
document.querySelectorAll('*')
).map(function(element) {
var listeners = getEventListeners(element);
return {
element: element,
listeners: Object.keys(listeners).map(function(k) {
return { event: k, listeners: listeners[k] };
})
};
}).filter(function(item) {
return item.listeners.length;
});
// See below for things you can do with the items
// Things you can do with the items
// 1. log them to the console
console.log(items);
// 2. Put a red border around the elements
items.forEach(function(item) {
item.element.style.outline = '1px solid red';
})
// 3. generate a summary
var summary = .map(function(item) {
var el = item.element,
id = el.id,
className = el.className;
if (className instanceof SVGAnimatedString) {
className = className.baseVal;
}
var str = el.tagName.toLowerCase() + (id ? '#' + id : '') + (className ? '.' + className.replace(/\s+/g, '.') : '');
str += ' ' + item.listeners.map(function(l) {
return l.event + ': ' + l.listeners.length;
}).join(' ');
return str;
}).join('\n');
console.log(summary);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment