Wayback Machine Image Fixer

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

Verzia zo dňa 07.10.2015. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==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.0.1
// @grant         GM_xmlhttpRequest 

// ==/UserScript==

var pics = document.images;

function replaceImage(target)
{
	var originalURL = target.src.substring(target.src.lastIndexOf('http'));
	var newURL = GM_xmlhttpRequest({
		url: "http://archive.org/wayback/available?url=" + originalURL,
		method: "GET",
		headers: {"Accept": "application/json"},
		onload: function(response) {
			if(JSON.parse(response.responseText)["archived_snapshots"]["closest"] == undefined)
			{
				//Try and "expose" image links that are unclickable due to the image not loading
				if(target.alt == "" && target.width == 0)
				{
					//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;
			}
			target.src = JSON.parse(response.responseText)["archived_snapshots"]["closest"]["url"];
		}
	});
}

function isImageValid(url)
{
	var lookup = GM_xmlhttpRequest({
		url: url,
		method: "HEAD",
		synchronous: true
	});
	//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
	return (lookup.responseHeaders.indexOf("Content-Type: text/html") == -1);
}

for(var i = 0; i < pics.length; i++)
{
	//Skip over stuff related to the Wayback Machine toolbar
	if(isInToolbar(pics[i])) continue;
	//See if the image needs to be replaced in the first place
	if (isImageValid(pics[i].src)) continue;
	replaceImage(pics[i]);
}

function isInToolbar(node,parent)
{
	var baseNode = node;
	var found = false;
	while(baseNode != document)
	{
		baseNode = baseNode.parentNode;
		if(baseNode == document.getElementById("wm-ipp"))
		{
			return true;
		}
	}
	return false;
}