gpt4 book ai didi

javascript - JS 忽略了我的 setInterval

转载 作者:行者123 更新时间:2023-11-29 19:12:31 25 4
gpt4 key购买 nike

我写了一个脚本来在网站上执行一些自动化操作(这不是我的)。

该网站是某种 PC 游戏在线商店。用户选择一个项目并单击“撤回”按钮。当网站负载很重时(经常),用户经常会收到一条消息说“负载过重 - 再试一次!”并且必须一次又一次地点击同一个按钮,直到他得到该元素或收到一条消息说“该元素已售出!”。

一切都在 chrome 扩展中运行!

我的脚本执行以下操作:

  • 给按钮添加一个onClick事件来运行一个函数

  • 点击“提现”

  • 阅读来自网站的消息

  • 取决于消息:

    • “正在发送要约...”- 什么也不做,在间隔后再次阅读

    • “商品已售出!” - 停止间隔

    • “重载 - 再试一次!” - 单击一个元素以删除消息并再次“撤回”

问题:

间隔设置为 2000 毫秒,但脚本似乎只是不停地向提款按钮发送垃圾邮件,并且似乎忽略了 clearInterval()。

我的代码:

function buy() {

//Get the innerHTML for the box that displays the message
var message = document.getElementsByClassName("pm-content")[0].innerHTML;

//Message: Offer is being sent! - DO NOTHING!
if (message == "Please wait while your trade offer is being sent...") {
console.log("Loading: Going on!")
}

//Message: Item is gone! - STOP EVERYTHING!
if (message == "Items unavailable") {
console.log("Unavailable: Stopping!")
clearInterval(buyInterval);
}

//Message: Transaction successfull! - STOP EVERYTHING
if (message.includes("Trade offer has been sent! Code: ")) {
console.log("Success: Stopping!")
clearInterval(buyInterval);
}

if (message == "Heavy load! - Try again!") {
console.log("Overload: Going on!")
document.getElementById("pgwModal").click(); //Click to remove the message
document.getElementById("withdraw").click(); //Click withdraw again

}
}



function forceBuy() {
buyInterval = setInterval(function(){ buy() }, 2000);
}

var button = document.getElementById("withdraw");

withdraw.onclick=function(){ forceBuy () };

感谢任何帮助!


编辑_1

现在的代码:

(function(){  //creating isolated scope to avoid using global variables.
var buyInterval; // declaring sharing variables.

function buy() {

var message = document.getElementsByClassName("pm-content")[0].innerHTML;

if (message == "Please wait while your trade offer is being sent...<br><small>(this might take up to 5 minutes)</small>") {
console.log("Loading: Going on!")
}

if (message == "You cannot afford that withdrawal.") {
console.log("Broke: Stopping!")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}

if (message == "Items unavailable") {
console.log("Unavailable: Stopping!")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}

if (message.includes("Trade offer has been sent!")) {
console.log("Success: Stopping!")
clearInterval(buyInterval);
}

if (message.includes("missing")) {
console.log("Missing: Stopping")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}

if (message == "You can have only one pending deposit or withdrawal.") {
console.log("Pending: Stopping!")
document.getElementById("pgwModal").click();
clearInterval(buyInterval);
}

if (message == "Too many active trades") {
console.log("Overload: Going on!")
document.getElementById("pgwModal").click();
document.getElementById("withdraw").click();
}

}

function forceBuy() {
return setInterval(function(){ buy(); }, 2000); // not necessary but // will be more readable
}

var button = document.getElementById("withdraw");

withdraw.onclick=function(){ //making a closure to catch buyInterval variable
buyInterval = forceBuy ();
};

}())

感谢 Vitalii 提供此代码 - 它现在似乎工作得更好,因为它不再不断地向按钮发送垃圾邮件。可悲的是,另一个问题仍然存在:例如,如果脚本到达代码的这一部分:

    if (message.includes("Trade offer has been sent!")) {
console.log("Success: Stopping!")
clearInterval(buyInterval);
}

它成功读取消息并打印出“Success: Stopping!” - 每两秒一次......一直持续到我停止手动操作为止。好像是 clearInterval(buyInterval);仍然被忽略。

我在这里做错了什么?

最佳答案

(function(){  //creating isolated scope to avoid using global variables.
var buyInterval; // declaring sharing variables.

function buy() {
... buying action
}

function forceBuy() {
return setInterval(function(){ buy(); }, 2000); // not necessary but // will be more readable
}

var button = document.getElementById("withdraw");

withdraw.onclick=function(){ //making a closure to catch buyInterval variable
buyInterval = forceBuy ();
};

}())

关于javascript - JS 忽略了我的 setInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37743549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com