BA Planner Enhancer

Add some desirable functionality to BA Resource Planner

当前为 2023-07-15 提交的版本,查看 最新版本

// ==UserScript==
// @name        BA Planner Enhancer
// @author      Dia
// @description Add some desirable functionality to BA Resource Planner
// @version     0.0.1
// @icon        https://i.imgur.com/oA2E4CJ.png
// @grant       none
// @match       https://justin163.com/planner/
// @namespace   wxw.moe/@dia
// @run-at      document-idle
// @license     Unlicense
// ==/UserScript==

const MINIMUM_AUTOSAVE = 130000; //minimum required delay for autosave, do not change this
const DEBUG_MODE_AUTOSAVE = 30000; //30 sec autosave in debug mode (for debug only; does not actually save anything to cloud)
const DEBUG_MODE = false; //set to true to debug
const AUTOSAVE_PERIOD_OFFSET = 170000; //millisec delay to autosave to cloud so we don't overwhelm the api- this is added to MINIMUM_AUTOSAVE

let lastSaveExport = localStorage.getItem('save-data');
let autosaveFreq = MINIMUM_AUTOSAVE + AUTOSAVE_PERIOD_OFFSET;

function exec(fn) {
    let script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = '(' + fn + ')();';
    document.body.appendChild(script); // run the script
    document.body.removeChild(script); // clean up
}

function log(msg) {
    let currTime = new Date();
    let currH = String(currTime.getHours()).padStart(2, 0);
    let currM = String(currTime.getMinutes()).padStart(2, 0);
    let currS = String(currTime.getSeconds()).padStart(2, 0);
    console.log("[BAPE-" + currH + ":" + currM + ":" + currS + "] " + msg);
}

function autoSave() {
    let xferUsername = document.getElementById("input-transfer-username").value;
    let xferAuthkey = document.getElementById("input-transfer-authkey").value;
    let currTime = new Date();
    let currSaveExport = localStorage.getItem('save-data');
    log("Autosave fired");
    if (xferUsername == "" || xferAuthkey == "") {
        log("Cannot autosave to cloud because there's no username and/or authkey found - please login first")
    } else {
        if (lastSaveExport === currSaveExport) {
            log("There are no changes to save. Skipping this round of autosave...");
        } else {
            log("Changes detected. Saving changes...");
            lastSaveExport = currSaveExport;
            if (DEBUG_MODE) {
                console.log("Saved to cloud (fake)");
            } else {
                exec(function() {
                    saveRequest(false);
                });
            }
        }
    }
}

function autoSaveToCloud(autosaveFreq) {
    setInterval(function() {
        autoSave();
    }, autosaveFreq);
}

function loadImprovement() {
    let loadBtn = document.getElementById("transfer-load-button");
    loadBtn.onclick = ";";
    loadBtn.addEventListener("click", confirmLoad);
}

function confirmLoad() {
    if (confirm("Do you really want to load? This will overwrite any unsaved data!")) {
        exec(function() {
            return loadClick();
        });
    }
}

log("Document has finished loading. Starting the userscript...");
if (DEBUG_MODE) {
    autosaveFreq = DEBUG_MODE_AUTOSAVE;
    log("Warning! This script is currently run with debug mode on. The autosave functionality does not actually save anything to cloud in this mode.")
    log("You can turn off the debug mode by setting DEBUG_MODE to false in the script and refresh the page!")
}
log("Autosave duration is currently every " + (autosaveFreq / 1000) + " seconds.")

//feature #1: ask first if you really want to load from cloud, preventing accidental click that wipes local data
loadImprovement();
//feature #2: autosave every 5 minute (will not save if there are no changes)
autoSaveToCloud(autosaveFreq);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址