Is it possible to split Greasemonkey user scripts into multiple files?

I am creating a user Grasemonkey script that is getting really big.

Is it possible to split it into several small files? If so, how?

+8
split greasemonkey multiple-files
source share
2 answers

Yes, and in Greasemonkey it's pretty easy. If you want to split your scripts into i18n.js , utils.js and your main script tag (and had them in that order in the original script), just change the script title to read something like this

i18n.js

 var hello = 'bonjour!'; 

utils.js

 function say(msg) { alert(msg); } 

my.user.js

 // ==UserScript== // @name My nifty script // @namespace Your unique author identifier // @require i18n.js // @require utils.js // ==/UserScript== say(hello); 

... and Greasemonkey will download and install all three files, attach to them in the order specified in your @require (the main script is the last), and execute it as usual. Put them in the same directory on the server where you distribute them, or remember to include the full URLs in the @require statements where they are located on the network.

+13
source share

Of course you can. For example, if you use Greasemonkey as a complement to Mozilla, then in config.xml you can use <Require> :

 <UserScriptConfig> <Script filename="babelfish.yahoo.com.js" name="Babel Fish" namespace="html" basedir="."> <Include>http://babelfish.yahoo.com/*</Include> <Require filename="document.js"/> <Require filename="cookie.js"/> <Resource name="babelfishCSS" filename="babelfish.yahoo.com.css" mimetype=""/> </Script> ... 

Both document.js and cookie.js must be in the same folder as babelfish.yahoo.com.js .

If your engine uses annotations in a file, use the @require directive:

 // ==UserScript== // @description This script automatically recovers the language selection. // @include http://babelfish.yahoo.com/* // @require cookie.js // @require document.js // @resource babelfishCSS babelfish.yahoo.com.css // ==/UserScript== 
0
source share

All Articles