For purposes of recording Selenium scripts, I need to be able to intercept all events on a page, such as keyup or click. Currently, I do this by attaching an event listener to all elements on the page, but if there is already a listener present that stops the event from bubbling up, it never reaches me.
I tried the "replacing addEventListener" approach from https://stackoverflow.com/a/6434924/15255 in How to find event listeners on a DOM node when debugging or from the JavaScript code? , but this does not seem to work.
This is all in a Firefox addon, so I can do privileged things, but there doesn't seem to be much functionality for enumerating or modifying existing listeners. Does anyone have a suggestion on how to do this?
Edit: The code as it stands:
jQuery(frame.document).
bind("dblclick", {}, this.listeners.writeJsonClicks, true).
bind("keyup", {}, this.listeners.writeJsonChange, true).
bind("change", {}, this.listeners.writeJsonChange, true);
To clarify: I don't have control over the contents of the document in frame.document. It's whatever page the user is recording a script on. The issue is that by the time the document has loaded, its own capturing have already been installed, so mine won't receive the event.
I've been trying to catch the page at a point in its loading when the page DOM is fully loaded but the JS that attaches the listeners hasn't been executed yet. Using NsIObserverService to listen for document-element-inserted
looked promising, but that event happens as soon as there is any DOM at all: the document and its head are loaded, but the body is not.
Fundamentally, I need either a way to re-order existing listeners to insert mine before ones that have come earlier, a way of being able to run code when the DOM tree is loaded but the JS hasn't been run yet, or a way of intercepting calls to addEventListener to wrap the added listeners, letting me eavesdrop.