NOTE: this blog entry was originally posted July 2nd, 2007 on my previous blogging system (dev2dev.bea.com). Comments on the old blog were not transferred.
This is part 1 of a series of blog entries on Greasemonkey mashups.
- Blog 1: More Mashups: Using Greasemonkey to Weave New Features into Web Sites
- Blog 2: Building a Greasemonkey Mashup Tutorial
- Blog 3: Greasemonkey in the Enterprise: When is GM the Right Tool for IT?
- Blog 4: Beware of Greasemonkey's Inverted Security Model
- Blog 5: Solving the Greasemonkey Script Versioning Problem
Client side Code Injection with Greasemonkey
Greasemonkey is just cool - and I have been having a lot of fun playing around with it. It operates on a simple principle, but delivers massive power from that simplicity.
To be useful, a Greasemonkey script generally will be targeted towards a specific web site, though a general purpose script is possible. The developer specifies which URLs (with wildcards) are valid for the script. The Greasemonkey plug-in in Firefox will silently watch the user's URLs as they browser, and will inject a script when appropriate.
To be clear, Greasemonkey is not installed by default, nor are the scripts that you write. It is an opt-in activity for the user to both install Greasemonkey and then each script that they want to have. This means that Greasemonkey is not quite as accesible as other mashup solutions that I have written about - the mashup here is not a URL. The user must be skilled enough to install Greasemonkey and your script.
Greasemonkey Hello World
Installing Greasemonkey is straightforward: you will find the download link at the Greasemonkey Home Page. You will need to use Firefox of course, but otherwise it can't go wrong. After you have it installed, the next step is to install your first Greasemonkey script.
Unlike a lot of cutting edge projects out on the web, Greasemonkey has excellent documentation. Primarily, the best place to go for an introduction, tutorials, and documentation is Mark Pilgrim's online book, Dive Into Greasemonkey. The book is quite comprehensive and is geared towards getting you on your feet quickly.
The meta-data is hopefully self-explanatory: it instructs Greasemonkey to execute it on any (*) site except for a couple of exclusions. If you install Greasemonkey, and then install this script helloworld.user.js by clicking on the link, you will see that it just pops up an alert box for every page view.
// Hello World! example user script
// version 0.1 BETA!
// Copyright (c) 2005, Mark Pilgrim
// Released under the GPL license
// @name Hello World
// @namespace http://diveintogreasemonkey.org/download/
// @description example script to alert "Hello world!"
// @include *
// @exclude http://diveintogreasemonkey.org/*
// @exclude http://www.diveintogreasemonkey.org/*
Mr. Wong and BEA dev2dev
If you visit BEA's dev2dev site, you will notice that Jon has helpfully included links to popular tagging services on all articles and blogs. This allows you to quickly tag the article or blog using del.icio.us, Digg, DZone, Furl or Reddit. See the image below to see how the site works today:
However, what if that list is not sufficient for your needs? That is my problem - there is one tagging site that I use that is not listed. As an aside, I have been doing research into the spread of Web 2.0 in China. In doing that work, I have been tagging chinese articles that I find, and some english ones just to be helpful. I am not using an english language tagging site, I am using Mr. Wong, which is a Chinese language tagging site. I would like to have Mr. Wong as one of my tagging options when browsing dev2dev. But since I don't have access to the dev2dev code, I cannot add it.
Weaving Mr. Wong into BEA dev2dev Using Greasemonkey
The process for developing a Mr. Wong feature on dev2dev is as follows:
- Navigate to dev2dev, and View Source on the HTML for blogs and articles
- Note that the tagging links box is easily found in both by looking for the last
divtag with class box_gray
- The div tag contains a simple HTML table, all we need to do is add a new row
Before I show the code for the script, here is the result:
As you can see, even though I did not have access to dev2dev code, I was able to easily add a brand new feature to it!
The Mr. Wong Script
The script used to add this feature to dev2dev is below. But if you would like to try it out, or view the live version, navigate to my script repository for Greasemonkey (or here if that link does not work). Install the script named dev2dv Mr Wong. The code roughly follows this path:
- Find the last box_gray
divtag on the page, this is the tagging box
- Find the inner
Add a new row to that table
- Populate the row with a link and image for Mr. Wong
// @name dev2dev Mr Wong
// @namespace http://dev2dev.bea.com
// @description Injects a link to Mr Wong on each blog/article link
// @include http://dev2dev.bea.com/blog/*
// @include http://dev2dev.bea.com/pub/*
var divTagsWithClass, taggingDiv;
GM_log('Running dev2dev Mr Wong script');
// get the existing tagging div box
// by looking for the class (box_gray)
divTagsWithClass = document.evaluate(
// the tagging div appears last
taggingDiv = divTagsWithClass.snapshotItem(divTagsWithClass.snapshotLength-1);
// find the table
tableTag = taggingDiv.getElementsByTagName('table');
// create the new row
var lastIndex = tableTag.rows.length;
newLinkTR = tableTag.insertRow(lastIndex);
// create the td for image and set styles
var newLinkTD = newLinkTR.insertCell(0);
newLinkTD.valign = 'bottom';
newLinkTD.width = '20';
// build the HTML
newLinkTD.innerHTML = '<img src='+
' alt="Mr. Wong" border="0" height="18" '+
// create the td for the image
var newLinkTD = newLinkTR.insertCell(1);
// set the styles
newLinkTD.nowrap = 'nowrap';
newLinkTD.valign = 'bottom';
// build the HTML
GM_log(' Error: did not find the tagging inner table');
GM_log(' Error: did not find a tagging div');
Pros and Cons
Hopefully you can see the power and simplicity of the Greasemonkey plugin. In a follow up blog I will show how you can take this technique further by injecting data from other sites to create a true mashup. Here is what I see as the pros and cons of this tool:
- Pro: it is easy for a developer to install both the plugin and scripts
- Pro: you do not need access to the web site's code to add new features
- Pro: the documentation and developer community around Greasemonkey are superb
- Con: mashups created with Greasemonkey aren't exactly URL accessible, it requires install steps
- Con: Greasemonkey is not supported on IE or any browser other than Firefox
- Con: there is a significant security issue that I will cover in my next blog entry
- Greasemonkey Home - download GM here
- Dive Into Greasemonkey - Mark Pilgrim's excellent online guide to Greasemonkey
- My script repository for Greasemonkey, includes my dev2dev scripts
- Programmable Web tag for Greasemonkey - not much there so far...
- Blog about the game changing nature of Greasemonkey