Wayback Machine Autosave

Automatically saves pages as you browse, if no copy already exists

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Wayback Machine Autosave
// @namespace    https://thannymack.com
// @version      2025-06-25
// @description  Automatically saves pages as you browse, if no copy already exists
// @license      GNU GPLv3 
// @author       TM
// @match        http://*/*
// @match        https://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=web.archive.org
// @grant        GM.xmlHttpRequest
// @run-at       document-end
// @connect      web.archive.org
// @connect      wayback.archive.org
// ==/UserScript==

(function () {
    "use strict";

    const gmFetch = ({ url, headers = {} }) => {
        return new Promise((resolve, reject) => {
            GM.xmlHttpRequest({
                method: "GET",
                url,
                headers,
                onload: e => resolve(e.response),
                onerror: reject,
                ontimeout: reject,
                responseType: "text"
            });
        });
    };

    async function checkEligibilityForSaving(currentUrl){

        const response = await gmFetch({
            url: `https://web.archive.org/__wb/sparkline?output=json&url=${encodeURI(currentUrl)}`,
            headers: { referer: "https://web.archive.org" },
        });

        try {
            const jsonResponse = JSON.parse(response);
            console.log({waybackMachineEligibilityResponse: jsonResponse});
            if (jsonResponse?.last_ts === null && jsonResponse?.is_live === true){
                return true;
            }
        }
        catch{
            console.log(`Error checking eligibility for saving ${currentUrl} to wayback machine. The page has not been saved.`);
            return false;
        }

    }

    async function saveUrlInWaybackMachine(currentUrl){

        console.log(`Saving ${currentUrl} to wayback machine...`);
        const response = await gmFetch({
            url: `https://wayback.archive.org/save/${encodeURI(currentUrl)}`
        });
        // console.log(response);
        console.log(`Saved ${currentUrl} to wayback machine.`);
    }

    async function start() {
        const currentUrl = document.URL;
        const eligableForSaving = await checkEligibilityForSaving(currentUrl);
        if (eligableForSaving === true){
            saveUrlInWaybackMachine(currentUrl);
        }
        else{
            console.log(`${currentUrl} has already been saved in the wayback machine, or the wayback machine has found it to be offline.`);
        }
    }

    start();
})();