油猴脚本 点击按钮的写法
在油猴脚本中,你可以使用以下方法来点击一个按钮:
- 使用
document.querySelector()来选择要点击的按钮元素,然后使用click()方法来模拟点击事件。
// ==UserScript==
// @name Button Click Example
// @namespace http://your-namespace.com
// @version 1.0
// @description Example script to click a button using Greasemonkey/Tampermonkey
// @match http://example.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 选择要点击的按钮元素
var button = document.querySelector('button');
// 模拟点击事件
button.click();
})();
- 如果按钮使用了特定的类名或id,你可以使用
document.getElementsByClassName()或document.getElementById()来选择按钮元素。
// ==UserScript==
// @name Button Click Example
// @namespace http://your-namespace.com
// @version 1.0
// @description Example script to click a button using Greasemonkey/Tampermonkey
// @match http://example.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 选择要点击的按钮元素
var button = document.getElementsByClassName('my-button')[0]; // 使用类名
// 或
var button = document.getElementById('my-button'); // 使用id
// 模拟点击事件
button.click();
})();
请确保在脚本的 @match 元数据指定了正确的网站匹配模式,以便脚本在正确的页面上执行
原文地址: https://www.cveoy.top/t/topic/iq8i 著作权归作者所有。请勿转载和采集!