Wayback Machine Image Fixer

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

当前为 2015-12-30 提交的版本,查看 最新版本

  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. // @include https://web.archive.org/web/*
  7. // @exclude /\*/
  8. // @exclude *.jpg
  9. // @exclude *.jpeg
  10. // @exclude *.png
  11. // @exclude *.gif
  12. // @exclude *.bmp
  13. // @version 1.2.3
  14. // @grant GM_xmlhttpRequest
  15.  
  16. // ==/UserScript==
  17.  
  18. var pics = document.images;
  19.  
  20. function specialUses(pic)
  21. {
  22. var specialCases = [{domain:"northarc.com/images/unsorted/",replacements:[pic.src.replace("thumb.","tn_"),pic.src.replace("thumb.","")],maxDimensions:{width:100,height:80}},
  23. {domain:"*",replacements:["data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="],condition:pic.src.indexOf("spacer.gif") > -1 || pic.src.indexOf("blank.gif") > -1},
  24. {domain:"tinypic.com/",replacements:[pic.src.replace("/i","/oi")]}];
  25. var filteredCases = {};
  26. filteredCases.replacements = [];
  27. for(var c = 0; c < specialCases.length; c++)
  28. {
  29. var additionalCondition = specialCases[c].condition !== undefined ? specialCases[c].condition : true;
  30. if((pic.src.indexOf(specialCases[c].domain) > -1 || specialCases[c].domain == "*") && additionalCondition)
  31. {
  32. filteredCases.replacements = filteredCases.replacements.concat(specialCases[c].replacements);
  33. if(specialCases[c].maxDimensions) filteredCases.maxDimensions = specialCases[c].maxDimensions;
  34. }
  35. }
  36. return filteredCases;
  37. }
  38.  
  39. var timestamp = /web\/(\d{1,14})/.exec(window.location.href)[1];
  40.  
  41. function replaceImage(target, altURLs)
  42. {
  43. var possibleUses = specialUses(target);
  44. var URLGuesses = [];
  45. if(altURLs && altURLs.length > 0) URLGuesses = altURLs;
  46. else if(possibleUses.replacements.length > 0) URLGuesses = possibleUses.replacements;
  47. else URLGuesses = [target.src];
  48. var testSet = [];
  49. for(var p = 0; p < URLGuesses.length; p++)
  50. {
  51. if(URLGuesses[p].indexOf("http") > -1) testSet[p] = APITest(URLGuesses[p]);
  52. else if(URLGuesses[p].indexOf("data:") > -1)
  53. {
  54. testSet[p] = Promise.resolve(URLGuesses[p]);
  55. break;
  56. }
  57. }
  58. Promise.all(testSet).then(function(value) {
  59. for(var v = 0; v < value.length; v++)
  60. {
  61. if(value[v] !== null)
  62. {
  63. if(possibleUses.maxDimensions) switchWithResize(value[v], target, possibleUses.maxDimensions.width, possibleUses.maxDimensions.height);
  64. else target.src = value[v];
  65. return;
  66. }
  67. }
  68. //Try and "expose" image links that are unclickable due to the image not loading
  69. if(target.alt === "" && target.width === 0 && target.parentNode.nodeName === "A")
  70. {
  71. //Changing the source is pretty hacky, but it's the only way I can think of to turn "invisible" image links into something clickable
  72. target.src = target.src.substring(target.src.lastIndexOf("http"));
  73. target.width = 25;
  74. target.height = 25;
  75. }
  76. });
  77. function APITest(replacement)
  78. {
  79. var subPromise = new Promise(function(resolve,reject) {
  80. var originalURL = replacement.substring(replacement.lastIndexOf("http"));
  81. GM_xmlhttpRequest({
  82. url: "http://archive.org/wayback/available?url=" + originalURL + "&timestamp=" + timestamp,
  83. method: "GET",
  84. headers: {"Accept": "application/json"},
  85. onload: function(response) {
  86. if(response.status == 503) reject(response.statusText + " for " + originalURL);
  87. else if(JSON.parse(response.responseText).archived_snapshots.closest !== undefined) resolve(JSON.parse(response.responseText).archived_snapshots.closest.url);
  88. else resolve(null);
  89. }
  90. });
  91. }).catch(function(e) { return APITest(replacement); });
  92. return subPromise;
  93. }
  94. function switchWithResize(url, target, width, height)
  95. {
  96. var oldDimensions = {width:width, height: height};
  97. target.src = url;
  98. target.onload = function() {
  99. var aspectRatio = target.naturalWidth / target.naturalHeight;
  100. if(target.naturalWidth >= target.naturalHeight)
  101. {
  102. target.width = oldDimensions.width;
  103. target.height = oldDimensions.width / aspectRatio;
  104. }
  105. else if(target.naturalWidth <= target.naturalHeight)
  106. {
  107. target.height = oldDimensions.height;
  108. target.width = oldDimensions.height * aspectRatio;
  109. }
  110. };
  111. }
  112. }
  113.  
  114. function evaluateImage(pic)
  115. {
  116. GM_xmlhttpRequest({
  117. url: pic.src,
  118. method: "GET",
  119. onload: function(response) {
  120. //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
  121. if(response.responseHeaders.indexOf("Content-Type: text/html") > -1)
  122. {
  123. //This might be a case where if you were visit the image directly, you would be redirected elsewhere. This attempts to catch that and replace the pic's src with where it would take you.
  124. var doc = document.implementation.createHTMLDocument("Possible Replacement");
  125. doc.documentElement.innerHTML = response.responseText;
  126. if(doc.getElementsByClassName("impatient")[0])
  127. {
  128. GM_xmlhttpRequest({
  129. url: doc.getElementsByClassName("impatient")[0].firstChild.href,
  130. method: "HEAD",
  131. onload: function(response) {
  132. //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
  133. if(response.responseHeaders.indexOf("Content-Type: text/html") == -1)
  134. {
  135. replaceImage(pic,[doc.getElementsByClassName("impatient")[0].firstChild.href]);
  136. }
  137. else
  138. {
  139. replaceImage(pic);
  140. }
  141. }
  142. });
  143. }
  144. else if(response.status != 403)
  145. {
  146. replaceImage(pic);
  147. }
  148. }
  149. }
  150. });
  151. }
  152.  
  153. for(var i = 0; i < pics.length; i++)
  154. {
  155. //Skip over stuff related to the Wayback Machine toolbar and data URIs
  156. if((document.getElementById("wm-ipp") && document.getElementById("wm-ipp").contains(pics[i])) || pics[i].src.indexOf("data:") > -1) continue;
  157. if(pics[i].src.indexOf("ttp://") === 0) pics[i].src = "web.archive.org/web/" + timestamp + "/h" + pics[i].src;
  158. evaluateImage(pics[i]);
  159. }

QingJ © 2025

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