Wayback Count Display

Show Wayback count on page

  1. // ==UserScript==
  2. // @name Wayback Count Display
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Show Wayback count on page
  6. // @author You
  7. // @match *://*/*
  8. // @grant GM_xmlhttpRequest
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to format large numbers as k, m, etc.
  16. const formatNumber = (num) => {
  17. if (num >= 1000000) {
  18. return (num / 1000000).toFixed(1) + 'M';
  19. } else if (num >= 1000) {
  20. return (num / 1000).toFixed(1) + 'K';
  21. }
  22. return num;
  23. };
  24.  
  25. // Function to display count on page
  26. const displayCount = (count) => {
  27. const formattedCount = formatNumber(count);
  28. const div = document.createElement('div');
  29. div.style.position = 'fixed';
  30. div.style.top = '0';
  31. div.style.left = '50%';
  32. div.style.transform = 'translateX(-50%)';
  33.  
  34. div.style.zIndex = '9999';
  35. div.style.backgroundColor = 'white';
  36. div.style.padding = '5px';
  37. div.textContent = `${formattedCount}`;
  38. document.body.appendChild(div);
  39. };
  40.  
  41. // Get the current URL and encode it
  42. const currentURL = window.location.href;
  43. const encodedURL = encodeURIComponent(currentURL);
  44.  
  45. // Wayback API URL
  46. const apiURL = `http://web.archive.org/cdx/search/cdx?url=${encodedURL}&output=json&fl=original`;
  47.  
  48. // Make API call
  49. GM_xmlhttpRequest({
  50. method: 'GET',
  51. url: apiURL,
  52. onload: function(response) {
  53. if (response.status === 200) {
  54. try {
  55. const results = JSON.parse(response.responseText);
  56. const count = results.length ? results.length - 1 : 0; // Exclude header row
  57. displayCount(count);
  58. } catch(e) {
  59. displayCount('Parse Error');
  60. }
  61. } else {
  62. displayCount('Error');
  63. }
  64. },
  65. onerror: function() {
  66. displayCount('Error');
  67. }
  68. });
  69.  
  70. })();