【网页自动化】篡改猴入门教程

安装篡改猴

  1. 打开浏览器扩展商店(Edge、Chrome、Firefox 等)。
  2. 搜索 Tampermonkey 并安装。
  3. 安装后,浏览器右上角会显示一个带有猴子图标的按钮。

创建用户脚本

  1. 已进入篡改猴管理面板
  2. 点击创建

脚本注释说明

  • @name:脚本名称。
  • @namespace:脚本唯一标识,可随意设置。
  • @version:脚本版本。
  • @description:脚本描述。
  • @match:脚本运行的网页 URL 模式(支持通配符 *)。
    • 示例:https://example.com/* 表示脚本在所有 example.com 的页面运行。
  • @grant:声明权限。
    • none:不使用任何特殊权限。
    • 可用权限:参考Tampermonkey 文档。

保存并测试脚本

  • 在编辑器中,按 Ctrl+S 或点击保存按钮。
  • 打开与 @match 中 URL 对应的网页。
  • 打开开发者工具(F12),在控制台查看脚本日志,确保脚本正常运行。

进阶操作

定时任务

通过 setIntervalsetTimeout 实现定时操作:

// 每 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("登录按钮已点击");
    }
})();

### Tampermonkey 脚本示例与教程 Tampermonkey 是一种强大的用户脚本管理工具,允许开发者通过编写自定义 JavaScript 来增强网页的功能。以下是几个常见的 Tampermonkey 用户脚本示例及其说明。 #### 示例 1: 自动登录到网站 此脚本会自动填充用户名和密码字段并提交表单。 ```javascript // ==UserScript== // @name Auto Login Script // @namespace http://tampermonkey.net/ // @version 0.1 // @description Automatically fill and submit login form. // @author Anonymous // @match https://example.com/login* // @grant none // ==/UserScript== (function() { 'use strict'; document.getElementById('username').value = 'your_username'; // 设置用户名 document.getElementById('password').value = 'your_password'; // 设置密码 const loginForm = document.querySelector('#login-form'); // 获取登录表单 if (loginForm) { loginForm.submit(); // 提交表单 } })(); ``` #### 示例 2: 修改网页上的文字 该脚本会在指定的网页上查找特定的文字,并将其替换为其他内容。 ```javascript // ==UserScript== // @name Text Replacement Script // @namespace http://tampermonkey.net/ // @version 0.1 // @description Replace specific text on a webpage with another string. // @author Anonymous // @match *://*.example.com/* // @grant none // ==/UserScript== (function() { 'use strict'; function replaceText(node, searchStr, newStr) { let walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT); while(walker.nextNode()) { walker.currentNode.nodeValue = walker.currentNode.nodeValue.replace(searchStr, newStr); } } replaceText(document.body, 'old_text', 'new_text'); // 替换旧文本为新文本 })(); ``` #### 示例 3: 添加设置界面 利用 `GM_registerMenuCommand` 函数创建一个菜单命令,用于打开设置界面[^4]。 ```javascript // ==UserScript== // @name Settings Interface Example // @namespace http://tampermonkey.net/ // @version 0.1 // @description Add settings interface to the script menu. // @author Anonymous // @match *://*/* // @grant GM_registerMenuCommand // ==/UserScript== (function() { 'use strict'; GM_registerMenuCommand("Open Settings", function() { alert("This is where you can add your custom settings."); }); })(); ``` #### 官方文档链接 更多关于 Tampermonkey 的高级特性和 API 使用方法可参考官方文档:[https://www.tampermonkey.net/documentation.php](https://www.tampermonkey.net/documentation.php)[^1] --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值