Wayback Machine Autosave

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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();
})();