$Config

Allows users to configure scripts

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/446506/1683593/%24Config.js

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

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

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

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

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

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

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

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

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

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

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

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

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

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

作者
ctl2
版本
0.0.1.20251025124509
建立日期
2022-06-15
更新日期
2025-10-25
尺寸
338.3 KB
授權條款
未知

Example instance hosted here. Source code available here.

Usage
// @require     https://update.greasyfork.org/scripts/446506/1586074/%24Config.js
// @grant       GM.setValue
// @grant       GM.getValue
// @grant       GM.deleteValue

// Define a config

const $config = new $Config(
    'STORAGE_KEY',
    {
        get: (_, configs) => Object.assign(...configs),
        children: [
            {label: 'a', value: 0, get: ({value: a}) => ({a})},
            {label: 'b', value: 1, get: ({value: b}) => ({b})},
            {label: 'c', value: 2, get: ({value: c}) => ({c})}
        ]
    },
);

// Await config load & handle problems

try {
    await $config.ready;
} catch (error) {
    if (!$config.reset) {
        // There's a problem with the arguments passed to the $Config constructor
        throw error;
    }

    // There's a problem with the user's data (could be from manual editing or a script update)
    if (!window.confirm(`${error.message}\n\nWould you like to erase your data?`)) {
        return;
    }

    $config.reset();
}

// Apply the user's config

function useConfig() {
    const {a, b, c} = $config.get();

    // ...
}

useConfig();

// Set up config editing

const button = document.createElement('button');

button.addEventListener('click', async () => {
    await $config.edit();

    useConfig();
});

Constructor arguments
  1. [string] An identifier used to store data
  2. [Root] A schema for user config data
  3. {
     defaultStyle: [DefaultStyle] Instructions for customizing the UI's appearance,
     outerStyle: [object] CSS to apply to the UI's root element,
     patches: [array<function>] Patches to update old config objects,
    }
The third argument and each of its properties are optional

Examples