Wayback Count Display

Show Wayback count on page

2023-10-07 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Wayback Count Display
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Show Wayback count on page
// @author       You
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to format large numbers as k, m, etc.
    const formatNumber = (num) => {
        if (num >= 1000000) {
            return (num / 1000000).toFixed(1) + 'M';
        } else if (num >= 1000) {
            return (num / 1000).toFixed(1) + 'K';
        }
        return num;
    };

    // Function to display count on page
    const displayCount = (count) => {
        const formattedCount = formatNumber(count);
        const div = document.createElement('div');
        div.style.position = 'fixed';
        div.style.top = '0';
        div.style.left = '50%';
        div.style.transform = 'translateX(-50%)';

        div.style.zIndex = '9999';
        div.style.backgroundColor = 'white';
        div.style.padding = '5px';
        div.textContent = `${formattedCount}`;
        document.body.appendChild(div);
    };

    // Get the current URL and encode it
    const currentURL = window.location.href;
    const encodedURL = encodeURIComponent(currentURL);

    // Wayback API URL
    const apiURL = `http://web.archive.org/cdx/search/cdx?url=${encodedURL}&output=json&fl=original`;

    // Make API call
    GM_xmlhttpRequest({
        method: 'GET',
        url: apiURL,
        onload: function(response) {
            if (response.status === 200) {
                try {
                    const results = JSON.parse(response.responseText);
                    const count = results.length ? results.length - 1 : 0; // Exclude header row
                    displayCount(count);
                } catch(e) {
                    displayCount('Parse Error');
                }
            } else {
                displayCount('Error');
            }
        },
        onerror: function() {
            displayCount('Error');
        }
    });

})();