Greasemonkey tip: running your handler before the page handler

I figured as it just took me well over an hour to diagnose and come up with a work-around for this issue (for adding BlogThis! support to my Blogger publish helper, which is done now, by the way -- feel free to reinstall it), others in the same situation might be glad if I shared this knowledge where search engines roam.

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.
blog comments powered by Disqus