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

07.06.2016 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

You will need to install an extension such as Tampermonkey to install this script.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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);