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 yourself.

2020-03-01 기준 버전입니다. 최신 버전을 확인하세요.

// ==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 yourself.
// @namespace    https://greasyfork.org/de/users/160920-flo-pinguin
// @author       FloPinguin
// @icon         https://abload.de/img/imageuqk3d.png
// @version      1.1
// @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==

(function() {
    globalUrl = location.href;

    GM_xmlhttpRequest({
        method: 'GET',
        url: 'https://archive.org/wayback/available?url=' + encodeURIComponent(globalUrl),
        onload: function(data){
            data = JSON.parse(data.responseText);

            if (isEmpty(data.archived_snapshots)){
                archive(globalUrl);
            }else{
                last_save = timestampConvert(data.archived_snapshots.closest.timestamp);
                if (Date.now() - last_save > 2592000000){
                    archive(globalUrl);
                }else{
                    console.log("Archiving not necessary, latest save: " + new Date(last_save).toString() + " (" + data.archived_snapshots.closest.timestamp + ")");
                }
            }
        }
    });

    function archive(url){
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://web.archive.org/save/' + url,
            onload: function(data){
	        if (data.status == 200){
		    console.log("Archived!");
		}else{
		    console.error("Archiving error (" + data.status + " - " + data.statusText + ")");
		}
            }
        });
    }

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