Greasy Fork

Greasy Fork is available in English.

Tampermonkey 配置

简易的 Tampermonkey 脚本配置库

当前为 2023-08-01 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/470224/1228585/Tampermonkey%20Config.js

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

作者
PRO-2684
版本
0.3.4
创建于
2023-07-05
更新于
2023-08-01
大小
4.2 KB
许可证
GPL-3.0

# `GM_config`

[![](https://img.shields.io/badge/Crazy%20Thur.-V%20me%2050-red?logo=kfc)](http://greasyfork.icu/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBaWZvIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--10e04ed7ed56ae18d22cec6d675b34fd579cecab/wechat.jpeg?locale=zh-CN)

## 🪄 功能

简易的 Tampermonkey 脚本配置库。 ([Greasy Fork](http://greasyfork.icu/scripts/470224)) ([GitHub](https://github.com/PRO-2684/gadgets/tree/main/GM_config))

## 🎉 特性

- 自动/手动注册菜单
- 配置修改后自动更新菜单(也支持由脚本修改)
- 支持监听配置获取/修改事件

## 🤔 权限

这个库需要以下权限:

```javascript
// @grant GM_setValue // 保存配置
// @grant GM_getValue // 获取配置
// @grant GM_registerMenuCommand // 注册菜单
// @grant GM_unregisterMenuCommand // 更新菜单
```

若你复制粘贴了上述代码,记得**删去注释**,否则可能报错。若有,你需要删去 `@grant none`。如果你代码内使用了 `window` 对象,你可能需要 `@grant unsafeWindow` 然后 `let window = unsafeWindow`。

## 📖 使用

```javascript
let config_desc = { // *配置描述*
password: {
name: "Password", // 显示名称
value: "tmp", // 默认值
processor: (v) => { // 处理用户输入,若不合法则报错
if (v.length < 3) throw "Too short!";
return v;
}
},
enabled: {
name: "Enabled",
value: true,
processor: GM_config_builtin_processors.boolean // 你可以使用内置处理器
},
price: {
name: "Price",
value: 10,
processor: GM_config_builtin_processors.integer(0, 100) // 部分内置处理器需要参数
},
foo: {
name: "Foo",
value: "bar"
// 若你认为不需要验证或处理用户输入,你也可以忽略 processor 项
}
}

let config = GM_config(config_desc, false); // *注册菜单命令*
// 第二个选项默认为 `true`,表示根据配置描述自动注册配置菜单
// 若为 `false`,则用户需要点击 `Show configuration` 后才会显示配置菜单
console.log(config.price); // *可以开始使用了 🎉*
window.addEventListener(GM_config_event, (e) => { // *监听配置变化*
console.log(config, e.detail);
});
```

## 📦 内置处理器

|名称|接受值|参数|例子|
|-|-|-|-|
|`boolean`|`true` 或 `false`|无|`GM_config_builtin_processors.boolean`|
|`integer`|任意 [`min`, `max`] 区间内的整数|`min`, `max` (`undefined` 认为是没有限制)|`GM_config_builtin_processors.integer(1, undefined)` (任意正整数)|
|`values`|任意数组 `accepted` 内的值|`accepted`|`GM_config_builtin_processors.values(["a", "b", "c"])` (允许 "a", "b" 或 "c")|

## 👀 完整的例子

将以下代码安装为脚本,观察它是如何工作的:

```javascript
// ==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 http://greasyfork.icu/*
// @icon http://greasyfork.icu/vite/assets/blacklogo16-bc64b9f7.png
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @require http://greasyfork.icu/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, false); // 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;
}, 5000);
})();
```

## ⚠️ 注意

- 这个项目正处于早期发展阶段