remove event listener

Post Reply
quease
Posts: 2
Joined: 03.02.18 19:25:52

remove event listener

Post by quease » 03.02.18 19:30:33

Hi. This code works in the console:

Code: Select all

document.removeEventListener('keydown', getEventListeners(document).keydown[0].listener);
But getEventListeners is a chrome dev console thing, not a DOM thing so it doesn't work in tampermonkey. Is there a way to achieve the same thing some other way? This page has hijacked apple-R (which would normally reload the page).

Thanks.

User avatar
spiralx
Posts: 31
Joined: 11.06.15 16:26:46
Location: London, UK

Re: remove event listener

Post by spiralx » 20.02.18 17:25:50

I don't think that there is in general, but if the events were added using jQuery, then the document might have something like:

Code: Select all

document.jQuery3110402912439332621234.events.keydown
where you can find the event and remove it - I've not tried that though. I just discovered document.releaseEvents() and window.releaseEvents() which are super-deprecated but could work:

https://developer.mozilla.org/en-US/doc ... easeEvents

And finally could you use event capturing to intercept the event on the window object? Something like:

Code: Select all

window.addEventListener('keydown', event => {
  event.preventDefault()
  event.stopPropagation()
}, true)
The capturing phase of an event goes outermost -> innermost, and my assumption is that window > document > document.documentElement > document.body > etc., so capturing event handlers on window would come before those on document. Worth a try!

quease
Posts: 2
Joined: 03.02.18 19:25:52

Re: remove event listener

Post by quease » 20.02.18 21:06:21

I tried your window.addEventListener which stops them from hijacking those keys, but then the reload didn't happen either. I took out the preventDefault line and now I think it is exactly what I want.

Thanks!!!

User avatar
spiralx
Posts: 31
Joined: 11.06.15 16:26:46
Location: London, UK

Re: remove event listener

Post by spiralx » 21.02.18 00:34:31

Glad I could help! Pages hijacking keyboard shortcuts is exactly the sort of thing that annoys me enough to try and fix :)

Post Reply