Last active
April 25, 2022 03:27
-
-
Save Sarverott/5122880892ed86edd8f3d54de7245370 to your computer and use it in GitHub Desktop.
to search events and listeners in dom by js
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 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