Wayback Machine Image Fixer

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

Versión del día 24/10/2015. 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/*
// @exclude       http://web.archive.org/web/*.jpg
// @exclude       http://web.archive.org/web/*.jpeg
// @exclude       http://web.archive.org/web/*.png
// @exclude       http://web.archive.org/web/*.gif
// @exclude       http://web.archive.org/web/*.bmp
// @version       1.1.3
// @grant         GM_xmlhttpRequest 

// ==/UserScript==

var pics = document.images;

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

function replaceImage(target)
{
	var originalURL = target.src.substring(target.src.lastIndexOf("http"));
	var newURL = GM_xmlhttpRequest({
		url: "http://archive.org/wayback/available?url=" + originalURL + "&closest=" + timestamp,
		method: "GET",
		headers: {"Accept": "application/json"},
		onload: function(response) {
			if (response.readyState == 4) {
				if(response.status == 503)
				{
					//We caught a 503, which means that the API wasn't "ready" for us yet. Let's try again
					replaceImage(target)
				}
				else if(JSON.parse(response.responseText).archived_snapshots.closest == null)
				{
					//Try and "expose" image links that are unclickable due to the image not loading
					if(target.alt == "" && target.width == 0 && target.parentNode.nodeName == "A")
					{
						//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.substring(target.src.lastIndexOf("http"));
						target.width = 25;
						target.height = 25;
					}
					return;
				}
				else target.src = JSON.parse(response.responseText)archived_snapshots.closest.url;
			}
		}
	});
}

function evaluateImage(pic)
{
	GM_xmlhttpRequest({
		url: pic.src,
		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);
			}
		}
	});
}

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;
	evaluateImage(pics[i]);
}