Melvor Idle - Timestamped Saves

Adds useful default filenames to downloaded saves

目前為 2020-09-25 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Melvor Idle - Timestamped Saves
// @description Adds useful default filenames to downloaded saves
// @version     2.0
// @namespace   Visua
// @match       https://melvoridle.com/*
// @match       https://www.melvoridle.com/*
// @require     https://cdnjs.cloudflare.com/ajax/libs/pako/1.0.11/pako.min.js
// @grant       none
// ==/UserScript==
/* jshint esversion: 6 */

(function (main) {
    var script = document.createElement('script');
    script.textContent = `try { (${main})(); } catch (e) { console.log(e); }`;
    document.body.appendChild(script).parentNode.removeChild(script);
})(() => {
    'use strict';

    function loadScript() {
        if (window.isLoaded) {
            clearInterval(interval);

            window.downloadSave = function (backup = false) {
                let exportSaved;
                try {
                    if (!backup) exportSaved = getSave();
                    else exportSaved = backupSave;

                    const save = JSON.parse(pako.ungzip(atob(exportSaved), { to: 'string' }));
                    if (save.username !== username) {
                        throw new Error('Save might not contain any data');
                    }
                } catch (e) {
                    console.error('Melvor Idle - Timestamped Saves:', e);
                    if (typeof exportSaved !== 'undefined') {
                        console.error('Melvor Idle - Timestamped Saves: Save has unexpected format:', exportSaved);
                    }
                    return;
                }

                const file = new Blob([exportSaved], { type: 'text/plain' });
                const filename = `melvoridlesave - ${username} - ${timestamp()}.txt`;
                if (window.navigator.msSaveOrOpenBlob) window.navigator.msSaveOrOpenBlob(file, filename); // IE10+
                else {
                    const a = document.createElement('a');
                    const url = URL.createObjectURL(file);
                    a.href = url;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.click();
                    setTimeout(function () {
                        document.body.removeChild(a);
                        window.URL.revokeObjectURL(url);
                    }, 0);
                }
            };
        }
    }

    function timestamp() {
        const date = new Date();
        return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} `
            + `${pad(date.getHours())}-${pad(date.getMinutes())}-${pad(date.getSeconds())}`;
    }

    function pad(n) {
        return n < 10 ? `0${n}` : n;
    }

    const interval = setInterval(loadScript, 200);
});