Simon Willison’s Weblog

Greasemonkey as a lightweight intermediary

In The architecture of intermediation, Jon Udell discusses the need for a mechanism for a high-level tool for adding custom features to web applications. In Jon’s case, he wants to add a private bookmarks feature to del.icio.us. Jon thought about using a web proxy to intercept and modify del.icio.us pages, but ruled it out as too low-level.

Jon, you need Greasemonkey.

The latest release of the swiss army knife of Firefox extensions adds support for cross-domain XMLHttpRequest calls from greasemonkey scripts. What that means is that you can create a user script (a short JavaScript that will be executed whenever your browser loads specific pages) that can then pull extra data in from another server. This new ability is described in the greasemonkey documentation.

I’m using this for my final year project, a decentralised web annotation system that lets you annotate pages, storing your annotations locally and then sharing your public annotations as a feed (similar to the way RSS aggregators work). The trick there is to run a local web server on some port, then have the Greasemonkey user script (eventually a full extension) communicate with that local server to store and retrieve data. I’m using Ruby on Rails’ built in WEBrick server to prototype the service, and it’s working a treat.

This architecture could be easily adapted to add private bookmarks to del.icio.us—or to add any number of cool features to any number of other sites. Here’s another example: Google’s Desktop Search integrates results from your local drive with the search results page on Google. Using greasemonkey and a local web server tied in to OS X Tiger’s Spotlight indexer, you could add this functionality to any search site you wanted to. Just be sure to lock down the web server to only serve requests from localhost, to avoid sharing search results for your data with anyone on the network who can see your machine.

When people asked me what I was excited about at SxSW, one of my answers was Greasemonkey. This kind of stuff is the reason why.

This is Greasemonkey as a lightweight intermediary by Simon Willison, posted on 30th March 2005.

Tagged ,

Next: Enter the hedgehog

Previous: PyCon observations

Previously hosted at http://simon.incutio.com/archive/2005/03/30/lightweight