安装篡改猴
- 打开浏览器扩展商店(Edge、Chrome、Firefox 等)。
- 搜索 Tampermonkey 并安装。 
  - 如图 
 
- 如图
- 安装后,浏览器右上角会显示一个带有猴子图标的按钮。
    
创建用户脚本
- 已进入篡改猴管理面板
- 点击 创建 创建
 脚本注释说明
脚本注释说明
- @name:脚本名称。
- @namespace:脚本唯一标识,可随意设置。
- @version:脚本版本。
- @description:脚本描述。
- @match:脚本运行的网页 URL 模式(支持通配符- *)。- 示例:https://example.com/*表示脚本在所有example.com的页面运行。
 
- 示例:
- @grant:声明权限。- none:不使用任何特殊权限。
- 可用权限:参考Tampermonkey 文档。
 

保存并测试脚本
- 在编辑器中,按 Ctrl+S 或点击保存按钮。
- 打开与 @match中 URL 对应的网页。
- 打开开发者工具(F12),在控制台查看脚本日志,确保脚本正常运行。
进阶操作
定时任务
通过 setInterval 或 setTimeout 实现定时操作:
// 每 5 秒执行一次
setInterval(() => {
    console.log('定时任务执行中...');
    const button = document.querySelector("#buttonID");
    if (button) button.click();
}, 5000);
动态URL匹配
使用正则表达式匹配多种 URL:
// ==UserScript==
// @match        https://*.example.com/*
// @match        https://another-site.com/page/*
// ==/UserScript==
高级权限
通过 @grant 使用更多功能,如跨域请求或本地存储:
 
// ==UserScript==
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
// 示例:跨域请求
GM_xmlhttpRequest({
    method: 'GET',
    url: 'https://api.example.com/data',
    onload: function(response) {
        console.log('数据加载成功:', response.responseText);
    }
});
示例:自动登录脚本
// ==UserScript==
// @name         自动登录
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自动填写用户名和密码并登录
// @match        https://login.example.com/*
// @grant        none
// ==/UserScript==
(function() {
    'use strict';
    // 自动填充用户名和密码
    const usernameField = document.querySelector("#username");
    const passwordField = document.querySelector("#password");
    const loginButton = document.querySelector("#loginButton");
    if (usernameField && passwordField && loginButton) {
        usernameField.value = "myUsername";
        passwordField.value = "myPassword";
        console.log("用户名和密码已填写");
        // 自动点击登录按钮
        loginButton.click();
        console.log("登录按钮已点击");
    }
})();



















