Tampermonkey Config

Simple Tampermonkey script config library

目前為 2023-08-01 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

作者
PRO-2684
版本
0.3.2
建立日期
2023-07-05
更新日期
2023-08-01
尺寸
3.9 KB
授權條款
GPL-3.0

GM_config

🪄 Function

Simple config lib for Tampermonkey scripts. (Greasy Fork) (GitHub)

🎉 Features

  • Automatically register menu
  • Automatically update menu after config modifications (also support those by your script)
  • Support listening for config get, set events

🤔 Permission

This library needs the following permissions to work:

// @grant        GM_setValue // Save your config
// @grant        GM_getValue // Get your config
// @grant        GM_registerMenuCommand // Register menu
// @grant        GM_unregisterMenuCommand // Update menu

Delete the comment if you copied and pasted the code, or there might be errors. You may want to delete @grant none (if present). If you used window object in your script, try @grant unsafeWindow and then let window = unsafeWindow.

📖 Usage

let config_desc = { // *Config description*
    password: {
        name: "Password", // Display name
        value: "tmp", // Default value
        processor: (v) => { // Process user inputs, throw error if invalid
            if (v.length < 3) throw "Too short!";
            return v;
        }
    },
    enabled: {
        name: "Enabled",
        value: true,
        processor: GM_config_builtin_processors.boolean // You can use builtin processors
    },
    price: {
        name: "Price",
        value: 10,
        processor: GM_config_builtin_processors.integer(0, 100) // Some builtin processors accept arguments
    },
    foo: {
        name: "Foo",
        value: "bar"
        // You may omit processor if you don't need to validate or process user inputs
    }
}

let config = GM_config(config_desc); // *Register menu commands*
console.log(config.price); // *Start using config as you wish 🎉*
window.addEventListener(GM_config_event, (e) => { // *Listen to config changes*
    console.log(config, e.detail);
});

📦 Built-in processors

Name Accept Argument Example
boolean true or false None GM_config_builtin_processors.boolean
integer Any integer in range [min, max] min, max (undefined infers as no limit) GM_config_builtin_processors.integer(1, undefined) (Any positive integer)
values Any value in given array accepted accepted GM_config_builtin_processors.values(["a", "b", "c"]) (Accepts "a", "b" or "c")

👀 Working example

Install below code as a script, and see how does it work:

// ==UserScript==
// @name         Test Config
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  This is an example to demostrate the usage of greasyfork.org/scripts/470224.
// @author       PRO
// @match        https://greasyfork.org/*
// @icon         https://greasyfork.org/vite/assets/blacklogo16-bc64b9f7.png
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @require      https://greasyfork.org/scripts/470224-tampermonkey-config/code/Tampermonkey%20Config.js
// @license      gpl-3.0
// ==/UserScript==

(function() {
    'use strict';
    let config_desc = { // Config description
        password: {
            name: "Password", // Display name
            value: "tmp", // Default value
            processor: (v) => { // Process user inputs, throw error if invalid
                if (v.length < 3) throw "Too short!";
                return v;
            }
        },
        enabled: {
            name: "Enabled",
            value: true,
            processor: GM_config_builtin_processors.boolean // You can use builtin processors
        },
        val: {
            name: "Float",
            value: 11.4,
            processor: parseFloat
        }
    }
    let config = GM_config(config_desc); // Register menu commands
    window.addEventListener(GM_config_event, (e) => { // Listen to config changes
        console.log(config, e.detail);
    });
    window.setTimeout(() => { // Change config values, and menu commands will be updated automatically
        config.val += 1;
    }, 2000);
})();

⚠️ Note

  • This project is in early development.