Melvor Idle - Timestamped Saves

Adds character name and a timestamp to the default filename when downloading a save

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Melvor Idle - Timestamped Saves
// @description Adds character name and a timestamp to the default filename when downloading a save
// @version     2.6
// @namespace   Visua
// @match       https://melvoridle.com/*
// @match       https://www.melvoridle.com/*
// @match       https://test.melvoridle.com/*
// @require     https://cdnjs.cloudflare.com/ajax/libs/pako/1.0.11/pako.min.js
// @grant       none
// ==/UserScript==
/* jshint esversion: 6 */

((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 replaceDownloadSave() {
        const _downloadSave = downloadSave;
        downloadSave = (backup = false) => {
            let saveString;
            try {
                if (!backup) {
                    saveString = getSave();
                } else {
                    saveString = backupSave;
                }
                const save = JSON.parse(pako.ungzip(atob(saveString), { 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 saveString !== 'undefined') {
                    console.error('Melvor Idle - Timestamped Saves: Save has unexpected format:', saveString);
                }
                _downloadSave();
                return;
            }

            const file = new Blob([saveString], { 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);
                }, 100);
            }
        };

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

    function loadScript() {
        if (typeof confirmedLoaded !== 'undefined' && confirmedLoaded && !currentlyCatchingUp) {
            clearInterval(interval);
            console.log('Loading Timestamped Saves');
            replaceDownloadSave();
            $('#header-user-options-dropdown .dropdown-divider').before('<a class="dropdown-item d-flex align-items-center justify-content-between pointer-enabled" onclick="downloadSave();"><span>Download Save</span></a>');
        }
    }

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