Melvor Idle - Timestamped Saves

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

Ajankohdalta 21.10.2020. Katso uusin versio.

// ==UserScript==
// @name        Melvor Idle - Timestamped Saves
// @description Adds character name and a timestamp to the default filename when downloading a save
// @version     2.3
// @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 */

((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 (typeof confirmedLoaded !== 'undefined' && confirmedLoaded && !currentlyCatchingUp) {
            clearInterval(interval);
            console.log('Timestamped Saves loaded');

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