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-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Wayback Machine Small Bug Fixes
  3. // @namespace DoomTay
  4. // @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
  5. // @version 1.3.3
  6. // @include http://web.archive.org/web/*
  7. // @include http://wayback.archive.org/web/*
  8. // @include https://web.archive.org/web/*
  9. // @include https://wayback.archive.org/web/*
  10. // @run-at document-start
  11. // @exclude /\*/
  12. // @grant none
  13.  
  14. // ==/UserScript==
  15.  
  16. var lastFolder = window.location.href.substring(window.location.href.lastIndexOf("/") + 1);
  17. var pics = document.images;
  18. var backgrounds = document.querySelectorAll("[background]");
  19. var shouldHaveTrailingSlash = (window.location.href.lastIndexOf(".") < window.location.href.lastIndexOf("/") || window.location.href.substring(window.location.href.lastIndexOf("//") + 2) == lastFolder) && !lastFolder.includes("?");
  20. var hasTrailingSlash = window.location.href.endsWith("/");
  21. var domain = window.location.href.substring(0,window.location.href.indexOf("/",window.location.href.lastIndexOf("//") + 2));
  22. var isInDomain = window.location.href == domain || window.location.href == domain + "/";
  23. var timestamp = /web\/(\d{1,14})/.exec(window.location.href)[1];
  24.  
  25. if(!document.getElementsByTagName("base")[0])
  26. {
  27. var base = document.createElement("base");
  28. if(shouldHaveTrailingSlash && !hasTrailingSlash) base.href = window.location.href + "/";
  29. else if((!hasTrailingSlash && !shouldHaveTrailingSlash) || hasTrailingSlash) base.href = document.baseURI;
  30. else base.href = domain + "/";
  31. document.head.appendChild(base);
  32. }
  33.  
  34. function relativeToAbsolute(bgURL)
  35. {
  36. var img = new Image();
  37. img.src = bgURL;
  38. return img.src;
  39. }
  40.  
  41. function fixURL(URL)
  42. {
  43. var fixedURL;
  44. if(URL.includes("archive.org")) fixedURL = URL.substring(URL.indexOf(document.domain) + document.domain.length);
  45. if(URL.startsWith("/")&& !URL.startsWith("/web/")) fixedURL = domain + URL;
  46. else if (URL.startsWith("../") && isInDomain) fixedURL = URL.substring(3);
  47. else fixedURL = domain.substring(0,domain.indexOf("/http") + 1) + URL.substring(URL.indexOf("/http") + 1);
  48. return fixedURL;
  49. }
  50.  
  51. function fixImage(pic)
  52. {
  53. var oldSrc = pic.getAttribute("src");
  54. if(!oldSrc) return;
  55. pic.setAttribute("src",fixURL(oldSrc));
  56. }
  57.  
  58. function fixBackground(node)
  59. {
  60. if(node.background) node.background = fixURL(node.background);
  61. else if(node.getAttribute("background")) node.setAttribute("background",fixURL(node.getAttribute("background")));
  62. }
  63.  
  64. var observer = new MutationObserver(function(mutations) {
  65. mutations.forEach(function(mutation) {
  66. checkMutations(mutation.target);
  67. for(var i = 0; i < mutation.addedNodes.length; i++)
  68. {
  69. if(mutation.addedNodes[i].nodeName == "BASE")
  70. {
  71. if(base) document.head.removeChild(base);
  72. Array.prototype.forEach.call(pics, function(pic) {
  73. pic.src = pic.src;
  74. });
  75. Array.prototype.forEach.call(backgrounds, function(background) {
  76. background.setAttribute("background",background.getAttribute("background"));
  77. });
  78. }
  79. else if(mutation.addedNodes[i].nodeName == "SCRIPT" && mutation.addedNodes[i].innerHTML.includes("__wm.bt()"))
  80. {
  81. //As far as we're concerned, it's guaranteed that the script we're trying to find here is immediately two nodes before this one, but I'm not sure how else to go about finding it
  82. var toolbarScript = Array.prototype.find.call(document.scripts,node => node.innerHTML.includes("wbCurrentUrl") && node.innerHTML.includes("&amp;"));
  83. if(toolbarScript)
  84. {
  85. var replacement = document.createElement("script");
  86. replacement.type = toolbarScript.type;
  87. var currentURL = toolbarScript.innerHTML.match(/wbCurrentUrl = "(.+)";/)[1];
  88. replacement.innerHTML = toolbarScript.innerHTML.replace(currentURL,decodeHTML(currentURL));
  89. toolbarScript.parentNode.replaceChild(replacement,toolbarScript);
  90. }
  91. }
  92. else
  93. {
  94. checkMutations(mutation.addedNodes[i]);
  95. if(mutation.addedNodes[i].nodeType == 1)
  96. {
  97. var children = mutation.addedNodes[i].getElementsByTagName("*");
  98. for(var c = 0; c < children.length; c++)
  99. {
  100. checkMutations(children[c]);
  101. }
  102. }
  103. }
  104. }
  105. });
  106. });
  107.  
  108. function decodeHTML(text) {
  109. var textarea = document.createElement("textarea");
  110. textarea.innerHTML = text;
  111. return textarea.value;
  112. }
  113.  
  114. function checkMutations(node)
  115. {
  116. if(document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(node)) return;
  117. if(node.nodeType == 1)
  118. {
  119. if(node.nodeName == "IMG" && !node.src.includes("/web/") && !node.src.includes("data:"))
  120. {
  121. observer.disconnect();
  122. fixImage(node);
  123. observer.observe(document.body || document, config);
  124. }
  125. var bg = node.background || node.getAttribute("background");
  126. if(bg && !relativeToAbsolute(bg).includes(document.domain + "/web"))
  127. {
  128. observer.disconnect();
  129. fixBackground(node);
  130. observer.observe(document.body || document, config);
  131. }
  132. if((node.nodeName == "A" || node.nodeName == "AREA") && node.href)
  133. {
  134. if(node.href.includes("/http://web.archive.org/web/")) node.href = node.href.substring(node.href.indexOf("/http://web.archive.org/web/") + 1);
  135. else if(node.getAttribute("href").startsWith("/")) node.setAttribute("href",fixURL(node.getAttribute("href")));
  136. }
  137. }
  138. }
  139.  
  140. var config = { attributes: true, childList: true, subtree: true };
  141. observer.observe(document.body || document, config);