Version 3.0

23 March 2010 4:13 PM

7 Comments

I taught a class this past fall where we used Greasemonkey as a tool for building prototypes of new browser functionality. We also used jQuery extensively to make JavaScript in the browser simpler. There are some tricks to getting the two to work together that I’ve been meaning to share.

Loading jQuery in a Greasemonkey script

Simply loading jQuery within Greasemonkey hasn’t always been straightforward. I think they should just bundle jQuery with the Greasemonkey package since just about everyone is using it already, but that’s a separate issue.

The old way to include jQuery in your Greasemonkey scripts was to add a <script> tag to every page you visit linking to the jQuery library. Although this works, it’s a clunky solution. It introduces timing issues: since you can’t be sure when the script would load, you have to poll continually, checking to see if jQuery has downloaded yet. Once you detect that jQuery is available, you can run your script. This approach also might clobber other JavaScript running on the main page (like the Prototype library) before you have a chance to call jQuery.noConflict.

Fortunately, later versions of Greasemonkey made this hack unnecessary by adding the @require attribute to scripts. When you install a Greasemonkey script that has an @require statement, the listed resource is downloaded once and included in the script as if it were pasted directly into the document. Unfortunately, there’s a small stumbling block for people testing their scripts: the @require statement is only executed when you install a script. This means that if you create a new Greasemonkey script in Firefox, add the @require line, and save your file nothing will happen. You have to uninstall your script and reinstall it because @require is only processed at installation. Once you do this, you can use jQuery all you want in your script. Except for…

jQuery AJAX and Greasemonkey

By default, jQuery’s helpful AJAX methods like $.get and $.post don’t work in Greasemonkey. jQuery acts as a wrapper around browser’s varying implementations of XMLHttpRequest (XHR), which is what makes AJAX possible. In a Greasemonkey script you don’t have XMLHttpRequest. Instead you get GM_xmlhttpRequest. You can use jQuery’s .ajaxSetup() to specify a different object for requests, but GM\_XMLHttpRequest actually operates differently than a normal XHR.

When you create a new XMLHttpRequest you get an object that you can use to set options like the URL, HTTP verb, callback functions, and so forth. Once everything is set up, you start the XHR with the .send() method. With GM_XMLHttpRequest, you set all these options when you create the object because it sends immediately when you create it. In order to use GM_XMLHttpRequest with jQuery, we need a way to encapsulate its functionality. This script does that:

Once we have this wrapper object, we need to tell jQuery to use it instead of the standard browser XHR:

$.ajaxSetup({
xhr: function(){return new GM_XHR;}
});

If you include this code in your Greasemonkey script (or @require it—see above), you will be able to use $.ajax, $.get, and $.post in your scripts. I’m not sure about $.getScript, and I know that $.getJSON will often fail.

Cross-site scripting with Greasemonkey and jQuery

The short story: using $.getJSON in Greasemonkey will fail anytime you request a resource on another site. To avoid this, just use $.get and parse the returned JSON yourself (using eval() or—better yet—JSON.parse() (see also json2.js).

Update 2010-04-11: jQuery 1.4 added the $.parseJSON method which is what you should use to handle JSON responses (via Paul Tarjan).

For cross-domain JSON requests jQuery uses <script> tags and JSONP as a workaround for the same origin policy. JSONP works because the responding server wraps its response with the name of a user-defined callback function. Instead of returning:

{"param": "value"}

the server will respond with

callback_function_name({"param": "value"})

where callback_function_name is something jQuery creates automatically. When you use Greasemonkey, jQuery defines this function in the scope of the Greasemonkey window, but the browser looks for the function in the main window’s scope and you get an error. The solution is simply to avoid $.getJSON and use $.get instead. Since GM_xmlhttprequest doesn’t have to abide by the same origin policy you don’t need JSONP.

I hope these tips are helpful for people familiar with jQuery who want to try their hand at making a Greasemonkey script.

Thanks a lot for the post. To make it working with JQuery 1.5.1, I had to implement getAllResponseHeaders. Furthermore, I removed GM_xmlhttpRequest.onreadystatechange which is not required.
I put the working version on https://gist.github.com/999065.

Entry Navigation

About

Ryan does stuff and writes about it here. He studied philosophy at Notre Dame and worked afterwards with Holy Cross Associates in Chile. Some of his interests are technology, design, and photography. He was a student at UC Berkeley’s School of Information and currently works as an engineer at Slack.

Colophon

This site's valid markup was created using TextMate and the invaluable CSSEdit. Pages get served up after MovableType fills in the blanks. Various PHP scripts hold the ship together.