Wayback Machine Image Fixer

Attempts to fix broken images by replacing them with working timestamps based on JSON results

当前为 2015-10-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Wayback Machine Image Fixer
  3. // @namespace DoomTay
  4. // @description Attempts to fix broken images by replacing them with working timestamps based on JSON results
  5. // @include http://web.archive.org/web/*
  6. // @exclude http://web.archive.org/web/*.jpg
  7. // @exclude http://web.archive.org/web/*.jpeg
  8. // @exclude http://web.archive.org/web/*.png
  9. // @exclude http://web.archive.org/web/*.gif
  10. // @exclude http://web.archive.org/web/*.bmp
  11. // @version 1.0.0.20151004173702
  12. // @grant GM_xmlhttpRequest
  13.  
  14. // ==/UserScript==
  15.  
  16. var pics = document.images;
  17.  
  18. function replaceImage(target)
  19. {
  20. var originalURL = target.src.substring(target.src.lastIndexOf('http'));
  21. var newURL = GM_xmlhttpRequest({
  22. url: "http://archive.org/wayback/available?url=" + originalURL,
  23. method: "GET",
  24. headers: {"Accept": "application/json"},
  25. onload: function(response) {
  26. if(JSON.parse(response.responseText)["archived_snapshots"]["closest"] == undefined)
  27. {
  28. //Try and "expose" image links that are unclickable due to the image not loading
  29. if(target.alt == "" && target.width == 0)
  30. {
  31. //Changing the source is pretty hacky, but it's the only way I can think of to turn "invisible" image links into something clickable
  32. target.src = target.src.substring(target.src.lastIndexOf("http"));
  33. target.width = 25;
  34. target.height = 25;
  35. }
  36. return;
  37. }
  38. target.src = JSON.parse(response.responseText)["archived_snapshots"]["closest"]["url"];
  39. }
  40. });
  41. }
  42.  
  43. function isImageValid(url)
  44. {
  45. var lookup = GM_xmlhttpRequest({
  46. url: url,
  47. method: "HEAD",
  48. synchronous: true
  49. });
  50. //Going off of response code is unreliable. Sometimes an image will return a status code of 200 even though it would redirect to an error page should you view the image directly, so we're looking at content type instead
  51. return (lookup.responseHeaders.indexOf("Content-Type: text/html") == -1);
  52. }
  53.  
  54. for(var i = 0; i < pics.length; i++)
  55. {
  56. //Skip over stuff related to the Wayback Machine toolbar
  57. if(containsParent(pics[i],document.getElementById("wm-ipp"))) continue;
  58. //See if the image needs to be replaced in the first place
  59. if (isImageValid(pics[i].src)) continue;
  60. replaceImage(pics[i]);
  61. }
  62.  
  63. function containsParent(node,parent)
  64. {
  65. var baseNode = node;
  66. var found = false;
  67. while(baseNode != document)
  68. {
  69. baseNode = baseNode.parentNode;
  70. if(baseNode == parent)
  71. {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址