Including external utility functions

in Script development
Subscribe to Including external utility functions 12 posts, 5 voices



László Monda Scriptwright

Hey guys,

I'd like to include some utility functions to use in my other userscripts. I tried to create a userscript that holds my functions, but I cannot access the functions I defined in it from other userscripts. I also tried to include my script remotely using document.write("

How can I include my utility functions in a way that actually works?

Thanks in advance,
Laci

 
Joel H Scriptwright

There's one possibility that I can think of, but I don't know enough about how GM itself works with the scripts, and I'd appreciate it if someone could answer these questions:
IF
1. you can control the order of execution of scripts, and
2. scripts are executed such that they share the same namespace
THEN
it should be possible to have a library script execute first (controlling order of execution) and then any functions listed there should be available to any later scripts (via shared namespace).

That said, it's probably a very bad idea to assume that any given user has a given script installed, so you should probably just have a comment block and then your library functions all in the same script, so that you can be sure you have what you need, but it is kept out of the way.

-Joel

 
alien_scum Scriptwright

From what I can tell scripts execute in the oreder they were installed, or with the new Greasemonkey the order they are in the list, for security reasons each script gets it's own namespace to ensure there is no leakage that could break other scripts. There has been a lot of discussion about this and the consensus has been just to copy in the necessary functions, as Joel H suggested.

 
László Monda Scriptwright

Ok, I understand that it's not a recommended practice since it can break things, but I don't want to deploy this particular script across a broad range of hosts, only privately, so I'm interested in whatever way I can achieve inclusion to work.

Regarding namespaces, I created two scripts with the same namespace. I defined a function in the first which I wanted to use in the second script, but according to Firefox it was undefined. I correctly set the order of these scripts so the first script ran earlier than the second one. What are namespaces good for exactly?

I'm also curious what the @include directive covers. Is it good for including external JavaScript files?

- Laci

 
alien_scum Scriptwright

Sorry about the confusion, the @namespace in the script header refers to where to wave GM_setValues, whereas i was refering to the javascript namespace (what variables you can access form where). The @include limits what sites the script can run on, you can think of it as checking against the address bar in FF when you open a page.

The easiest solution i can think of is to use

<SCRIPT LANGUAGE="JavaScript" SRC="file://c:/mylib.js"></SCRIPT>

and then dump the whole script into the page in a <script> tag.
It would be very dirty and ugly but if you really have to use a library it would work.
I'm hoping someone else can come up with something better though.

 
Arvid Scriptwright

You could XHR the script source and eval it. pretty ugly though.

 
alien_scum Scriptwright

You would have to put the script online though as GM_xhR won't access file URLs.

 
László Monda Scriptwright

alien_scum: If you meant injecting the script tag you referenced by using document.write(), that's exactly what I mentioned at my forst post. Unfortunately my code got escaped by this silly forum engine, but I meant that. That ran into infinite loop for some strange reason for me whenever I opened new tabs / windows or visited URLs.

Arvid: XHR'ing the external JavaScript file could work, but it's such a hack that I won't even try it. Good idea though.

I think I'll just embed my utility functions to all my userscripts that need them. It's a shame that Greasemonkey doesn't support such an essential feature.

- Laci

 
Arvid Scriptwright

Just to clear this up: greasemonkey devs and volunteers have been working a lot on adding this feature, but there's a lot of disagreement on how it should work. Some day it'll probably be included.

 
Henrik N Admin

The @namespace is for script identity: a script is uniquely identified by its name+namespace as far as GM_get/setValue and installing is concerned. If you install a script with the same name+namespace as an already installed one, the old one will be replaced. The new script will have access to the GM_values of the old script.

It has nothing to do with code scope. The only ways two separate userscripts can communicate is, to my knowledge, through GM_values, the DOM (i.e. scripts will see DOM changes made by other scripts) or the unsafeWindow.

 
Joel H Scriptwright

Henrik -

I may be wrong, but I believe cookies may also be used.

-Joel

 
Henrik N Admin

Joel H: Probably true.

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel