我编写了一个脚本来在网站上执行一些自动操作(这不是我的)。
该网站是某种电脑游戏在线商店。用户选择一个项目并单击“撤回”按钮。当网站负载过重时(经常),用户经常会收到一条消息:“负载过重 - 再试一次!”并且必须一次又一次地点击同一个按钮,直到他收到该元素或收到一条消息说“该元素已售出!”。
一切都在 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);
}
它成功读取消息并打印出“成功:停止!” - 每两秒一次......持续进行,直到我阻止它手动这样做。看起来像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/