[Greasemonkey] Running before a pre-defined event handler

Johan Sundström oyasumi at gmail.com
Thu Dec 22 01:32:51 EST 2005


I figured as it just took me well over an hour to diagnose and come up
with a work-around for this issue, others in the same situation might
be glad if I shared it on the list.

I faced a problem where I wanted to add an onclick handler that would
run prior to an already defined onclick handler in a page, as my handler
was changing the lots of page state intended for the original handler to
see. node.addEventListener( event, handler, false ); would install my
own callback _after_ the already existing handler, which wouldn't do
much good here.

The kludge I came up with was adjusting the present node to wait for a
decisecond before executing, so my greasemonkey injected hook would
have ample time to do its business before it would run the original code:

var code = node.getAttribute( 'onclick' );
if( code )
  node.setAttribute( 'onclick', 'setTimeout("' +code+ '", 100)' );

Really ugly, but it does work. I'd love to hear of better solutions for
this.

--
/ Johan Sundström, http://ecmanaut.blogspot.com/


More information about the Greasemonkey mailing list