User:Gerbrant/edit/linksInDiff.js

From Wikipedia, the free encyclopedia

< User:Gerbrant | edit

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

hookEvent("load", function()
{
	var d = document.getElementById("wikiDiff"), i, t;
	if(!d && document.getElementById("difference"))
		d = document.getElementById("bodyContent");
	if(d)
	{
		d = d.getElementsByTagName("TABLE").item(0);
		if(d && d.className == "diff")
		{
			d = d.getElementsByTagName("TD");
			for(i = 0; i < d.length; i++)
			{
				t = d.item(i);
				switch(t.className)
				{
				case "diff-context":
				case "diff-deletedline":
				case "diff-addedline":
					t.innerHTML = t.innerHTML.replace(/\[\[((.*?)(?:\|.*?)?)\]\]/g,	function(a, b, p)
					{
						var h = /<[^>]*>/g;
						var o = '<a href="' + wgArticlePath.replace("$1", p.replace(h, "")) + '">';
						var c = '</a>';
						return "[[" + o + b.replace(h, c + "$&" + o) + c + "]]";
					});
				}
			}
		}
	}
});