[Greasemonkey] Using functions in JavaScript Includes

Richard Wallis Richard.Wallis at talis.com
Thu Aug 18 17:55:20 EDT 2005


OK, I thought it would be something like that just to clarify it (in my
old brain) the "Library code" would be in the xx.user.js file?

Is it a variation on the same problem that causes the not defined error
when I try to call fun1() from my xx.user.js script?

> -----Original Message-----
> From: greasemonkey-bounces at mozdev.org 
> [mailto:greasemonkey-bounces at mozdev.org] On Behalf Of Jeremy Dunck
> Sent: 18 August 2005 16:41
> To: greasemonkey at mozdev.org
> Subject: Re: [Greasemonkey] Using functions in JavaScript Includes
> 
> On 8/18/05, Richard Wallis <Richard.Wallis at talis.com> wrote:
> > Hi,
> > 
> > I want to be able to reuse a set of functions in several 
> Greasemonkey 
> > scripts.  To keep things simple I want to create a utils.js script 
> > file to keep them in and then load that file in each xx.user.js.
> > 
> > For test purposes here is the contents of utils.js:
> > 
> > //GM_log("Hi");
> > function fun1(){
> >         //GM_log("F1");
> > 
> >         alert("<H1>Hello world</H1>"); }
> > 
> > I would expect that document.write("<script 
> > type='application/x-javascript'
> > src='http://myserver.com/scripts/utils.js'></script>"); 
> would load the 
> > file for me.  I know it does because if I uncomment the 
> GM_log() I get 
> > a not defined error.
> > 
> > If I try to call fun1(); from my .user.js script I get a 
> not defined 
> > error.
> > 
> > Any ideas if what I want to do is possible, and if it is what is my 
> > problem.
> 
> When you append a script elm, you're in the content context, 
> which is intentionally separate from the GM context (for 
> security reasons). 
> The GM API is not available in the page context.  You're encouraged to
> -not- add the GM API functions to the page context, either.
> 
> Consider writing your library functions to use callbacks, like so:
> 
> GM code:
> ====
> //inject library script elm here...
> //...later...
> DoStuffWithGM('waka', GM_log);
> 
> 
> Library code:
> =====
> function DoStuffWithGM(param1, gmapifunc) {
>   gmapifunc('foo!');
> }
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
> 
>
 
Talis Insight - Register now to receive your early bird discount
http://www.talis.com/insight2005/

Any views or personal opinions expressed within this email may not be those of Talis Information Ltd. The content of this email message and any files that may be attached are confidential, and for the usage of the intended recipient only. If you are not the intended recipient, then please return this message to the sender and delete it. Any use of this e-mail by an unauthorised recipient is prohibited.


More information about the Greasemonkey mailing list