I'm creating a Grasemonkey user script that is getting really big.
Can I split it into several smaller files? If so, how?
Tampermonkey is used to run so-called userscripts (sometimes also called Greasemonkey scripts) on websites. Userscripts are small computer programs that change the layout of a page, add or remove new functionality and content, or automate actions.
GreaseMonkey is a Firefox extension that lets you run arbitrary Javascript code against selected web pages. What this means is that GreaseMonkey lets you change how other people's web pages look and how they function (but just in your own browser, of course).
In your Firefox window, once you've downloaded and installed Greasemonkey, there'll be a little monkey face in the right-hand corner of the status bar. Right-click on that and you'll get a menu that includes the option "New User Script". Click that and you'll get a dialog looking a bit like the box on the right.
Tampermonkey has it's own built-in editor. Just hit the Tampermonkey button and select Dashboard. To get a new script, hit the little + tab in the upper right. You'll get a nice template with an IIFE (Immediately Invoked Function Expression) that you should put all your code in to avoid global namespace pollution.
Yes, and in Greasemonkey, it's rather easy. If you want to split your scripts into i18n.js
, a utils.js
and your main script body (and had them in that order in the original script), just change your script header 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, join them up in the order listed by your @require
statements (main script last), and execute it as usual. Put them in the same directory on the server you distribute them from, or be sure to give full URLs in the @require
statements to where they reside on the net.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With