Discussions » Développement

Userscript as library [SOLVED]

§
Posté le: 04/05/2017
Édité le: 11/05/2017

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();

§
Posté le: 05/05/2017

To clarify, are both userscripts installed in Tampermonkey and are they both running on the same page?

§
Posté le: 05/05/2017

Yes, both userscripts installed in Tampermonkey and are they both running on the same domain.

§
Posté le: 05/05/2017

Assuming you can edit the script you want to use as a resource, you could add an html

§
Posté le: 05/05/2017
Édité le: 05/05/2017

Is the "userscript1" adding to the page the library functions in the "\< script \>" tag? It probably will work, thanks.

§
Posté le: 05/05/2017
Édité le: 07/05/2017

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();

Deleted user 20822
§
Posté le: 06/05/2017

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

§
Posté le: 11/05/2017

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.

Poster une réponse

Connectez-vous pour poster une réponse.