Wayback Machine Favicon Fixer

Attempts to add a favicon to a site crawled by the Wayback Machine in the event one does not come up normally

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

  1. // ==UserScript==
  2. // @name Wayback Machine Favicon Fixer
  3. // @namespace DoomTay
  4. // @description Attempts to add a favicon to a site crawled by the Wayback Machine in the event one does not come up normally
  5. // @version 1.0.0
  6. // @include http://web.archive.org/web/*
  7. // @include http://wayback.archive.org/web/*
  8. // @include https://web.archive.org/web/*
  9. // @include https://wayback.archive.org/web/*
  10. // @exclude /\*/
  11. // @grant GM_xmlhttpRequest
  12.  
  13. // ==/UserScript==
  14.  
  15. var domain = window.location.href.substring(0,window.location.href.indexOf("/",window.location.href.lastIndexOf("//") + 2));
  16. var timestamp = /web\/(\d{1,14})/.exec(window.location.href)[1];
  17.  
  18. var originalDomain = domain.substring(domain.lastIndexOf("http"));
  19. if(!document.querySelector("link[rel~='icon']")) retrieveFavicon(window.location.href.substring(window.location.href.lastIndexOf("/http") + 1,window.location.href.lastIndexOf("/") + 1));
  20.  
  21. function retrieveFavicon(base)
  22. {
  23. GM_xmlhttpRequest({
  24. url: "http://archive.org/wayback/available?url=" + base + "favicon.ico" + "&timestamp=" + timestamp,
  25. method: "GET",
  26. headers: {"Accept": "application/json"},
  27. onload: function(response) {
  28. if(response.status == 503) retrieveFavicon(base);
  29. else if(JSON.parse(response.responseText).archived_snapshots.closest)
  30. {
  31. var newFavicon = document.createElement("link");
  32. newFavicon.type = "image/x-icon";
  33. newFavicon.rel = "shortcut icon";
  34. newFavicon.href = JSON.parse(response.responseText).archived_snapshots.closest.url;
  35. document.head.appendChild(newFavicon);
  36. }
  37. else
  38. {
  39. var furtherTrim = base.substring(0,base.lastIndexOf("/",base.lastIndexOf("/") - 1) + 1);
  40. if(furtherTrim.indexOf("//") != (furtherTrim.length - 2)) retrieveFavicon(furtherTrim);
  41. }
  42. }
  43. });
  44. }