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

As of 2016-02-15. See the latest version.

  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.2.8
  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 toolbarNav = document.getElementById("wm-graph-anchor");
  17. var lastFolder = window.location.href.substring(window.location.href.lastIndexOf("/") + 1);
  18. var pics = document.images;
  19. var backgrounds = document.querySelectorAll("[background]");
  20. var shouldHaveTrailingSlash = (window.location.href.lastIndexOf(".") < window.location.href.lastIndexOf("/") || window.location.href.substring(window.location.href.lastIndexOf("//") + 2) == lastFolder) && lastFolder.indexOf("?") == -1;
  21. var hasTrailingSlash = window.location.href.lastIndexOf("/") == window.location.href.length - 1;
  22. var domain = window.location.href.substring(0,window.location.href.indexOf("/",window.location.href.lastIndexOf("//") + 2));
  23.  
  24. function fixToolbar()
  25. {
  26. while(toolbarNav.href.indexOf("&amp;") > -1) toolbarNav.href = toolbarNav.href.replace("&amp;","&");
  27. }
  28.  
  29. //Fix cases of &amp; in the capture graph
  30. if(toolbarNav) fixToolbar();
  31.  
  32. if(!document.getElementsByTagName("base")[0])
  33. {
  34. var base = document.createElement("base");
  35. if(shouldHaveTrailingSlash && !hasTrailingSlash) base.href = window.location.href + "/";
  36. else if((!hasTrailingSlash && !shouldHaveTrailingSlash) || hasTrailingSlash) base.href = document.baseURI;
  37. else base.href = domain + "/";
  38. document.head.appendChild(base);
  39. }
  40.  
  41. for(var i = 0; i < pics.length; i++)
  42. {
  43. //Skip over stuff related to the Wayback Machine toolbar and data URIs
  44. if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(pics[i])) || pics[i].src.indexOf("data:") > -1) continue;
  45. //Refresh images in case the "base url" had to be modified.
  46. pics[i].src = pics[i].src;
  47. //For whatever reason, some images will point to within Internet Archive's "main" servers, instead of the crawled site. This attempts to fix that.
  48. if(pics[i].src.indexOf(document.domain + "/web") == -1) pics[i].src = fixURL(pics[i].src);
  49. }
  50.  
  51. for(var b = 0; b < backgrounds.length; b++)
  52. {
  53. var bg = backgrounds[b].background || backgrounds[b].getAttribute("background");
  54. //Skip over stuff related to the Wayback Machine toolbar and data URIs
  55. if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(backgrounds[b])) || bg.indexOf("data:") > -1) continue;
  56. //Refresh images in case the "base url" had to be modified.
  57. changeBackground(backgrounds[b],bg);
  58. //For whatever reason, some images will point to within Internet Archive's "main" servers, instead of the crawled site. This attempts to fix that.
  59. if(relativeToAbsolute(bg).indexOf(document.domain + "/web") == -1)
  60. {
  61. var absoluteBG = relativeToAbsolute(bg)
  62. changeBackground(backgrounds[b],fixURL(absoluteBG));
  63. }
  64. }
  65.  
  66. function relativeToAbsolute(bgURL)
  67. {
  68. var img = new Image();
  69. img.src = bgURL;
  70. return img.src;
  71. }
  72.  
  73. function fixURL(URL)
  74. {
  75. if(URL.indexOf(document.domain) > -1) return domain + URL.substring(URL.indexOf("/",URL.lastIndexOf("//") + 2));
  76. else return domain.substring(0,domain.indexOf("/http") + 1) + URL.substring(URL.indexOf("/http") + 1);
  77. }
  78.  
  79. function changeBackground(node, newBackground)
  80. {
  81. if(node.background) node.background = newBackground;
  82. else if(node.getAttribute("background")) node.setAttribute("background",newBackground);
  83. }
  84.  
  85. var observer = new MutationObserver(function(mutations) {
  86. mutations.forEach(function(mutation) {
  87. if(mutation.target.id == "wm-graph-anchor") toolbarNav = mutation.target;
  88. if(mutation.type == "attributes" && mutation.target == toolbarNav) fixToolbar();
  89. if(mutation.target.nodeType == 1) checkMutations(mutation.target);
  90. for(var i = 0; i < mutation.addedNodes.length; i++)
  91. {
  92. if(mutation.addedNodes[i].nodeType == 1) checkMutations(mutation.addedNodes[i]);
  93. }
  94. });
  95. });
  96.  
  97. function checkMutations(node)
  98. {
  99. if(document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(node)) return;
  100. if(node.nodeName == "IMG" && node.src.indexOf(document.domain + "/web") == -1)
  101. {
  102. node.src = fixURL(node.src);
  103. }
  104. if((node.getAttribute("background") || node.background) && (node.getAttribute("background") || node.background).indexOf(document.domain + "/web") == -1)
  105. {
  106. var bg = node.background || node.getAttribute("background");
  107. var absoluteBG = relativeToAbsolute(bg);
  108. changeBackground(node,fixURL(absoluteBG));
  109. }
  110. for(var n = 0; n < node.childNodes.length; n++)
  111. {
  112. if(node.childNodes[n].nodeType == 1) checkMutations(node.childNodes[n]);
  113. }
  114. }
  115.  
  116. var config = { attributes: true, childList: true, characterData: true, subtree: true };
  117. observer.observe(document.body || document, config);