Wayback Machine Small Bug Fixes

Fixes encoded ampersands on Wayback Machine's captures graph and problems that arise when trailing slashes are missing in an URL and other small issues universally present in all crawled sites

当前为 2016-06-07 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Wayback Machine Small Bug Fixes
// @namespace     DoomTay
// @description   Fixes encoded ampersands on Wayback Machine's captures graph and problems that arise when trailing slashes are missing in an URL and other small issues universally present in all crawled sites
// @version       1.3.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/*
// @run-at        document-start
// @exclude       /\*/
// @grant         none

// ==/UserScript==

var lastFolder = window.location.href.substring(window.location.href.lastIndexOf("/") + 1);
var pics = document.images;
var backgrounds = document.querySelectorAll("[background]");
var shouldHaveTrailingSlash = (window.location.href.lastIndexOf(".") < window.location.href.lastIndexOf("/") || window.location.href.substring(window.location.href.lastIndexOf("//") + 2) == lastFolder) && !lastFolder.includes("?");
var hasTrailingSlash = window.location.href.endsWith("/");
var domain = window.location.href.substring(0,window.location.href.indexOf("/",window.location.href.lastIndexOf("//") + 2));
var isInDomain = window.location.href == domain || window.location.href == domain + "/";
var timestamp = /web\/(\d{1,14})/.exec(window.location.href)[1];
var checkedNodes = [];

if(!document.getElementsByTagName("base")[0])
{
	var base = document.createElement("base");
	if(shouldHaveTrailingSlash && !hasTrailingSlash) base.href = window.location.href + "/";
	else if((!hasTrailingSlash && !shouldHaveTrailingSlash) || hasTrailingSlash) base.href = document.baseURI;
	else base.href = domain + "/";
	document.head.appendChild(base);
}

function relativeToAbsolute(bgURL)
{
	var img = new Image();
	img.src = bgURL;
	return img.src;
}

function fixURL(URL)
{
	if(URL.includes(document.domain)) return domain + URL.substring(URL.indexOf("/",URL.lastIndexOf("//") + 2));
	else return domain.substring(0,domain.indexOf("/http") + 1) + URL.substring(URL.indexOf("/http") + 1);
}

function fixImage(pic)
{
	var oldSrc = pic.getAttribute("src");
	if(!oldSrc) return;
	if(oldSrc.includes("archive.org")) oldSrc = oldSrc.substring(oldSrc.indexOf(document.domain) + document.domain.length);
	if(oldSrc.startsWith("/")&& !oldSrc.startsWith("/web/")) pic.setAttribute("src",domain + oldSrc);
	else if (oldSrc.startsWith("../") && isInDomain) pic.setAttribute("src",oldSrc.substring(3));
	else pic.setAttribute("src",domain.substring(0,domain.indexOf("/http") + 1) + pic.src.substring(pic.src.indexOf("/http") + 1));
}

function changeBackground(node, newBackground)
{
	if(node.background) node.background = newBackground;
	else if(node.getAttribute("background")) node.setAttribute("background",newBackground);
}

var observer = new MutationObserver(function(mutations) {
	mutations.forEach(function(mutation) {	
		if(mutation.target.nodeName != "HEAD" && mutation.target.nodeName != "BODY") checkMutations(mutation.target);
		if(checkedNodes.includes(mutation.target)) checkedNodes.splice(checkedNodes.indexOf(mutation.target),1);
		for(var i = 0; i < mutation.addedNodes.length; i++) 
		{
			if(mutation.addedNodes[i].nodeName == "BASE")
			{
				if(base) document.head.removeChild(base);
				Array.prototype.forEach.call(pics, function(pic) {
					pic.src = pic.src;
				});
				Array.prototype.forEach.call(backgrounds, function(background) {
					background.setAttribute("background",background.getAttribute("background"));
				});
			}
			else if(mutation.addedNodes[i].nodeName == "SCRIPT" && mutation.addedNodes[i].innerHTML.includes("__wm.bt()"))
			{
				var toolbarScript = Array.prototype.find.call(mutation.addedNodes[i].parentNode.childNodes,node => node.innerHTML && node.innerHTML.includes("wbCurrentUrl") && node.innerHTML.includes("&amp;"));
				if(toolbarScript)
				{
					var replacement = document.createElement("script");
					replacement.type = toolbarScript.type;
					var currentURL = toolbarScript.innerHTML.match(/wbCurrentUrl = "(.+)";/)[1];
					replacement.innerHTML = toolbarScript.innerHTML.replace(currentURL,decodeHTML(currentURL));
					replacement.innerHTML = replacement.innerHTML.replace("bt:bootstrap}","bt:bootstrap,ff:\"balls\"}");
					toolbarScript.parentNode.replaceChild(replacement,toolbarScript);
				}
			}
			else checkMutations(mutation.addedNodes[i]);
		}
	});
});

function decodeHTML(text) {
    var textarea = document.createElement("textarea");
    textarea.innerHTML = text;
    return textarea.value;
}

function checkMutations(node)
{
	if(document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(node)) return;
	if(node.nodeType == 1)
	{
		if(node.nodeName == "IMG" && !node.src.includes("/web/") && !node.src.includes("data:"))
		{
			observer.disconnect();
			fixImage(node);
			observer.observe(document.body || document, config);
		}
		var bg = node.background || node.getAttribute("background");
		if(bg && !relativeToAbsolute(bg).includes(document.domain + "/web"))
		{
			observer.disconnect();
			changeBackground(node,fixURL(relativeToAbsolute(bg)));
			observer.observe(document.body || document, config);
		}
		if(node.nodeName == "A" && node.href.includes("/http://web.archive.org/web/")) node.href = node.href.substring(node.href.indexOf("/http://web.archive.org/web/") + 1);
	}
	checkedNodes.push(node);
	for(var n = 0; n < node.childNodes.length; n++)
	{
		if(!checkedNodes.includes(node.childNodes[n])) checkMutations(node.childNodes[n]);
	}
}

var config = { attributes: true, childList: true, subtree: true };
observer.observe(document.body || document, config);