Use Chrome's webkit inspector to remove an event listener
Asked Answered
S

5

18

I know you can see the event listeners in the Chrome Inspector but i'm doing some debugging work and there are so many event listeners lying around I'd like to disable some without editing the code

enter image description here

Is there a way to disable an event listener quickly from the Webkit inspector?

Perhaps have a look and type some code into the console to removeEventListener the listener? How would I do this? For instance how would i remove the 'click' listener above

Shealy answered 13/6, 2012 at 11:29 Comment(0)
S
4

Sorry, you are out of luck (at least for the time being.) removeEventListener requires the exact listener Function object as an argument, and DevTools do not let you get a grip of the listener function in any way.

If you definitely need this feature, please file a bug at http://new.crbug.com (against Chrome) or http://bugs.webkit.org (against WebKit, the preferred way).

Supernaturalism answered 15/6, 2012 at 12:24 Comment(1)
@Jackson's answer below works in chrome as of 2017. https://mcmap.net/q/585679/-use-chrome-39-s-webkit-inspector-to-remove-an-event-listenerElwin
V
18

You can use getEventListeners(element).click[index].listener to get a reference to a listener (in a WebKit console).

So, to remove the first listener, you could do:

document.removeEventListener('click', getEventListeners(document).click[0].listener)

Similarly, to remove all listeners, you could use this function:

function removeEventListeners(element, listenerMap) {
    Object.keys(listenerMap).forEach(function (name) {
        var listeners = listenerMap[name];
        listeners.forEach(function (object) {
            element.removeEventListener(name, object.listener);
        });
    });
}

removeEventListeners(document, getEventListeners(document))
Vyse answered 24/8, 2015 at 21:24 Comment(1)
This is a great answer. Note, you might have to call it with window instead of document.Elwin
S
4

Sorry, you are out of luck (at least for the time being.) removeEventListener requires the exact listener Function object as an argument, and DevTools do not let you get a grip of the listener function in any way.

If you definitely need this feature, please file a bug at http://new.crbug.com (against Chrome) or http://bugs.webkit.org (against WebKit, the preferred way).

Supernaturalism answered 15/6, 2012 at 12:24 Comment(1)
@Jackson's answer below works in chrome as of 2017. https://mcmap.net/q/585679/-use-chrome-39-s-webkit-inspector-to-remove-an-event-listenerElwin
A
3

You can remove an event listener in the javascript console. First find the element to which this event listener is attached. Let's call it e. Then you execute: e.onclick=null. For example, many event listeners are attached to "body", then the above code becomes: document.body.onclick=null. After that the event listener is removed.

Arabeila answered 6/6, 2014 at 9:14 Comment(2)
Just tried what you suggested and it didn't work when creating an event listener with: document.body.addEventListener('click', function() { console.log('clicked'); });Inversion
yeah you are right. My tests didn't cover all cases. Seems the correct answer is in here: #4919579Arabeila
S
3

Maybe late but.

In chrome there is a specific button to remove the method call of a specific event.

Right click on the html input (boutton in my example) inspect element, chrome opens the element viewer tab (in html mode) with another panel composed of subtabs in this panel click on the "event listener" tab and unfold the event you're intersted in (onclick in my example) put the mouse point over the name of the button you inspected and a button "remove" will apear and one clicked the event listener is deleted for the current page loaded.

Et voilà

Stroganoff answered 20/3, 2019 at 8:33 Comment(1)
but what if this line with the name of the class is too long? it seems that this Remove word is way too far to see it. is there any hint on how to make this Remove word visible even fro super-long names of elements?Fusiform
W
1

Simply:

getEventListeners(document).click[0].remove()

This works with other elements, too:

getEventListeners($('#submit-button')[0]).click[0].remove()

And other types of events:

getEventListeners($('#login-form')[0]).submit[0].remove()
Withstand answered 31/10, 2015 at 5:30 Comment(1)
Remove is not a method for the listener object.Put

© 2022 - 2024 — McMap. All rights reserved.