|
|
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,
|
|
|
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:
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 |
|
|
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. |
|
|
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 |
|
|
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. |
|
|
You could XHR the script source and eval it. pretty ugly though. |
|
|
You would have to put the script online though as GM_xhR won't access file URLs. |
|
|
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 |
|
|
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. |
|
|
The 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 |
|
|
Henrik - I may be wrong, but I believe cookies may also be used. -Joel |
|
|
Joel H: Probably true. |