[Greasemonkey] Adding a script to the document.head?
chris olive
perljunkie at gmail.com
Thu Sep 8 14:17:02 EDT 2005
Just got turned on to GM, and... I'm hooked.
I want to add some HTML to the beginning of a document. That HTML
would contain an object with an "onClick" event handler. I'm learning
about the scope of GM scripts, and it seems that the GM script is
outside the scope of objects in the DOM. For instance (using a VERY
simplistic example), this does NOT work as planned:
// my.user.js
// Meta-data snipped
function dude() { alert( 'Dude!' ) }
var strSomeHTML = ''
+ '<form>'
+ '<input type="button" name="btnDude" value="Say Dude" onClick="dude();">'
+ '</form>';
var someHTML = document.createElement( 'div' );
document.body.insertBefore( someHTML, document.body.firstChild );
// End my.user.js
When the button is clicked, dude() in the GM script is not evoked.
Ok, fine, it's out of scope. But even if I set strSomeHTML to this:
var strSomeHTML = ''
+ '<script language="javascript">'
+ 'function dude() { alert( "Dude!" ); }'
+ '</script>'
+ '<form>'
+ '<input type="button" name="btnDude" value="Say Dude" onClick="dude();">'
+ '</form>';
It still doesn't work. Obviously inserting a script object into the
document must be done a different way. Can anyone point me to an
example of inserting a script object that DOM/Form element might use
for its onClick event?
-pj
More information about the Greasemonkey
mailing list