// ==UserScript==
// @name           Test
// @namespace      http://userscripts.org/users/28612
// @include        http://userscripts.org/forums/2/topics/2188
// ==/UserScript==

var word="google";
var url="http://www.google.com";

var func=new Function("match","var link=document.createElement(\"A\"); link.textContent=match; link.setAttribute(\"href\",\""+url+"\"); return link;}");

replaceTextContent(new RegExp(word,"gmi"),func);

function replaceTextContent(regexp,handler)
{
	var snapshots=document.evaluate("//body//text()",document,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
	for(var num1=0;num1<snapshots.snapshotLength;num1++)
	{
		regexp.lastIndex=0;
		var node1=snapshots.snapshotItem(num1);
		var match1=regexp.exec(node1.textContent);
		if (match1)
		{
			var node2=node1.parentNode;
			var node3=node1.nextSibling;
			node2.removeChild(node1);
			while (match1)
			{
				node2.insertBefore(document.createTextNode(RegExp.leftContext),node3);
				try{node2.insertBefore(handler(match1),node3);}
				catch(ex){node2.insertBefore(document.createTextNode(match1),node3);}
				regexp.lastIndex=0;
				match1=regexp.exec(RegExp.rightContext);
			}
			node2.insertBefore(document.createTextNode(RegExp.rightContext),node3);
		}
	}
}