Attempts to fix broken images by replacing them with working timestamps based on JSON results
当前为
// ==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.1.6
// @grant GM_xmlhttpRequest
// ==/UserScript==
var pics = document.images;
var timestamp = /web\/(\d{1,14})/.exec(window.location.href)[1];
function replaceImage(target)
{
var originalURL = target.src.substring(target.src.lastIndexOf("http"));
var newURL = GM_xmlhttpRequest({
url: "http://archive.org/wayback/available?url=" + originalURL + "×tamp=" + 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 testThingy(pic)
{
var originalURL = pic.src.substring(pic.src.lastIndexOf("http"));
var newScript = document.createElement("script");
newScript.type = "application/javascript";
newScript.src = "http://archive.org/wayback/available?url=" + originalURL + "×tamp=" + timestamp + "&callback=replaceImage";
document.head.appendChild(newScript);
}
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.
if(response.responseText.indexOf("Impatient?") > -1)
{
var doc = document.implementation.createHTMLDocument("Possible Replacement");
doc.documentElement.innerHTML = response.responseText;
pic.src = doc.getElementsByClassName("impatient")[0].firstChild.href;
replaceImage(pic);
}
else 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]);
evaluateImage(pics[i]);
}