Skip to content

Instantly share code, notes, and snippets.

@Sarverott
Last active April 25, 2022 03:27
Show Gist options
  • Save Sarverott/5122880892ed86edd8f3d54de7245370 to your computer and use it in GitHub Desktop.
Save Sarverott/5122880892ed86edd8f3d54de7245370 to your computer and use it in GitHub Desktop.
to search events and listeners in dom by js
function eventSearch(searchQuery="*", searchEvent=false){
var out=Array.from(
document.querySelectorAll(searchQuery)
);
out=out.map(function(element){
const events=getEventListeners(element);
if(Object.keys(events).length!==0&&(!searchEvent||Object.keys(events).includes(searchEvent))){
var itemQuery=element.nodeName;
if(
typeof element.id=="string"
&&
element.id.trim()!=""
)itemQuery+="#"+element.id;
if(
typeof element.classList!="undefined"
&&
`${element.classList}`!=""
)itemQuery+=`.${element.classList.value.trim().split(" ").join('.')}`;
return {
query:itemQuery,
events
};
}else return false
});
out=out.filter(function(e){return e});
return out;
}
console.log(eventSearch());
console.log(eventSearch("a"));
console.log(eventSearch("button"));
console.log(eventSearch("div"));
console.log(eventSearch("*", "click"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment