Wayback Machine Image Fixer

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

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

  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.3
  12. // @grant GM_xmlhttpRequest
  13.  
  14. // ==/UserScript==
  15.  
  16. var pics = document.images;
  17.  
  18. var timestamp = /web\/(\d{1,14})/.exec(window.location.href);
  19.  
  20. function replaceImage(target)
  21. {
  22. var originalURL = target.src.substring(target.src.lastIndexOf("http"));
  23. var newURL = GM_xmlhttpRequest({
  24. url: "http://archive.org/wayback/available?url=" + originalURL + "&closest=" + timestamp,
  25. method: "GET",
  26. headers: {"Accept": "application/json"},
  27. onload: function(response) {
  28. if (response.readyState == 4) {
  29. if(response.status == 503)
  30. {
  31. //We caught a 503, which means that the API wasn't "ready" for us yet. Let's try again
  32. replaceImage(target)
  33. }
  34. else if(JSON.parse(response.responseText).archived_snapshots.closest == null)
  35. {
  36. //Try and "expose" image links that are unclickable due to the image not loading
  37. if(target.alt == "" && target.width == 0 && target.parentNode.nodeName == "A")
  38. {
  39. //Changing the source is pretty hacky, but it's the only way I can think of to turn "invisible" image links into something clickable
  40. target.src = target.src.substring(target.src.lastIndexOf("http"));
  41. target.width = 25;
  42. target.height = 25;
  43. }
  44. return;
  45. }
  46. else target.src = JSON.parse(response.responseText)archived_snapshots.closest.url;
  47. }
  48. }
  49. });
  50. }
  51.  
  52. function evaluateImage(pic)
  53. {
  54. GM_xmlhttpRequest({
  55. url: pic.src,
  56. method: "HEAD",
  57. onload: function(response) {
  58. //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
  59. if(response.responseHeaders.indexOf("Content-Type: text/html") > -1)
  60. {
  61. replaceImage(pic);
  62. }
  63. }
  64. });
  65. }
  66.  
  67. for(var i = 0; i < pics.length; i++)
  68. {
  69. //Skip over stuff related to the Wayback Machine toolbar and data URIs
  70. if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(pics[i])) || pics[i].src.indexOf("data:") > -1) continue;
  71. evaluateImage(pics[i]);
  72. }

QingJ © 2025

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