I was recently working with events and wanted to view/control all events in a page. Having looked at possible solutions, I've decided to go my own way and create a custom system to monitor events. So, I did three things. First, I needed a container for all the event listeners in the page: that's the Next, I created an 0, 1, 2, 3, 4, 5, and added a method remove() to remove the listener.Lastly, I extended the native 7 and 8 methods to make them work with the objects I've created (EventListener and EventListeners ).Usage:
7 creates an EventListener object, adds it to EventListeners and returns the EventListener object, so it can be removed later. 5 can be used to view the listeners in the page. It accepts an 6 or a string (event type).
Demo Let's say we want to know every event listener in this current page. We can do that (assuming you're using a script manager extension, Tampermonkey in this case). Following script does this:
And when we list all the listeners, it says there are 299 event listeners. There "seems" to be some duplicates, but I don't know if they're really duplicates. Not every event type is duplicated, so all those "duplicates" might be an individual listener. Code can be found at my repository. I didn't want to post it here because it's rather long. Update: This doesn't seem to work with jQuery. When I examine the EventListener, I see that the callback is
I believe this belongs to jQuery, and is not the actual callback. jQuery stores the actual callback in the properties of the EventTarget:
To remove an event listener, the actual callback needs to be passed to the 8 method. So in order to make this work with jQuery, it needs further modification. I might fix that in the future. |