Internet Archive Saver

This userscript saves every visited page to the Internet Archive Wayback Machine (archive.org), if the page has not already been saved there within the last 30 days. This prevents duplicate saves (We don't want to spam the Internet Archive). Some sites are excluded by default (No need to archive google searches, for example). You can add more @exclude flags on your own.

目前為 2020-03-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Internet Archive Saver
// @description  This userscript saves every visited page to the Internet Archive Wayback Machine (archive.org), if the page has not already been saved there within the last 30 days. This prevents duplicate saves (We don't want to spam the Internet Archive). Some sites are excluded by default (No need to archive google searches, for example). You can add more @exclude flags on your own.
// @namespace    https://greasyfork.org/de/users/160920-flo-pinguin
// @author       FloPinguin
// @icon         https://abload.de/img/imageuqk3d.png
// @version      2.0
// @grant        GM_xmlhttpRequest
// @connect      archive.org
// @noframes

// @match        *://*/*

// @exclude      *startpage.com*
// @exclude      *google.*/search*
// @exclude      *ecosia.*/search*
// @exclude      *bing.*/search*
// @exclude      *yahoo.*/search*
// @exclude      *ask.com*
// @exclude      *duckduckgo.com*
// @exclude      *aol.*/search*
// @exclude      *yandex.*/search*
// @exclude      *baidu.*
// @exclude      *docs.google.*
// @exclude      *youtube.com*
// @exclude      *archive.*
// @exclude      http://localhost*

// ==/UserScript==



// ==ChangeLog==

// V1.0 - Initial version
// V1.1 - Small exclude optimizations
// V2.0 - Badges! The archiving status gets shown in the bottom-right corner of each visited page. A = Archived, FIRST = First archival, U = Archiving unneccessary, E = Archiving error. This can be turned off in the settings. Version 2.0 also features more detailed console output.

// ==/ChangeLog==



(function() {
    /* SETTINGS */
    var SHOW_BADGES = true;



    console.log("[IA Saver] Asking for archiving necessity...")
    GM_xmlhttpRequest({
        method: 'GET',
        url: 'https://archive.org/wayback/available?url=' + encodeURIComponent(location.href),
        onload: function(data){
            data = JSON.parse(data.responseText);

            if (isEmpty(data.archived_snapshots)){
                archive(location.href, true);
            }else{
                var last_save = timestampConvert(data.archived_snapshots.closest.timestamp);
                if (Date.now() - last_save > 2592000000){
                    archive(location.href, false);
                }else{
                    var log = "[IA Saver] Archiving unnecessary, latest save: " + new Date(last_save).toString() + " (" + data.archived_snapshots.closest.timestamp + ")";
                    console.log(log);
                    showBadge("U", "darkorange", log)
                }
            }
        }
    });

    function archive(url, first){
        console.log("[IA Saver] Archiving...")
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://web.archive.org/save/' + url,
            onload: function(data){
                var log;
                if (data.status == 200){
                    log = "[IA Saver] " + (first ? "FIRST ARCHIVAL!" : "Archived!") + " (https://web.archive.org/web/" + url + ")";
                    console.log(log);
                    showBadge(first ? "FIRST" : "A", "green", log)
                }else{
                    log = "[IA Saver] Archiving error (" + data.status + " - " + data.statusText + ")";
                    console.error(log);
                    showBadge("E", "#ff2e2e", log)
				}
            }
        });
    }

    function showBadge(status, color, title){
        var el = document.createElement("div");
        el.setAttribute("title", title)
        el.setAttribute("style", "position: fixed; bottom: 0; right: 0; background: " + color + "; color: #fff; padding: 3px 5px; font-size: 12.5px; border-top-left-radius: 5px; z-index: 1000000");
        el.innerHTML = status;
        var body = document.getElementsByTagName("body")[0];
        body.insertBefore(el, body.firstChild);
    }

    function timestampConvert(ts){
        return Date.parse(ts.replace(
            /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/,
            '$4:$5:$6 $2/$3/$1 GMT'
        ));
    }

    function isEmpty(obj) {
        return Object.keys(obj).length === 0;
    }
})();