简体   繁体   English

增量与其余部分不起作用

[英]Increment with remainder not working

In my script the computer clicks through contact tabs in WhatsApp Web and for each checks whether the person is online or not. 在我的脚本中,计算机单击WhatsApp Web中的联系人选项卡,然后每次检查该人是否在线。 This is done with a loop, which starts again when contact number 16 is reached. 这是通过一个循环完成的,当到达触点号16时,该循环再次开始。 Anyhow, the loop doesn't work and the variable 'i' doesn't increase. 无论如何,循环不起作用,变量“ i”也不会增加。 This is strange, since if I replace selectContact( ${i} ) by console.log, the increment works. 这很奇怪,因为如果我用console.log替换selectContact( ${i} ),那么增量就可以了。 Maybe the ${} prevents the i from updating? 也许${}阻止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);

Here is the code for selectContact, if the issue should lie within here. 如果问题应在此处,则这是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")
        }
    }
}

You've missed out the var statement declaring i in your for loop, meaning it overwrites your global i . 您已经错过了在var循环中声明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")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM