Melvor Idle - Timestamped Saves

Adds useful default filenames to downloaded saves

当前为 2020-09-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
});