Thursday, July 26, 2007

Greasemonkey: Making the internet slightly less annoying

One of the more annoying memes I've seen on some blogs is the use of "Canadia" instead of "Canada". The level of annoyance has finally risen high enough to overcome a certain level of laziness on my part. So I downloaded Greasemonkey and wrote my first JavaScript.

I'm sure it is not the best script ever written, but I now feel like I've done my part to make the internet a better place.

// ==UserScript==
// @name Canadia
// @description Change "Canadia" to "Canada"
// @include *
// ==/UserScript==

(function() {
var expression, replacement, regex, textnodes, node, s;

expression = "([Cc]anad)ia([^n]|$)";
replacement = "$1a$2";

regex = new RegExp(expression, 'g');

textnodes = document.evaluate( "//body//text()", document, null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for (var i = 0; i < textnodes.snapshotLength; i++) {
node = textnodes.snapshotItem(i);
s = node.data;
s = s.replace(regex, replacement);
node.data = s;
}
})();