User:AoV2/fix image links

From Wikipedia, the free encyclopedia
Jump to: navigation, search

This script reverts any over-ridden (or nullified) image links back to the original state, causing each image to link to its own description page without exception. ―AoV² 08:51, 1 March 2010 (UTC)

function fix_image_links() {
	//to-do: properly discern which parts of image-page to ignore
	if(wgNamespaceNumber == 6) return; 
 
	img = document.getElementsByTagName("img");
	for(i = 0; i < img.length; i++) {
		pn = img[i].parentNode;
		// if we have no “a” element of which to change the href, create one now
		if(pn.nodeName != "A") {
			a = document.createElement("a");
			a.appendChild(img[i].cloneNode(true));
			pn.replaceChild(a, img[i]);
			}
		a = img[i].parentNode;
		if(m = img[i].src.match(/\/[a-f0-9]\/[a-f0-9]{2}\/([^\/]+)/))
			a.href = wgArticlePath.replace("$1", wgFormattedNamespaces[6] + ":" + m[1]);
		}
	}
 
addOnloadHook(fix_image_links);