[Greasemonkey] Long literal strings of html

Edward Lee edilee at gmail.com
Wed Apr 20 21:27:33 EDT 2005


On 4/20/05, Julien Couvreur <julien.couvreur at gmail.com> wrote:
> One question about this piece of code ("injectScript" function).
> How come we're setting these GM_* on the windows object?
> I noticed the function above it ("greaseLoad") actually deletes these
> references. But I don't understand why we need to set them in the
> first place.

The scripts that get injected act like scripts that are originally on
the page. They wouldn't be able to access the functions defined in
extensions. Doing something like "var iS = injectScript" to pass on
the injectScript function to the user script wouldn't make sense
because there would be no injectScript function when that script body
is evaluated.

Putting the functions in window provides a middleground for the
extension to pass stuff to scripts on the page. It seems like each
time injectScript is called, it defines a new GM_setValue, etc for
that particular script and then runBrowserScript appends the script,
and it evaluates and binds the GM_ functions before they get
overwritten by the next injectScript.

The reason why the GM_functions are removed is to prevent the original
document owner from making use of them after the Greasemonkey user
scripts have already run.


var scriptContents = getContents(getScriptChrome(script.filename));
win.GM_getSelf = function() {
  return scriptContents;
}
win.GM_getSelf2 = function() {
  return getContents(getScriptChrome(script.filename));
}

Both of those work fine, but the second one allows it to get updated
changes to the file. The first one keeps the original script contents
while the second one goes back into the extension's functions and
evaluates everything. Should it be allowed to get the new contents of
the file or just keep things efficient by just saving it to a variable
that is used anyway later for runBrowserScript.

-- 
Ed


More information about the Greasemonkey mailing list