Wayback Machine Image Fixer

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

目前为 2015-12-30 提交的版本。查看 最新版本

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

// ==/UserScript==

var pics = document.images;

function specialUses(pic)
{
	var specialCases = [{domain:"northarc.com/images/unsorted/",replacements:[pic.src.replace("thumb.","tn_"),pic.src.replace("thumb.","")],maxDimensions:{width:100,height:80}},
		{domain:"*",replacements:["data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="],condition:pic.src.indexOf("spacer.gif") > -1 || pic.src.indexOf("blank.gif") > -1},
		{domain:"tinypic.com/",replacements:[pic.src.replace("/i","/oi")]}];
	var filteredCases = {};
	filteredCases.replacements = [];
	for(var c = 0; c < specialCases.length; c++)
	{
		var additionalCondition = specialCases[c].condition !== undefined ? specialCases[c].condition : true;
		if((pic.src.indexOf(specialCases[c].domain) > -1 || specialCases[c].domain == "*") && additionalCondition)
		{
			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];
		
	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 target.src = value[v];
				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")
		{
			//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;
		}
	});
	
	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) resolve(JSON.parse(response.responseText).archived_snapshots.closest.url);
				else resolve(null);
			}
			});
		}).catch(function(e) { return APITest(replacement); });
		return subPromise;
	}
	
	function switchWithResize(url, target, width, height)
	{
		var oldDimensions = {width:width, height: height};
		target.src = url;
		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 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]);
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址