Wayback Count Display

Show Wayback count on page

Tính đến 07-10-2023. Xem phiên bản mới nhất.

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

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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');
        }
    });

})();