[Greasemonkey] Greasemonkey class libraries?

Vectorspace vectorspace at ntlworld.com
Fri Feb 10 15:05:21 EST 2006


I don't know if its possible, but I know that @include is not the way to 
do it - the websites that @include are simply the websites that the 
script is allowed to run on - Even if two scripts run on the same page, 
they can't access each other.


Chris Beams wrote:
> I'm building a large, multi-site GM mash-up application and would like 
> to reuse many of the classes I'm writing across various pages.  My 
> hope was to be able to create a user script (or scripts) serving as a 
> class library.  These 'class library' user scripts would then be 
> @included on all the pages that need the classes defined within them.  
> For example:
>
>> // ==UserScript==
>> // @name          My Mash-up's Common Class Library
>> // @namespace     http://mycompany.com/userscripts
>> // @description   Shared class library for my company's mashup
>> // @include       http://firstdomain.com*
>> // @include       http://*.firstdomain.com*
>> // @include       http://seconddomain.com*
>> // @include       http://*.seconddomain.com*
>> // ==/UserScript==
>>
>> function Customer(name) {
>>     this.name = name;
>>     this.sayName = function() {
>>         alert("Hello.  My name is " + this.name);
>>     }
>> }
>>
>> function Account() {
>>     // ...
>> }
>>
>> // and so on...
>
> Unfortunately, I cannot access these classes (functions) from my other 
> user scripts.  I'm certain that all the scripts have been loaded, but 
> it appears that the functions defined in each are in different 
> scopes.  For example, the following does not work:
>
>> // ==UserScript==
>> // @name          Customer Detail Page
>> // @namespace     http://mycompany.com/userscripts
>> // @description   Tweaks the display of customer details
>> // @include       http://customer.firstdomain.com/detail/*
>> // ==/UserScript==
>>
>> alert(new Customer('bob').sayName());
>
> This results in the following error message being printed to the console:
>
>> Error: Customer is not defined.
>
> Any ideas as to how I can get visibility to my Customer class without 
> having to copy and paste it everywhere?  Or perhaps there's a 
> completely different technique that might work better?  I've 
> considered grabbing the class library javascript via XMLHttpRequest 
> and eval()'ing the contents; this seems terribly hackish, though.  I'm 
> wide open to any suggestions.
>
> Thanks!
>
> - Chris
>
> Chris Beams
>
>
>
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>


More information about the Greasemonkey mailing list