Greasemonkey 是一个浏览器扩展,允许用户在网页上运行自定义的 JavaScript 脚本。这些脚本可以修改网页的内容、行为或添加新的功能。为了提供更好的用户体验和脚本配置的灵活性,开发者通常会创建一个“config”或“options”页面,让用户可以自定义脚本的行为。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Greasemonkey 配置页面</title>
</head>
<body>
<h1>Greasemonkey 脚本配置</h1>
<label for="option1">选项1:</label>
<input type="text" id="option1">
<button onclick="saveOptions()">保存</button>
<script>
function saveOptions() {
const option1 = document.getElementById('option1').value;
GM_setValue('option1', option1);
}
window.onload = function() {
GM_registerMenuCommand('打开配置页面', function() {
window.open('options.html');
});
};
</script>
</body>
</html>
// ==UserScript==
// @name 示例脚本
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 示例脚本描述
// @author 你的名字
// @match http://example.com/*
// @grant GM_registerMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function() {
'use strict';
const option1 = GM_getValue('option1', '默认值');
// 使用 option1 进行操作
console.log('选项1:', option1);
})();
通过以上步骤和示例代码,你可以创建一个简单的配置页面,让用户可以自定义Greasemonkey脚本的行为。