Userscript as library [SOLVED]
To clarify, are both userscripts installed in Tampermonkey and are they both running on the same page?
Yes, both userscripts installed in Tampermonkey and are they both running on the same domain.
Assuming you can edit the script you want to use as a resource, you could add an html
Is the "userscript1" adding to the page the library functions in the "\< script \>" tag? It probably will work, thanks.
I made an example:
// ==UserScript==
// @name Userscript1
// @namespace http://tampermonkey.net/
// @version 0.1
// @description none
// @author Tom Burris
// @match http*://greasyfork.org/en/forum/discussion/comment/34360*
// @grant none
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// The important thing in the meta-block data is the @run-at option, which make Userscript1 run before Userscript2.
// See https://wiki.greasespot.net/Metadata_Block#.40run-at for more info.
// declare resource(s).
var myResource = function X() {
alert("Userscript2 called this function from Userscript1! :)");
};
var scriptTag = document.createElement("script");
scriptTag.innerHTML = myResource.toString(); // .toString() turns the source code of the function to a string.
(document.head || document.getElementsByTagName("head")[0]).appendChild(scriptTag); // append it to the head.
})();
and:
// ==UserScript==
// @name Userscript2
// @namespace http://tampermonkey.net/
// @version 0.1
// @description none
// @author Tom Burris
// @match http*://greasyfork.org/en/forum/discussion/comment/34360*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// The default behavior of @run-at is document-end.
X(); // function X is created, then appended to the head in Userscript1.
})();
EDIT:
The above code works, but if you want to share variables, it would be easier to just add them to the window object. Example:
window.X = function() {
alert("hello world!");
};
Then in your second userscript just access them via the window object.
window.X();
Must-reads for mixing scripts from different sources: https://wiki.greasespot.net/Sandbox
As mentioned in the linked pages, some functions exported by GreaseMonkey are very useful: cloneInto, createObjectIn, exportFunction, dump
EDIT:
The above code works, but if you want to share variables, it would be easier to just add them to the window object. Example:
window.X = function() {
alert("hello world!");
};
Then in your second userscript just access them via the window object.
window.X();
This is a great idea, thank you very much!
I did it this:
var head = document.getElementsByTagName('head')[0];
var lib= document.createElement( 'script' );
lib.type= 'text/javascript';
lib.src= 'https://rawgit.com/path/to/lib/Library.js';
head.appendChild( lib );
but your idea with the window object I like more.
Userscript as library [SOLVED]
Is it possible to use the "userscript1" as a library for another "userscript2" in Tampermonkey? Without using the "@resource file://", both scripts will be installed from GitHub.
For example:
@name userscript1
...
function X(){
...
}
@name userscript2
...
var x = userscript1 -> function X()
SOLUTION from Tom Burris2:
/*The above code works, but if you want to share variables, it would be easier to just add them to the window object.
Example:*/
window.X = function() {
alert("hello world!");
};
//Then in your second userscript just access them via the window object.
window.X();