[Greasemonkey] Script scoping

Nikolas Coukouma lists at atrus.org
Tue May 3 13:15:53 EDT 2005


Scott R. Turner wrote:

> On Tue, 3 May 2005, Jeremy Dunck wrote:
>
>>> I'm facing many of these issues in building the GM scripting extension.
>>> One approach is to hook any script using another script to onload; I 
>>> think
>>> that guarantees the other script will be fully loaded.
>>
>>
>> Yeah it does, but only works to one level of dependency.
>
>
> I'm not sure what you mean here.  By the time onload occurs, all other
> scripts ought to be loaded and their functions available.  In
> particular, if you want to get functions from a "library" script, then
> they ought to be available.

I can imagine libraries needing to do some setup (e.g. global variables, 
ow) based on each other. The simple solution is to have the onload (last 
script) call an init function, which could then call depending libraries 
init functions before it runs it's own.

You might consider using objects and prototype magic.

> I think more of a problem is ensuring that your library script gets
> loaded into all the pages that use any script that uses the library
> script.  How do you manage that?
>
>          -- Scott 

The best you can do right now is tell the user "hey, this is a library. 
it needs to load for all pages..." and hope they listen. You could also 
do a check and alert the user that the library is missing. They could 
then fix it and reload the page.

Again, preprocessing the script before publishing it is probably best 
(similar to #include). Actually, couldn't you just use m4 directly? For 
example:

test2.js:
define( HI, "hi" )

test1.js:
include( test2.js )
alert( HI )
alert( HI + " world" )

heck, the m4 stuff even looks like Javascript, so it should feel pretty 
natural.

-Nikolas 'Atrus' Coukouma


More information about the Greasemonkey mailing list