Loading External JavaScript Libraries in Greasemonkey

I’m working on a new Greasemonkey script and with the recent discovery of cross-domain access to JSON data, I figured the same technique would be a good way to bring in JavaScript libraries that can be useful in your Greasemonkey scripts. The only hitch is that you have to trust the host of the libraries you want to bring in… else it’s possible to cause harm to your users.

Here’s an example of how I can load the Prototype library. The key thing here is that you have to wait for the external JavaScript file to load before you can use it. I do this by recursively checking if a variable is available (see the waitForProto function).  Once the library is loaded, I continue with the rest of my script.

var p = unsafeWindow;
function waitForProto() {
    if (typeof p.Prototype=='undefined')
	// set to check every 100 milliseconds if the libary has loaded
        window.setTimeout(waitForProto, 100);
    else
        // call the rest of your code
}
function loadProto() {
	// dynamically creates a script tag
        var proto = document.createElement('script');
        proto.type = 'text/javascript';
        proto.src = 'http://prototype.conio.net/dist/prototype-1.3.1.js';
        document.getElementsByTagName('head')[0].appendChild(proto);
        waitForProto();
}
window.addEventListener(’load’, loadProto(), false);

By the way, after the library is loaded, you can access the “p” object to gain access to the Prototype objects. “p” is a reference to Greasemonkey’s unsafeWindow object.

Has anyone figured out a better way to do this?

Technorati Tags: ,

3 Responses to “Loading External JavaScript Libraries in Greasemonkey”

  1. -<( TechSteward )>- » Blog Archive » WWW Hacking with Firefox and Greasemonkey Says:

    [...] WWW Hacking with Firefox and Greasemonkey Greasemonkey (latest in official blog) is a Firefox extension that allows you to hack browser content on the fly — think of it as CSS on steroids. Here’s a good handling in book format, and starter scripts exist here. Nivi shows a glimpse of just how big a deal this is assuming folks “get it”. Still need more to connect your brain to this new model? Check out Jon Udell’s screencast. Lastly, see Simon Willison’s etiquette suggestions here. Pull external JS libs using this technique. Already using Greasemonkey? Don’t forget to use this compiler to convert your scripts into full-fledged Firefox extensions. Another bunch of must-have hacking extensions platypus, and aardvark. Can this and similar techniques be thwarted by content providers? [...]

  2. Terry’s Blog » Blog Archive » Integrating Greasemonkey with External Libaries as of 2006-07-23 Says:

    [...] Some libraries, like the excellent DOM manipulators jQuery and cssQuery are self contained. If you need to use just a simple script see the example contained in Rich Manalang’s blog post. Richard’s post mentions Prototype. To modify his example to fit your need, you should : [...]

  3. Jim R. Wilson Says:

    Here’s my take on the problem:

    http://jimbojw.com/wiki/index.php?title=Using_Prototype_and_Scriptaculous_with_Greasemonkey

    In my case, I’m injecting both Prototype and Scriptaculous - then using a Scriptaculous object. No polling necessary.

Leave a Reply