Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Can I split it into several smaller files? If so, how?

like image 845
jotaelesalinas Avatar asked Jan 01 '12 20:01

jotaelesalinas


People also ask

Is Greasemonkey the same as Tampermonkey?

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.

How does Greasemonkey work?

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).

How do I make a Greasemonkey script?

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.

How do I write a script in Tampermonkey?

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.


1 Answers

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.

like image 93
ecmanaut Avatar answered Oct 05 '22 08:10

ecmanaut