Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Uncaught ReferenceError: getEventListeners is not defined"

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);

        }
    }
}
like image 523
TimeTq Avatar asked Jan 18 '26 05:01

TimeTq


1 Answers

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)

like image 91
fregante Avatar answered Jan 20 '26 17:01

fregante