1

I have noticed my AdBlocker removes click event listeners in JavaScript. I would like to remove click event listeners on the page and am not sure how to do so.

I read that generally I cannot remove event listeners I don't have a reference to from JavaScript but I am wondering if there is some way to remove event listeners given this is my browser since the AdBlocker manages to do it somehow.

How can I remove click event listeners from the JavaScript console programmatically?

7
  • You would need to remove the click listener - which is only possible with a debugger connected and not through userland JavaScript. Basically: this is (by design) not possible without elevated permissions. (Extensions can use the debugger and getEventListeners) Commented Jan 22, 2022 at 17:40
  • 1
    Probably because you did not read How to Ask? Your question does not contain an isolated reproduction of the issue, contains a bunch of links to a third-party website and does things (like thanks and using !!!s) that you are asked not to do in the asking questions guide. Additionally you are asking how to use a website in a way that prevents the people maintaining it from getting ad revenue. I didn't downvote it by the way because the actual technical question "can I read event listeners from a user script?" is interesting. Commented Jan 23, 2022 at 11:26
  • Also, it is valuable to remember that downvotes are a criticism of a question and not of you. Take them as an opportunity to learn how to interact with the community - you are not being attacked here (Even though I remember when I was starting it certainly felt that way!) there is a reason every upvote is significantly more points than a downvote. I've been using the site for many years now and I still get downvoted all the time it's important to put in context :) Commented Jan 23, 2022 at 11:28
  • @BenjaminGruenbaum I do understand your pov but I want to list some things myself. Firstly the adblock can be easily used to disable ads on the page so I'm just trying to reproduce that using code programmatically. Secondly, the site itself is in the gray zone when it comes to "streaming" and is not entirely legal. Third I don't understand what part of "Reproducing the issue" is unclear here? You simply need to navigate to the player in order to understand the working of the ads. Commented Jan 23, 2022 at 12:10
  • 1
    I think you're slightly misunderstanding the situation. You are trying to rationalize to me why you think the 4 people who downvoted your question were wrong. That won't help - I'm not those people - I'm just a person trying to help you out so you won't get a negative experience in the future because I've answered thousands of questions. You don't have to take my feedback and apply it - just don't be surprised if people will keep downvoting your questions. (The site being in a gray zone is actually much worse since it makes StackOverflow potentially liable) Commented Jan 23, 2022 at 12:23

1 Answer 1

2

Your AdBlocker cheats by connecting to the browser's debugger which has elevated permissions page JavaScript does not have.

This is because (by design) there is no way to get existing page event listeners and remove them.

Using Event capture

You can intercept events before the page (most likely) by adding an event in "capture mode" and preventing the event from propagating to the page - but the page simply has better hooks than you since the devtools only let you run code after the page runs:

document.documentElement.addEventListener('click', (e) => {
  e.preventDefault(); // stop the click
  // still fire the event on the video player so you can click it
  document.querySelector('video').dispatchEvent(e);
}, { useCapture: true });

This is because the page can simply attach the event (in capture) before you and since there is no way to "prepend" a listener they can "win" this battle.

What you can do

Luckily, the devtools also has superpowers! It can call into debugger methods and provides utilities that normal page JavaScript does not have:

// This works in Chrome, Firefox, Safari
const listeners = getEventListeners(document);
// Also get the listeners for document.body and so on until you find it
for(const { listener, useCapture} of listeners.click) {
  // remove the listener
  document.removeEventListener(listener, { useCapture });
}

Which is a better hook than the page has and should always work.

Extensions do this by directly "talking debugger" and using the chrome.debugger interface to call DOMDebugger.getEventListeners

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.