Wayback Machine Favicon Fixer

Attempts to add a favicon to a site crawled by the Wayback Machine in the event one does not come up normally

当前为 2016-01-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          Wayback Machine Favicon Fixer
// @namespace     DoomTay
// @description   Attempts to add a favicon to a site crawled by the Wayback Machine in the event one does not come up normally
// @version       1.0.0
// @include       http://web.archive.org/web/*
// @include       http://wayback.archive.org/web/*
// @include       https://web.archive.org/web/*
// @include       https://wayback.archive.org/web/*
// @exclude       /\*/
// @grant         GM_xmlhttpRequest

// ==/UserScript==

var domain = window.location.href.substring(0,window.location.href.indexOf("/",window.location.href.lastIndexOf("//") + 2));
var timestamp = /web\/(\d{1,14})/.exec(window.location.href)[1];

var originalDomain = domain.substring(domain.lastIndexOf("http"));
if(!document.querySelector("link[rel~='icon']")) retrieveFavicon(window.location.href.substring(window.location.href.lastIndexOf("/http") + 1,window.location.href.lastIndexOf("/") + 1));

function retrieveFavicon(base)
{
	GM_xmlhttpRequest({
		url: "http://archive.org/wayback/available?url=" + base + "favicon.ico" + "&timestamp=" + timestamp,
		method: "GET",
		headers: {"Accept": "application/json"},
		onload: function(response) {
			if(response.status == 503) retrieveFavicon(base);
			else if(JSON.parse(response.responseText).archived_snapshots.closest)
			{
				var newFavicon = document.createElement("link");
				newFavicon.type = "image/x-icon";
				newFavicon.rel = "shortcut icon";
				newFavicon.href = JSON.parse(response.responseText).archived_snapshots.closest.url;
				document.head.appendChild(newFavicon);
			}
			else
			{
				var furtherTrim = base.substring(0,base.lastIndexOf("/",base.lastIndexOf("/") - 1) + 1);
				if(furtherTrim.indexOf("//") != (furtherTrim.length - 2)) retrieveFavicon(furtherTrim);
			}
		}
	});
	}