Config_Manager

设置获取配置的APIs

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/476522/1292633/Config_Manager.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

const cfg = {};
(() => {
'use strict';

const config = {};

const inplace = (() => {
    const id = "test";
    try{
        const cap = GM_registerMenuCommand("测试", Function.prototype, {id});
        GM_unregisterMenuCommand(cap);
        return (cap === id);
    } catch(e) {return false;}
})();

const tmpu = {};

const register = (() => {
    const boolean_prefix = ["❌(已禁用) ", "✅(已启用) "];
    const int_family = ['uint', 'int'];
    return ({name, default: def, type = "other", desc: prompts, tips: ipt, input: func = prompt, callback: listener = Function.prototype, init = false, autoClose = true, judge: _jud, temp = false}) => {
        // let val = GM_getValue(name, def);
        // config[name] = val;
        const judge = (() => {
            if (typeof _jud === 'function') return _jud;
            return () => true;
        })();
        if (!temp) Object.defineProperty(config, name, {
            get: () => GM_getValue(name, def),
            set: val => GM_setValue(name, val)
        });
        else tmpu[name] = def;
        if (typeof init === 'function') init(name, config[name]);
        if (int_family.includes(type)) {
            const judge = (type === 'uint') ? (s => (s|0) < 0) : (()=>false);
            if (func === prompt) {
                func = () => {
                    let p;
                    do {p = prompt(ipt, config[name]);} while(isNaN(p) || judge(p));
                    return p | 0;
                };
            }
            type = 'other';
        }
        const cfg = {id: name, autoClose};
        if (type === 'bool') {
            let cont;
            const reg = () => cont = GM_registerMenuCommand(boolean_prefix[config[name] | 0] + prompts, () => {
                const newval = !config[name];
                if (judge(name, newval))
                    config[name] = newval;
            }, cfg);
            if (true == init) listener(name, config[name], config[name], cont);
            const stn = (() => {
                const re_reg = inplace ? reg : () => (GM_unregisterMenuCommand(cont), reg());
                return (_1, ov, nv) => (reg(), listener(name, ov, nv, cont));
            })();
            if (!temp) GM_addValueChangeListener(name, stn);
            else Object.defineProperty(config, name, {
                get: () => tmpu[name],
                set: v => (stn(name, tmpu[name], v), tmpu[name] = v),
            });
            reg();
        } else if (type === "other") {
            if (func === prompt) func = () => prompt(ipt, config[name]);
            const cont = GM_registerMenuCommand(prompts, () => {
                const inp = func();
                if (judge(name, inp))
                    GM_setValue(name, inp);
            }, cfg);
            if (true == init) listener(name, config[name], config[name], cont);
            if (!temp) GM_addValueChangeListener(name, (_1, ov, nv) => listener(name, ov, nv, cont));
            else Object.defineProperty(config, name, {
                get: () => tmpu[name],
                set: v => (listener(name, tmpu[name], v, cont), tmpu[name] = v),
            });
        }
        console.debug(name, "in type", type, "\nRegistered!");
    };
})();

cfg.config = config;
cfg.register = register;
})();