Tampermonkey脚本不断单击按钮

编程入门 行业动态 更新时间:2024-10-21 11:35:03
本文介绍了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 ajax.googleapis/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:

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

更多推荐

Tampermonkey脚本不断单击按钮

本文发布于:2023-11-06 14:21:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   脚本   按钮   Tampermonkey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!