- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
BJAX With Greasemonkey
I've had a number of people write me wanting to know how to do BJAX (Browser Extension and AJAX) with Greasemonkey on Firefox. I thought I'd give a simple little example here. The example will show a small box in the top left corner of a google page. This box will display the weather in Chicago as served up by a Yahoo! RSS feed, refreshed every 30 seconds. Yes, yes, this isn't terribly useful, but that's not the point. Also, this is a quick hack. It won't work on IE or probably most versions of browsers.
OK, having dispensed with preliminaries, let's get started. First you'll need to install Greasemonkey. Once you've done that, download the example script here. You'll need to load it in a browser window and install it.
There are a couple of differences in programming Javascript in Greasemonkey as opposed to a regular browser environment. We'll only touch on a few of these here, but you should have a look at Dive Into Greasemonkey for a more complete treatment.
OK, the first step in our extension involves looking for a particular method:
if (typeof GM_xmlhttpRequest != 'function') {
return;
As you can probably guess, GM_xmlhttpRequest is a special version of XMLHttpRequest that allows you to access any remote site. It isn't present in all versions of Greasemonkey, so you have to check for it. Next, we check that we have the top window. We don't want the box showing up in IFrames.
if (window == top) {
The next bit looks pretty standard; we create a div, give a few of the elements id's and insert it at the beginning.
var mybox = document.createElement("div");
mybox.innerHTML = '<div id="bjax_box" style="margin: 0 auto 0 auto; ' +
'position:absolute; left:15px; top:15px; width:175px; opacity: .75; filter: alpha(opacity=75); z-index:100; ' +
'margin: 5px; padding: 5px; overflow: hidden; height: auto; ' +
'font-size: 8pt; font-weight: bold; font-family: arial, sans-serif; background-color: #ccffcc; ' +
'color: #000000;"> ' +
'BJAX in Action! <br/> Chicago weather: <br/> ' +
'<p id="bjax_weather"></p>' +
'</div>';
document.body.insertBefore(mybox, document.body.firstChild);
There remains the periodic call to GM_xmlhttpRequest. We parse the RSS XML and grab the second description. It's an example. In an actual app you would want to be a little more careful.
window.setInterval(function() {
GM_xmlhttpRequest({
method: 'GET',
url: 'http://xml.weather.yahoo.com/forecastrss?p=60602',
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function(responseDetails) {
// convert string to XML object
var xmlobject = (new DOMParser()).parseFromString(responseDetails.responseText, "text/xml");
var condition = xmlobject.getElementsByTagName('description')[1];
var weather = document.getElementById('bjax_weather');
weather.innerHTML = condition.firstChild.nodeValue;
}
});
}, 30 * 1000);
A few quirks you may want to watch out for (see also this pitfalls articles):
- When you get a DOM element in Greasemonkey, it will be a
XPCNativeWrapper. That type doesn't have many properties you've come to expect, such as onclick. You can add event listeners usingdocument.addEventListener('click',...instead. - The scope of functions and variables. Greasemonkey wraps your code in an anonymous functions, so you variables and functions are not available to other Javascript in the file. Use anonymous functions where you have to pass a function pointer.
Now that you've been through this little example, you may want to uninstall the example BJAX component. Who, after all, wants weather hanging over their google search?
One additional interesting feature of Greasemonkey is that once you've written your script, you can compile it into a Firefox extension.
Topics: Ajax Examples, BJAX, Greasemonkey
Comments: 1 so far
Leave a comment
About Pathfinder
Recent
- Project Website Part 4: Drag and Drop in jQuery
- The App Store, iPhone, and You
- Multiple Column Sorting with Drag and Drop using Scriptaculous
- Five jQuery plugins that are a joy to use
- Visualizing Your Database Schema Entirely in Rails
- jQuery plugins: Five tips for separating the good from the bad and the ugly
- Resolved: Should schema.rb be included in your source control?
- Flash 10 - FileReference Runtime Access
- Papervision3d 2.0 (Great White) in Flex 3 (Part I)
- MetaWidget - Convention over Configuration UI
Archives
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006



The prerequisite test “if(!GM_xmlhttpRequest)” is more appropriately phrased as “if(typeof GM_xmlhttpRequest != ‘function’)”, since the former yields an exception, whereas the latter actually executes the return statement as your code flow suggests.
Comment by Johan Sundström, Tuesday, August 1, 2006 @ 11:03 am