Tampermonkey脚本不断单击按钮 [英] Tampermonkey script keeps clicking on button

查看:116
本文介绍了Tampermonkey脚本不断单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Javascript/Tampermonkey完全陌生.我正在尝试使Tampermonkey脚本在页面加载后立即(快速)在特定页面按钮上单击一次.单击鼠标后,网站要求进行验证.验证必须手动完成.

I am completely new to Javascript/Tampermonkey. I am trying to make a Tampermonkey script that (fast) clicks once on a specific page button as soon as the page loads. After the mouse click, the website asks for a verification. The verification has to be done manually.

这是网站上按钮的代码:

This is the code of the button on the website:

<button class="btn  btn--arrow  push-right  js-continue-scrollposition" type="submit" id="btnSave" name="Command" value="plaats">Send</button>

这是我在Tampermonkey中的脚本:

This is my script in Tampermonkey:

// ==UserScript==
// @name         Test
// @namespace    Test
// @version      1
// @description  test
// @author       Me
// @match        LINK OF THE WEBSITE HERE
// @grant        none
// ==/UserScript==
(function() {
document.querySelector("#btnSave").click();

})();

这段代码似乎可以正常工作,但是只有一个问题.该脚本会不断向网站上的按钮发送垃圾邮件.我只想在按钮上产生一个(快速!)鼠标单击,然后需要停止或暂停脚本,以便进行验证.我忙了一整天,但找不到解决方案.也许有人可以帮忙吗?

This code seems to work fine but there is only one problem. The script keeps spamming the button on the website. I only want to generate one (fast!) mouse click on the button, and after that the script needs to be stopped or paused so I can do the verification. I've been busy for a whole day but I can't find a solution. Maybe someone can help?

请记住,我对此完全陌生.这是我使用所有这些的第一天.

Please keep in mind that I am completely new to this. This is my first day of using all of this.

推荐答案

一种解决方法.

制作一个名为"isClicked"的布尔值并将其设置为false,然后在运行代码时,检查脚本是否单击了按钮,如果是,则将布尔值更改为true.然后停止该功能.

Make a boolean named "isClicked" and set it to false, then when you run the code, check if the script has clicked the button, if yes change the boolean to true. Then stop the function.

// ==UserScript==
// @name         Test
// @namespace    Test
// @version      1
// @description  test
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @author       Me
// @match        LINK OF THE WEBSITE HERE
// @grant        none
// ==/UserScript==

var $ = window.$;                //  Avoids warnings in TamperMonkey
var isClicked = false;

window.setInterval(function(){
   if(!isClicked){               //  !isClicked checks if the boolean is not true
      $("#btnSave").click();
   }
}, 1000);                        //  1000 stands for the refresh rate of this function in milliseconds

$("#btnSave").click(function(){  //  Functions when button is clicked
   isClicked = true;
}); 

此答案使用 jQuery(JavaScript库)来简化javascript.

This answer uses jQuery (JavaScript Library) for simplified javascript.

注意:,您必须通过以下操作在Meta/Header"UserScript"中导入jQuery库:

Note: You have to import the jQuery Library in the Meta/Header "UserScript" by the following:

//@需要https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js

这篇关于Tampermonkey脚本不断单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆