Greasy Fork is available in English.

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-01-10 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  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.3
  6. // @include http://web.archive.org/web/*
  7. // @include https://web.archive.org/web/*
  8. // @exclude /\*/
  9. // @grant none
  10.  
  11. // ==/UserScript==
  12.  
  13. var toolbarNav = document.getElementById("wm-graph-anchor");
  14. var lastFolder = window.location.href.substring(window.location.href.lastIndexOf("/") + 1);
  15. var pics = document.images;
  16. var backgrounds = document.querySelectorAll("[background]");
  17. var shouldHaveTrailingSlash = window.location.href.lastIndexOf(".") < window.location.href.lastIndexOf("/") || window.location.href.substring(window.location.href.lastIndexOf("//") + 2) == lastFolder;
  18. var hasTrailingSlash = window.location.href.lastIndexOf("/") == window.location.href.length - 1;
  19. var domain = window.location.href.substring(0,window.location.href.indexOf("/",window.location.href.lastIndexOf("//") + 2));
  20.  
  21. function fixToolbar()
  22. {
  23. while(toolbarNav.href.indexOf("&amp;") > -1) toolbarNav.href = toolbarNav.href.replace("&amp;","&");
  24. }
  25.  
  26. //Fix cases of &amp; in the capture graph
  27. if(toolbarNav) fixToolbar();
  28.  
  29. if(!document.getElementsByTagName("base")[0])
  30. {
  31. var base = document.createElement("base");
  32. if(shouldHaveTrailingSlash && !hasTrailingSlash) base.href = window.location.href + "/";
  33. else if((!hasTrailingSlash && !shouldHaveTrailingSlash) || hasTrailingSlash) base.href = window.location.href;
  34. else base.href = domain + "/";
  35. document.head.appendChild(base);
  36. }
  37.  
  38. for(var i = 0; i < pics.length; i++)
  39. {
  40. //Skip over stuff related to the Wayback Machine toolbar and data URIs
  41. if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(pics[i])) || pics[i].src.indexOf("data:") > -1) continue;
  42. //Refresh images in case the "base url" had to be modified.
  43. pics[i].src = pics[i].src;
  44. //For whatever reason, some images will point to within Internet Archive's "main" servers, instead of the crawled site. This attempts to fix that.
  45. if(pics[i].src.indexOf("http://web.archive.org/web") == -1) pics[i].src = domain + pics[i].src.substring(pics[i].src.indexOf("/",pics[i].src.lastIndexOf("//") + 2));
  46. }
  47.  
  48. for(var b = 0; b < backgrounds.length; b++)
  49. {
  50. var bg = backgrounds[b].background || backgrounds[b].getAttribute("background");
  51. //Skip over stuff related to the Wayback Machine toolbar and data URIs
  52. if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(backgrounds[b])) || bg.indexOf("data:") > -1) continue;
  53. //Refresh images in case the "base url" had to be modified.
  54. changeBackground(backgrounds[b],bg);
  55. //For whatever reason, some images will point to within Internet Archive's "main" servers, instead of the crawled site. This attempts to fix that.
  56. if(relativeToAbsolute(bg).indexOf("http://web.archive.org/web") == -1)
  57. {
  58. var absoluteBG = relativeToAbsolute(bg)
  59. changeBackground(backgrounds[b],domain + absoluteBG.substring(absoluteBG.indexOf("/",absoluteBG.lastIndexOf("//") + 2)));
  60. }
  61. }
  62.  
  63. function relativeToAbsolute(bgURL)
  64. {
  65. var img = new Image();
  66. img.src = bgURL;
  67. return img.src;
  68. }
  69.  
  70. function changeBackground(node, newBackground)
  71. {
  72. if(node.background) node.background = newBackground;
  73. else if(backgrounds[b].getAttribute("background")) backgrounds[b].setAttribute("background",newBackground);
  74. }
  75.  
  76. var observer = new MutationObserver(function(mutations) {
  77. mutations.forEach(function(mutation) {
  78. if(mutation.type == "attributes" && mutation.target == toolbarNav) fixToolbar();
  79. if(mutation.attributeName == "src" && mutation.target.src.indexOf("http://web.archive.org/web") == -1)
  80. {
  81. mutation.target.src = domain + mutation.target.src.substring(mutation.target.src.indexOf("/",mutation.target.src.lastIndexOf("//") + 2));
  82. }
  83. if(mutation.attributeName == "background" && (mutation.target.getAttribute("background") || mutation.target.background).indexOf("http://web.archive.org/web") == -1)
  84. {
  85. var bg = mutation.target.background || mutation.target.getAttribute("background");
  86. var absoluteBG = relativeToAbsolute(bg);
  87. changeBackground(mutation.target,domain + absoluteBG.substring(absoluteBG.indexOf("/",absoluteBG.lastIndexOf("//") + 2)));
  88. }
  89. });
  90. });
  91.  
  92. var config = { attributes: true, childList: true, characterData: true, subtree: true };
  93. observer.observe(document.body, config);