My goal is to dynamically trace all registrations of event handlers in JavaScript, including but not limited to the document and window object. A piece of code is supposed to run before the rest of the website executes (onLoad) and should report what functions register for events. I want to wrap addEventListener
, onmouseclick
, onmessage
etc. but I don't want to do this for each object individually.
I was looking into modifying the behavior of Object.constructor
to perform self-inspection in the constructor. The code should be pretty trivial and look roughly like this:
var toWrap = [ "addEventListener", "onclick", "..." ];
for ( var x in toWrap ) {
if ( toWrap[x] in this ) {
this[toWrap[x]] = wrap(toWrap[x], this[toWrap[x]]);
}
}
... but what is the right place to plug the code?
A pure JavaScript solution would be preferred over jQuery etc., I am implementing in PhantomJS.
Update:
I came up with the following code after reading this. It seems to work fine - any objections?
Object = ( function ( ) {
this.wrap = function(e) {
// ....
}
this.wrap();
});