[Greasemonkey] Adding methods/properies to document object with GM
ldrhcp at gmail.com
Wed Jul 20 16:15:30 EDT 2005
This specific case (calling alert) is not a security concern, but the
best practice is to keep objects in the user script scope.
define in the user script. A more elegant way to get the same
a function call is where the function is defined, not where it is
called from. When you set innerHTML, you are having your "onclick=..."
code parsed as HTML, and onclick is defined only then. If you define
onclick in the user script, you will have access to all user script
objects. Your example code can be rewritten to this:
var foo = 'bar';
var div = document.createElement('div');
var link = document.createElement('a');
link.onclick = func; // that fancy closure thing
var content = document.getElementById('content');
A few more notes:
* Don't put parentheses in the addEventListener function reference.
You want to pass the function, not the results of the function. You
can usually avoid waiting for the window load event, anyway, since
Greasemonkey executes when the DOM loads, but before onload.
would be treated as a link, not a page anchor.
More information about the Greasemonkey