-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ability to remove events #1
Comments
No, it's not possible using getEventListeners....at least automatically. However, because In this example, the 'paste' event listener assigned to the element // select the element
let el = document.querySelector( '#myElement' );
// retrieve the first "paste" event listener
let event = el.getEventListeners( 'paste' )[0];
// remove the listener
el.removeEventListener( event.type , event.listener, event.useCapture ); I hope it helps |
Thanks! My second example was not working because I made a mistake by using Now it is fine like this also: Your example works too but you have to remove the "#" for the |
Both This is my own |
I like this tool a lot, but it bothers me on a personal level that the JS Web API doesn't do listener management for you. Everyone who programs with JS should be exposed to this approach, even if they already have an awesome tool that owns. Thanks colxi for the great tool. An alternative solution to listener management in JS is to extend the EventTarget class from the JS Web API, overriding the addEventListener and removeEventListener methods to maintain a data structure containing the listener function references. I follow this approach in the Gist below, where I also have a few unit tests. You can run the tests in the browser JS console by copying the code from the Gist to your clipboard, pasting it into the browser JS console, and pressing enter. Please read strange JS from the internet before running it in your JS browser console. https://gist.github.com/angstyloop/504414aba95b61b98be0db580cb2a3b0 |
@angstyloop What is the difference / advantage with your script? @colxi For information, I was able to use successfully your script with Greasemonkey to bypass a paste event restriction on a website, very useful. Basically I used the Content Script Injection method from Greasemonkey (https://wiki.greasespot.net/Content_Script_Injection) to inject the getEventListeners function on page load by creating a Greasemonkey script with the code window.eval( |
Hello,
Is there a way to remove events with the script getEventListeners.js? This would be useful as a Greasemonkey script that loads before the website content.
Some websites prevent copy-pasting or add other validation restrictions. So I reproduced a paste restriction by adding an event on a text input with Firefox:
document.getElementById("test").addEventListener("paste", function(e) {e.preventDefault()})
Then I tried to bypass this restriction by removing the event, based on this code that uses the default getEventListeners function of Chrome / Chromium (https://stackoverflow.com/questions/4386300/javascript-dom-how-to-remove-all-events-of-a-dom-object/40448083#40448083):
document.getElementById("test").getEventListeners().paste.forEach((e)=>{e.remove()})
But I get the error
TypeError: e.remove is not a function
.I also tried a solution based on another code (https://stackoverflow.com/questions/4386300/javascript-dom-how-to-remove-all-events-of-a-dom-object/41877059#41877059):
document.removeEventListener("paste", document.getElementById("test").getEventListeners().paste[0].listener)
But it is still not possible to paste in the text input.
Thanks.
The text was updated successfully, but these errors were encountered: