Wayback Machine Image Fixer

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

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

  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.1.0
  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 && target.parentNode.nodeName == "A")
  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. else target.src = JSON.parse(response.responseText)["archived_snapshots"]["closest"]["url"];
  39. }
  40. });
  41. }
  42.  
  43. function evaluateImage(pic)
  44. {
  45. GM_xmlhttpRequest({
  46. url: pic.src,
  47. method: "HEAD",
  48. onload: function(response) {
  49. //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
  50. if(response.responseHeaders.indexOf("Content-Type: text/html") > -1)
  51. {
  52. replaceImage(pic);
  53. }
  54. }
  55. });
  56. }
  57.  
  58. for(var i = 0; i < pics.length; i++)
  59. {
  60. //Skip over stuff related to the Wayback Machine toolbar
  61. if(isInToolbar(pics[i])) continue;
  62. evaluateImage(pics[i]);
  63. }
  64.  
  65. function isInToolbar(node,parent)
  66. {
  67. var baseNode = node;
  68. var found = false;
  69. while(baseNode != document)
  70. {
  71. baseNode = baseNode.parentNode;
  72. if(baseNode == document.getElementById("wm-ipp"))
  73. {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }

QingJ © 2025

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