[Greasemonkey] Script scoping

Jeremy Dunck jdunck at gmail.com
Mon May 2 13:29:15 EDT 2005


On 5/2/05, Andrew Hayward <greasemonkey at mooncalf.me.uk> wrote:
> I've just got round to installing GM0.3b, after finally getting fed up
> of not having the new features at my disposal. However, I noticed the
> following in the change log...
> 
> > Improved error handling by isolating scripts from one another. Errors
> > in one script will no longer stop other scripts from running. Errors
> > loading a script will not stop other scripts from being loaded.
> 
> That's all well and good, but what happens if I actually want to access
> another script?

Hey Andrew, cross-script dependencies, eh.  There'd been some talk
about this, but no support for it yet.

Others have already suggested the window.foo = yourfirstsscriptfunc() approach.

The only downside of this is that your first script has to be
injected, err, first.  Which is the same as it was before, modulo the
scoping issue.

That is to say, if you had cross script dependencies before, even
without the anon function scoping added in 0.3b, the dependant script
would have to inject after the lib script.

So..  that suggestion may already solve your problem.

Otherwise, I like the idea of @scriptinclude
yournamespace/yourlibname, which, the way I'm thinking, would inject
another (already installed) script, regardless of its
@include/@exclude.

Would that work for you?  ...Injection would get more complicated,
because the order of injection would have to be smarter to resolve
dependencies.

Hmm, also, GM_getValue and GM_setValue on the library would be
something else to discuss.  Should values stored as a library
injection be under the top script name, or the lib script name?

...Sure you can't preprocess just your user.js to just include all the
stuff you want into the deployed user.js file?

We can work this out, but for now, I recommend you hack up your
greasemonkey install.
browser.xul line 124:
              runBrowserScript(
                doc,
                [ "(function(){",
                  "var GM_xmlhttpRequest = window.GM_xmlhttpRequest;",
                  "var GM_registerMenuCommand = window.GM_registerMenuCommand;",
                  "var GM_setValue = window.GM_setValue;",
                  "var GM_getValue = window.GM_getValue;",
                  "var GM_log = window.GM_log;\n",
                  getContents(getScriptChrome(script.filename)),
                  "})();"
                  ].join("\n")
              );
to:
              runBrowserScript(
                doc,
                [ 
                  "var GM_xmlhttpRequest = window.GM_xmlhttpRequest;",
                  "var GM_registerMenuCommand = window.GM_registerMenuCommand;",
                  "var GM_setValue = window.GM_setValue;",
                  "var GM_getValue = window.GM_getValue;",
                  "var GM_log = window.GM_log;\n",
                  getContents(getScriptChrome(script.filename))
                  ].join("\n")
              );


More information about the Greasemonkey mailing list