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

安装篡改猴

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

创建用户脚本

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

脚本注释说明

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

保存并测试脚本

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

进阶操作

定时任务

通过 setIntervalsetTimeout 实现定时操作:

javascript 复制代码
// 每 5 秒执行一次
setInterval(() => {
    console.log('定时任务执行中...');
    const button = document.querySelector("#buttonID");
    if (button) button.click();
}, 5000);

动态URL匹配

使用正则表达式匹配多种 URL:

javascript 复制代码
// ==UserScript==
// @match        https://*.example.com/*
// @match        https://another-site.com/page/*
// ==/UserScript==

高级权限

通过 @grant 使用更多功能,如跨域请求或本地存储:

javascript 复制代码
// ==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);
    }
});

示例:自动登录脚本

javascript 复制代码
// ==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("登录按钮已点击");
    }
})();
相关推荐
李狗蛋儿啊1 小时前
zero自动化框架搭建---Git安装详解
运维·git·自动化
小金的学习笔记2 小时前
如何在本地和服务器新建mysql用户和密码
运维·服务器·mysql
s_fox_2 小时前
nginx ngx_http_module(7) 指令详解
运维·nginx·http
EasyNVR2 小时前
EasyRTC智能硬件:实时畅联、沉浸互动、消音护航
运维·服务器·网络·安全·音视频·webrtc·p2p
CarryBest3 小时前
Jenkins 环境搭建---基于 Docker
运维·jenkins
若云止水3 小时前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_process_options
运维·nginx
s_fox_3 小时前
nginx ngx_http_module(9) 指令详解
运维·nginx·http
xing.yu.CTF5 小时前
Web入侵实战分析-常见web攻击类应急处置实验2
运维·服务器·windows·web安全·apache·php漏洞·phpstudy后门漏洞
我们的五年6 小时前
【Linux网络】TCP/IP地址的有机结合(有能力VS100%???),IP地址的介绍
linux·运维·网络·tcp/ip
Mike_188702783518 小时前
1688代采下单API接口使用指南:实现商品采集与自动化下单
前端·python·自动化