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

当前为 2016-01-03 提交的版本,查看 最新版本

  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
  5. // @version 1.1.5
  6. // @include http://web.archive.org/web/*
  7. // @include https://web.archive.org/web/*
  8. // @grant none
  9.  
  10. // ==/UserScript==
  11.  
  12. var toolbarNav = document.getElementById("wm-graph-anchor");
  13. var pics = document.images;
  14. var lastFolder = window.location.href.substring(window.location.href.lastIndexOf("/") + 1);
  15. var shouldHaveTrailingSlash = window.location.href.lastIndexOf(".") < window.location.href.lastIndexOf("/") || window.location.href.substring(window.location.href.lastIndexOf("//") + 2) == lastFolder;
  16. var hasTrailingSlash = window.location.href.lastIndexOf("/") == window.location.href.length - 1;
  17.  
  18. function fixToolbar()
  19. {
  20. while(toolbarNav.href.indexOf("&amp;") > -1) toolbarNav.href = toolbarNav.href.replace("&amp;","&");
  21. }
  22.  
  23. //Fix cases of &amp; in the capture graph
  24. if(toolbarNav) fixToolbar();
  25.  
  26. if(shouldHaveTrailingSlash && !hasTrailingSlash)
  27. {
  28. if(!document.getElementsByTagName("base")[0])
  29. {
  30. var base = document.createElement("base");
  31. base.href = window.location.href + "/";
  32. document.head.appendChild(base);
  33. }
  34. for(var i = 0; i < pics.length; i++)
  35. {
  36. //Skip over stuff related to the Wayback Machine toolbar and data URIs
  37. if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(pics[i])) || pics[i].src.indexOf("data:") > -1) continue;
  38. //It might be an offsite or absolute reference, which means we don't need to do anything
  39. //Weird hack to make sure the "revised" images will actually display
  40. pics[i].src = pics[i].src;
  41. }
  42. }
  43.  
  44. var observer = new MutationObserver(function(mutations) {
  45. mutations.forEach(function(mutation) {
  46. if(mutation.type == "attributes" && mutation.target == toolbarNav) fixToolbar();
  47. });
  48. });
  49.  
  50. var config = { attributes: true, childList: true, characterData: true, subtree: true, attributeFilter: ["href"] };
  51. observer.observe(toolbarNav, config);