Skip to content

Instantly share code, notes, and snippets.

@tjbenton
Created August 12, 2015 20:32
Show Gist options
  • Save tjbenton/77a87ee631a37e38c144 to your computer and use it in GitHub Desktop.
Save tjbenton/77a87ee631a37e38c144 to your computer and use it in GitHub Desktop.
Utility to list out elements that have event listeners. Their listeners will be listed out in the console.
/// @name has_events
/// @author Tyler Benton
/// @description
/// Utility to list out elements that have event listeners. Their listeners will be listed out in the console.
/// @arg {string} target ["*"]
/// @note {10} - Only works if you copy this code and paste it in the chrome console.
/// @markup {js}
/// has_events(); // lists out all elements
/// has_events("a"); // lists out all anchor tags that have event listeners
function has_events(target){
target = target !== undefined ? target : "*";
var all_the_things = Array.prototype.slice.call(document.querySelectorAll(target));
for(var i = 0, l = all_the_things.length; i < l; i++){
var thing = all_the_things[i],
_events = getEventListeners(thing);
if(Object.keys(_events).length){
console.groupCollapsed(!!thing.id ? "#" + thing.id : !!thing.className ? "." + thing.className.split(" ").join(".") : thing.tagName.toLowerCase());
console.log("%o", thing);
console.log("%cEvents list: %c%s", "font-weight: 800;", "font-weight: 400;", Object.keys(_events).join(", "));
console.log("%cEvents: %O", "font-weight: 800;", _events);
console.groupEnd();
}
}
};
// this is to define out `getEventListeners` in the window since it's
// only available in the chrome console by default. Why? I have no idea
// why this isn't avaiable anywhere else.
window.getEventListeners = getEventListeners;
console.clear(); // removes the pasted function from the console
function has_events(e){e=void 0!==e?e:"*";for(var o=Array.prototype.slice.call(document.querySelectorAll(e)),t=0,s=o.length;s>t;t++){var n=o[t],l=getEventListeners(n);Object.keys(l).length&&(console.groupCollapsed(n.id?"#"+n.id:n.className?"."+n.className.split(" ").join("."):n.tagName.toLowerCase()),console.log("%o",n),console.log("%cEvents list: %c%s","font-weight: 800;","font-weight: 400;",Object.keys(l).join(", ")),console.log("%cEvents: %O","font-weight: 800;",l),console.groupEnd())}}window.getEventListeners=getEventListeners,console.clear();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment