Wayback Machine Image Fixer

Attempts to fix broken images by replacing them with working timestamps based on JSON results

Versión del día 04/01/2016. Echa un vistazo a la versión más reciente.

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

You will need to install an extension such as Tampermonkey to install this script.

Necesitarás instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Necesitará instalar una extensión como Tampermonkey para instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión como Stylus para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

Necesitará instalar una extensión del gestor de estilos de usuario para instalar este estilo.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name          Wayback Machine Image Fixer
// @namespace     DoomTay
// @description   Attempts to fix broken images by replacing them with working timestamps based on JSON results
// @include       http://web.archive.org/web/*
// @include       https://web.archive.org/web/*
// @exclude       /\*/
// @exclude       *.jpg
// @exclude       *.jpeg
// @exclude       *.png
// @exclude       *.gif
// @exclude       *.bmp
// @version       1.4.0
// @grant         GM_xmlhttpRequest

// ==/UserScript==

var pics = document.images;
var backgrounds = document.querySelectorAll("[background]");

function specialUses(pic)
{
	var imgRef = pic.src || relativeToAbsolute(pic.background);
	var specialCases = [{domain:"northarc.com/images/unsorted/",replacements:[imgRef.replace("thumb.","tn_"),imgRef.replace("thumb.","")],maxDimensions:{width:100,height:80},tags:"before"},
		{domain:"*",replacements:["data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="],condition:imgRef.indexOf("spacer.gif") > -1 || imgRef.indexOf("blank.gif") > -1,tags:"after"},
		{domain:"tinypic.com/",replacements:[imgRef.replace("/i","/oi")],tags:"after"},
		{domain:"logitech.com",replacements:["http://www.logitech.com/logitech/new6-1.gif"],condition:imgRef.indexOf("home_header.gif") > -1,tags:"before"},
		{domain:"logitech.com",replacements:["http://www.logitech.com/logitech/new6-3.gif"],condition:imgRef.indexOf("home_footer.gif") > -1,tags:"before"},
		{domain:"logitech.com",replacements:["http://www.logitech.com/logitech/prod_row.gif"],condition:imgRef.indexOf("Images/main_navigator.gif") > -1,tags:"before"},
		{domain:"logitech.com",replacements:["http://www.logitech.com/logitech/al1.gif"],condition:imgRef.indexOf("Images/navigator.gif") > -1,tags:"before"},
		{domain:"logitech.com",replacements:["http://www.logitech.com/logitech/msensa.gif"],condition:imgRef.indexOf("Images/mmsensa.gif") > -1,tags:"before"},
		{domain:"*",replacements:["http://www.surpara.com/img/ban/sp.gif","http://www.surpara.com/img/ban/sp2.gif","http://www.surpara.com/img/ban/sp3.gif","http://www.surpara.com/img/ban/sp4.gif","http://www.surpara.com/img/ban/sp5.gif","http://www.surpara.com/img/ban/sp6.gif","http://www.surpara.com/img/ban/sp7.gif"],condition:pic.width == 88 && pic.height == 31 && pic.parentNode.href.indexOf("surpara") > -1,tags:"random,after"},
		{domain:"*",replacements:["http://www.gameha.com/gameha_m.gif"],condition:pic.width == 88 && pic.height == 31 && pic.parentNode.href.indexOf("gameha.com") > -1,tags:"after"},
		{domain:"*",replacements:["http://www.oekaki.net/img/bn2_1.gif","http://www.oekaki.net/img/bn2_2.gif","http://www.oekaki.net/img/bn2_3.gif","http://www.oekaki.net/img/bn2_4.gif","http://www.oekaki.net/img/bn2_5.gif","http://www.oekaki.net/img/bn2_6.gif"],condition:pic.width == 88 && pic.height == 31 && pic.parentNode.href.indexOf("oekaki.net") > -1,tags:"random,after"},
		{domain:"*",replacements:["http://brandk.net/search/img2/s_bnr_11.jpg","http://brandk.net/search/img2/s_bnr_10.jpg","http://brandk.net/search/img2/s_bnr_08.jpg","http://brandk.net/search/img2/s_bnr_04.jpg","http://brandk.net/search/img2/s_bnr_01.jpg"],condition:pic.width == 88 && pic.height == 31 && pic.parentNode.href.indexOf("brandk.net") > -1,tags:"random,after"},
		{domain:"*",replacements:["http://ragsearch.com/bana/banas.gif"],condition:pic.width == 88 && pic.height == 31 && pic.parentNode.href.indexOf("ragsearch.com") > -1,tags:"after"},
		{domain:"*",replacements:["http://celest.pobox.ne.jp/links/imgs/bn88.gif"],condition:pic.width == 88 && pic.height == 31 && pic.parentNode.href.indexOf("celest.pobox.ne.jp") > -1,tags:"after"},
		{domain:"*",replacements:["http://td9.sakura.ne.jp/links/banner_w+prinsess.gif"],condition:pic.width == 88 && pic.height == 31 && pic.parentNode.href.indexOf("http://blue.sakura.ne.jp/~real") > -1,tags:"after"}];
	var filteredCases = {};
	filteredCases.replacements = [];
	var tagScan = ["before","between","after"];
	for(var t = 0; t < 3; t++)
	{
		if(t == 1) filteredCases.replacements = filteredCases.replacements.concat(imgRef);
		else
		{
			for(var c = 0; c < specialCases.length; c++)
			{
				var additionalCondition = specialCases[c].condition !== undefined ? specialCases[c].condition : true;
				if((imgRef.indexOf(specialCases[c].domain) > -1 || specialCases[c].domain == "*") && additionalCondition && specialCases[c].tags.indexOf(tagScan[t]) > -1)
				{
					if(specialCases[c].tags.indexOf("random") > -1) filteredCases.replacements = filteredCases.replacements.concat(specialCases[c].replacements[Math.floor(Math.random() * specialCases[c].replacements.length)]);
					else filteredCases.replacements = filteredCases.replacements.concat(specialCases[c].replacements);
					if(specialCases[c].maxDimensions) filteredCases.maxDimensions = specialCases[c].maxDimensions;
				}
			}
		}
	}
	return filteredCases;
}

var timestamp = /web\/(\d{1,14})/.exec(window.location.href)[1];

function replaceImage(target, altURLs)
{
	var possibleUses = specialUses(target);
	var URLGuesses = [];
	if(altURLs && altURLs.length > 0) URLGuesses = altURLs;
	else if(possibleUses.replacements.length > 0) URLGuesses = possibleUses.replacements;
	else URLGuesses = [target.src || relativeToAbsolute(target.background)];
	
	var testSet = [];
	for(var p = 0; p < URLGuesses.length; p++)
	{
		if(URLGuesses[p].indexOf("http") > -1) testSet[p] = APITest(URLGuesses[p]);
		else if(URLGuesses[p].indexOf("data:") > -1)
		{
			testSet[p] = Promise.resolve(URLGuesses[p]);
			break;
		}
	}
	Promise.all(testSet).then(function(value) {
		for(var v = 0; v < value.length; v++)
		{
			if(value[v] !== null)
			{
				if(possibleUses.maxDimensions) switchWithResize(value[v], target, possibleUses.maxDimensions.width, possibleUses.maxDimensions.height);
				else changeImage(value[v], target);
				//There might have been an image map that had to be "patched" by one of my other scripts. The patch isn't needed anymore, so this will remove it
				if(target.getAttribute("usemap") && document.body.querySelector("svg[class ~= " + target.getAttribute("usemap").substring(1) + "]"))
				{
					var SVGPatch = document.body.querySelector("svg[class ~= " + target.getAttribute("usemap").substring(1) + "]");
					SVGPatch.parentNode.removeChild(SVGPatch);
				};
				return;
			}
		}
		//Try and "expose" image links that are unclickable due to the image not loading
		if(target.alt === "" && target.width === 0 && target.parentNode.nodeName === "A" && target.src)
		{
			target.width = 25;
			target.height = 25;
			//"Changing" the source is pretty hacky, but it's the only way I can think of to turn "invisible" image links into something clickable
			target.src = target.src;
		}
	});
	
	function APITest(replacement)
	{
		var subPromise = new Promise(function(resolve,reject) {
			var originalURL = replacement.substring(replacement.lastIndexOf("http"));
			GM_xmlhttpRequest({
			url: "http://archive.org/wayback/available?url=" + originalURL + "&timestamp=" + timestamp,
			method: "GET",
			headers: {"Accept": "application/json"},
			onload: function(response) {
				if(response.status == 503) reject(response.statusText + " for " + originalURL);
				else if(JSON.parse(response.responseText).archived_snapshots.closest !== undefined)
				{
					//Evidently an additional layer of examination is needed to make absolutely sure we have an actual image
					GM_xmlhttpRequest({
					url: JSON.parse(response.responseText).archived_snapshots.closest.url,
					method: "HEAD",
					onload: function(secondResp) {
						if(secondResp.responseHeaders.indexOf("Content-Type: text/html") == -1) resolve(this.url);
						else resolve(null);
					}
					});
				}
				else resolve(null);
			}
			});
		}).catch(function(e) { return APITest(replacement); });
		return subPromise;
	}
	
	function switchWithResize(url, target, width, height)
	{
		var oldDimensions = {width:width, height: height};
		changeImage(url, target);
		target.onload = function() {
			var aspectRatio = target.naturalWidth / target.naturalHeight;
			if(target.naturalWidth >= target.naturalHeight)
			{
				target.width = oldDimensions.width;
				target.height = oldDimensions.width / aspectRatio;
			}
			else if(target.naturalWidth <= target.naturalHeight)
			{
				target.height = oldDimensions.height;
				target.width = oldDimensions.height * aspectRatio;
			}
		};
	}
	
	function changeImage(url, target)
	{
		if(target.src) target.src = url;
		else if(target.background) target.background = url;
	}
}

function relativeToAbsolute(bgURL)
{
	var img = new Image();
	img.src = bgURL;
	return img.src;
}

function evaluateImage(pic)
{
	GM_xmlhttpRequest({
		url: pic.src,
		method: "GET",
		onload: function(response) {
			//Going off of response code is unreliable. Sometimes an image will return a status code of 200 even though it would redirect to an error page should you view the image directly, so we're looking at content type instead
			if(response.responseHeaders.indexOf("Content-Type: text/html") > -1)
			{
				//This might be a case where if you were visit the image directly, you would be redirected elsewhere. This attempts to catch that and replace the pic's src with where it would take you.
				var doc = document.implementation.createHTMLDocument("Possible Replacement");
				doc.documentElement.innerHTML = response.responseText;
				if(doc.getElementsByClassName("impatient")[0])
				{
					GM_xmlhttpRequest({
						url: doc.getElementsByClassName("impatient")[0].firstChild.href,
						method: "HEAD",
						onload: function(response) {
							//Going off of response code is unreliable. Sometimes an image will return a status code of 200 even though it would redirect to an error page should you view the image directly, so we're looking at content type instead
							if(response.responseHeaders.indexOf("Content-Type: text/html") == -1)
							{
								replaceImage(pic,[doc.getElementsByClassName("impatient")[0].firstChild.href]);
							}
							else
							{
								replaceImage(pic);
							}
						}
					});
				}
				else if(response.status != 403)
				{
					replaceImage(pic);
				}
			}
		}
	});
}

for(var i = 0; i < pics.length; i++)
{
	//Skip over stuff related to the Wayback Machine toolbar and data URIs
	if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(pics[i])) || pics[i].src.indexOf("data:") > -1) continue;
	if(pics[i].src.indexOf("ttp://") === 0) pics[i].src = "web.archive.org/web/" + timestamp + "/h" + pics[i].src;
	evaluateImage(pics[i]);
}

for(var b = 0; b < backgrounds.length; b++)
{
	evaluateImage(backgrounds[b]);
}