- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
BJAX With Greasemonkey for Firefox and IE
A few weeks ago I put together a BJAX example using Greasemonkey (BJAX = Browser Extensions and AJAX). The idea behind BJAX, of course, is that you can use AJAX widgets on third party sites and do cross domain scripting with ease. Two examples of BJAX apps are Book Burro,
which will get you book prices from competing retailers, and MyStickies,
Which will let you put stickies on pretty much any web site and use them on any computer where you have the MyStickies plugin installed. These BJAX applications go quite a bit beyond the original intent of Greasemonkey. They don't just enhance a few link but rather let you insert little mini-applications into someone else's webapp. This is potentially very disruptive to site owners, as they can no longer control their message. Amazon and Barnes and Noble can't be happy that people can seamlessly comparison shop, and the upcoming shareable feature of MyStickies could reduce the ability of community sites to moderate their own content.
My own BJAX application was built just for demo purposes in response to all the people who asked for a concrete example and thus wasn't particularly useful. It put the current weather in downtown Chicago, updated every minute, in a translucent box and floated it over the google.com search page. I decided to update it so it worked both with Firefox and IE and could run in the Greasemonkey clone for IE, Turnabout (get the advanced version, otherwise you won't be able to load your own scripts). The new, updated script can be found here. Three things have changed. Two of the changes are merely dealing with cross browser xml parsing and event handling. The third change is that clicking on the box now will toggle it from maximized to minimized.
Next steps are to port a client side AJAX framework so it uses GM_xmlhttpRequest, can handle being loaded after the page loads in Greasemonkey's sandbox, and doesn't have namespace conflicts with other AJAX frameworks. The mind races. Could GWT be adapted to build BJAX applications?
One potential issue with Greasemonkey and BJAX is that Greasemonkey runs after the page has loaded and all of the page's Javascript has run. What will happen with a true AJAX application that controls its DOM much more closely. Could our inserted DIV get deleted or munged? Should Greasemonkey fire after every DOM update or XMLHttpRequest?
Update 1: Johan Sundström takes me to task for being lazy. I've changed the test for GM_xmlhttpRequest to
typeof GM_xmlhttpRequest != 'function'
Topics: BJAX, Greasemonkey
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
Firefox Extensions and AJAX - A Model for Web 2.0?
Let's face it, cross site AJAX is a real pain. If you're going to provide your components as a service, sort of like Google maps, you're task is not impossible yet still challenging. If you want to mash up your app with other services at the browser level, you've got to proxy those services. Put any way, it's a pain. How to simplify creating truly powerful, cross-site AJAX apps?
Greasemonkey showed the way, demonstrating how you could manipulate pages with Javascript. Book Burro was one of the first AJAX/Greasemonkey hybrids that allowed you to search for a book at your favorite online book merchant, then click on a semi-transparent drop down menu and see book prices at competing retailers. The magic of AJAX! Book Burro has since made the transition from Greasemonkey script to Firefox extension in its own right.
Another example of this curious Firefox Extension/AJAX hybrid caught my eye the other day: MyStickies. This extension will allow you to insert AJAX-DIV stickies onto any web page. The extension handles this insertion and also includes a Javascript library via a Javascript file from mystickies.com and another via chrome://mystickies/ for the behavior.
The sticky location is persisted to the mystickies site where you can also use a webapp to search, manage, etc., your stickies. It's a direct manipulation bookmark that doesn't reside just on your local computer.
Now I happen to think mystickies is pretty cool, especially if they implement some of the features on their roadmaps, like sticky sharing (how web2.0!). But what interests me here is the application architecture. Firefox Extension plus AJAX = a service that can be applied across other web sites. There is clearly a limit to this opportunity -- you have to really like a service and truly, how many of us are willing to keep 20 extensions running, each of which hogs a piece of browser toolbar realestate? Many of these players are already planning IE, Safari and Opera ports.
What shall we call this thing? Browser Extensions and AJAX? BEJAX?
Topics: Ajax Products, Firefox Extensions, Greasemonkey, Web 2.0
Greasemonkey Tricks for AJAX Debugging
This is an old post that shows off a AJAX debugging tool written using Greasemonkey. It's still an interesting idea, but what is perhaps most useful is the long trail of comments that make reference to all sorts of other useful tools such as LiveHTTPHeaders.
Topics: Ajax Tools, Greasemonkey
About Pathfinder
Recent
- Roles Testing For Security
- Blackbird takes the pain out of JavaScript logging
- Making GWT JSON not Quite so Painful
- IDEA - preconference workshop 06 Oct 08
- HTML5, Ajax history management, and The Ajax Experience 2008 Boston
- A Look Back At Past Posts
- Flash Player on iPhone gossip
- Microsoft to Jump on Board EC2
- TAE Boston 2008: The Unsexy Presentations
- The Ajax Experience 2008: Hope to see you in Beantown
Archives
- October 2008
- September 2008
- August 2008
- 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



