Nexus Clash Speedy Safe (B4)

Remembers your selections in the faction safe/footlocker dropdowns

当前为 2020-04-01 提交的版本,查看 最新版本

// ==UserScript==
// @name         Nexus Clash Speedy Safe (B4)
// @namespace    https://roadha.us
// @version      1.0
// @description  Remembers your selections in the faction safe/footlocker dropdowns
// @author       haliphax
// @match        https://nexusclash.com/modules.php?name=Game*
// @match        https://www.nexusclash.com/modules.php?name=Game*
// ==/UserScript==

(function() {
    'use strict';

    var saved = JSON.parse(GM_getValue('saved', '[]')),
        forms = document.querySelectorAll('form'),
        primer = (saved.length === 0);

    // save selected dropdown index on submit
    function onSubmit(idx, e) {
        var options = this.querySelectorAll('select[name="item"] option');

        for (var j = 0; j < options.length; j++) {
            var o = options[j];

            if (!o.selected) continue;

            saved[idx] = j;
            break;
        }

        GM_setValue('saved', JSON.stringify(saved));
    }

    var idx = 0;

    for (var i = 0; i < forms.length; i++) {
        var f = forms[i];

        if (!/^footlockergrab|safestock$/.exec(f.name)) continue;

        // add onsubmit event handler for each safe/footlocker button
        f.addEventListener('submit', onSubmit.bind(f, idx));

        if (primer) {
            // first run, prime data
            saved.push(null);
            idx++;
            continue;
        }
        else if (saved[idx] === null) {
            // no value yet, skip
            idx++;
            continue;
        }

        // set dropdown to saved value
        f.querySelectorAll('select[name="item"] option')[saved[idx]].selected = true;
        idx++;
    }
})();