GM_config 开源项目教程
1. 项目的目录结构及介绍
GM_config 是一个轻量级的跨浏览器图形设置框架,用于用户脚本中。项目的目录结构如下:
GM_config/
├── LICENSE
├── README.md
├── gm_config.ico
├── gm_config.js
├── gm_config_icon.png
├── gm_config_icon_large.png
└── types/
└── ...
LICENSE
:项目遵循的 LGPL-3.0 许可证。README.md
:项目的介绍和使用说明。gm_config.ico
和gm_config_icon.png
:项目的图标文件。gm_config.js
:项目的主要脚本文件。gm_config_icon_large.png
:项目的大图标文件。types/
:TypeScript 类型定义文件目录。
2. 项目的启动文件介绍
项目的启动文件是 gm_config.js
。这个文件包含了 GM_config 框架的核心功能,允许用户在用户脚本中创建和使用图形设置对话框。
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant GM_getValue
// @grant GM_setValue
在用户脚本中,通过 @require
指令引入 gm_config.js
文件,并使用 @grant
指令授权 GM_getValue
和 GM_setValue
权限。
3. 项目的配置文件介绍
GM_config 项目本身没有传统的配置文件,但可以通过在用户脚本中定义配置项来实现自定义配置。配置项通常在用户脚本的元数据块中定义,例如:
// ==UserScript==
// @name Example Script
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://example.com/*
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function() {
'use strict';
// Initialize GM_config
GM_config.init({
id: 'example_config',
fields: {
exampleField: {
label: 'Example Field',
type: 'text',
default: 'Default Value'
}
}
});
// Use GM_config
console.log(GM_config.get('exampleField'));
})();
在这个示例中,GM_config.init
方法用于初始化配置,定义了一个名为 exampleField
的字段,并设置了默认值。通过 GM_config.get
方法可以获取配置项的值。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考