2005-12-21

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.

1 comment:

  1. I just removed the onclick attribute. It only sets the draft field value to 1 on draft posts and submits the form. I added the draft field handling to the click listener function.

    ReplyDelete

Limited HTML (such as <b>, <i>, <a>) is supported. (All comments are moderated by me amd rel=nofollow gets added to links -- to deter and weed out monetized spam.)

I would prefer not to have to do this as much as you do. Comments straying too far off the post topic often lost due to attention dilution.

Note: Only a member of this blog may post a comment.