Sometimes a website doesn’t function correctly. Even if a website works fine we may want it to display or perform differently. With a few web development and javascript skills we can change a website for ourselves using the Greasemonkey Firefox extension.

Below this paragraph we have a web based sales invoice. Unfortunately the invoice has a broken image. The image is supposed to display a logo. Perhaps we receive many invoices like this-all with broken images-and we want to print these out. We would prefer the logo to display and print.

Item Title Subtotal Packaging Total
5x Apples 23.78 10.50 34.28
10x Oranges 23.78 10.50 34.28

If we analyse the source code of the webpage we can see that the broken image is trying to load a file at ‘http://www.example.com/images/paymentDetails.gif’. We have a copy of the logo and have uploaded it to ‘http://blog.dingoeweb.com/images/paymentDetails.gif’. We can use greasemonkey to substitute the broken link.

First we need to be running the firefox browser; available for free here. Next we need to install the Greasemonkey Firefox extension; available here. Greasemonkey loads javascript files and executes them on designated webpages automatically.

These scripts must be named using the following convention ’scriptName.user.js’ and include a few special Greasemonkey directives in the script. Please find below, the code for our script followed by a detailed explanation.

// ==UserScript==
// @name          Fix Broken Invoice Image
// @description   blog.dingoeweb.com Greasemonkey example
// @include       http://blog.dingoeweb.com*
// ==/UserScript==
 
var docImages = document.getElementsByTagName("img");
var image;
for(var x=0;x<docImages.length;x++)
{
	image = docImages[x];
	if(image.src == "http://www.example.com/images/paymentDetails.gif")
		image.src = "http://blog.dingoeweb.com/images/paymentDetails.gif";
}

@name and @description provide Greasemonkey with information used to store the script in it’s list and user interface. @include tells Greaemonkey which domains the script should run on. The ‘*’ wildcard instructs Greaemonkey to work on all domains. ‘http://blog.dingoeweb.com*’ tells Greasemonket to run on all pages within the http://blog.dingoeweb.com domain.

// ==UserScript==
// @name          Fix Broken Invoice Image
// @description   blog.dingoeweb.com Greasemonkey example
// @include       http://blog.dingoeweb.com*
// ==/UserScript==

The following code iterates through every image in the document object model (DOM). If the image is trying to load the broken image; we replace it with our own, forcing the image to display.

var docImages = document.getElementsByTagName("img");
var image;
for(var x=0;x<docImages.length;x++)
{
	image = docImages[x];
	if(image.src == "http://www.example.com/images/paymentDetails.gif")
		image.src = "http://blog.dingoeweb.com/images/paymentDetails.gif";
}

To install the script open in in Firefox using the native ‘open’ command or click here. After reloading the page we will see that our image nowdisplays correctly. More user scripts can be downloaded from here. More information on Greasemonkey can be found here.