增加余数不起作用

Increment with remainder not working

在我的脚本中,计算机点击 WhatsApp Web 中的联系人选项卡,并针对每个选项卡检查此人是否在线。这是通过一个循环完成的,该循环在到达联系号码 16 时再次开始。无论如何,循环不起作用并且变量 'i' 不增加。这很奇怪,因为如果我将 selectContact(${i}) 替换为 console.log,增量会起作用。也许 ${} 阻止了 i 更新?

var i = 1
setInterval(function () {
    selectContact(`${i}`)

    if (document.getElementsByClassName("O90ur")[0] !== undefined) {
    var online = document.getElementsByClassName("O90ur")[0].innerHTML
        if (online == "online") {
        console.log(`${i}`)};
                        }
    i = i % 16 + 1
}, 1000);

这里是 selectContact 的代码,如果问题出在此处。

var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);

contacts = [];
chat_div = [];

function triggerMouseEvent(node, eventType) {
    var event = document.createEvent('MouseEvents');
    event.initEvent(eventType, true, true);
    node.dispatchEvent(event);
}

function getChatname(){
    $("#pane-side > div > div > div").find("._2FBdJ >    div._25Ooe").each(function(){ 
        contacts.push($(this).text());
        chat_div.push($(this));
    })  
}

function selectContact(name){
    getChatname()
    for (i = 0; i < contacts.length; i++){
        if (name.toUpperCase() === contacts[i].toUpperCase()){
             triggerMouseEvent(chat_div[i][0],"mousedown")
        }
    }
}

您错过了在您的 for 循环中声明 ivar 语句,这意味着它会覆盖您的全局 i.

function selectContact(name){
    getChatname()
    for (var i = 0; i < contacts.length; i++){
        if (name.toUpperCase() === contacts[i].toUpperCase()){
             triggerMouseEvent(chat_div[i][0],"mousedown")
        }
    }
}