i'm trying to remove Listeners. In console a code below is working. When i use code in Chrome extension i get: "Uncaught ReferenceError: getEventListeners is not defined". Why does this happen? Is there an equivalent to the function "getEventListeners"?
document.addEventListener('click', fireContentLoadedEvent, false);
function fireContentLoadedEvent () {
console.log ("DOMContentLoaded");
for (let i = 0; i < document.getElementsByClassName("someClass").length; i++) {
plusButton = document.getElementsByClassName("someClass")[i]
if ( getEventListeners(plusButton)["click"].length > 1) {
plusButton.removeEventListener("click", getEventListeners(plusButton).click[1].listener);
}
}
}
getEventListeners is part of the Console API and it's not available outside the browser console.
As mentioned in a comment, you can replicate it by spying on the addEventListener method before it is ever called, for example: https://stackoverflow.com/a/6434924/288906
In a browser extension you would also make sure to run this code in the main world (not the isolated one)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With