Third-Party Libraries

From GreaseSpot Wiki
Jump to: navigation, search

With the @require metadata imperative, one can include entire extra files into a user script. This can also be used for including entire third-party libraries like jQuery or YUI.

Most general purpose libraries are not written to operate within the Greasemonkey sandbox and thus may not work properly, so tread carefully.


For a simple example, here is a way to load and use jQuery in your user scripts. Note that @require works by downloading the files once, at install time, and is thus fast and efficient.

// ==UserScript==
// @name          jQuery Example
// @require
// ==/UserScript==

// Append some text to the element with id someText using the jQuery library.
$("#someText").append(" more text.");


Some versions of jQuery may not run inside the Greasemonkey sandbox. But, as of this writing, all released versions, from 1.3.2 onward, work with no known issues except the potential conflict discussed below. (The current jQuery version is 1.9.1.)

Potential conflict

In some cases, @required jQuery can conflict with the web page. In that case, you can use jQuery in noConflict mode at the top of your script:

this.$ = this.jQuery = jQuery.noConflict(true);

It's also possible to force execution in the legacy sandbox, thus isolating your script further from the content page. Specify specify any @grant value other than none to do this. See @grant Scope for more information.

// ==UserScript==
// @grant  unsafeWindow
// ==/UserScript==

Links and References

jQuery UI

All jQuery UI framework releases, from 1.5.2 onward, work with no know issues -- except that the CSS files have to be modified to take full advantage of some images.

jQuery Tools

Version 1.2.4 of jQuery Tools is reported to be at least minimally compatible.


Sometimes using the @resource imperative alongside @require can be helpful.

YUI has a nice tool to bundle your required libraries on the fly. After you receive your script source (Loading Script and CSS Directly box) use the @require key for the script and @resource for the CSS (if any).

For example:

// ==UserScript==
// @name           YUI Example
// @require
// @resource       yCSS
// ==/UserScript==

// add Yahoo! css to head
var yCSS = GM_getResourceText("yCSS");