JavaScript循环不会退出

时间:2015-03-28 23:12:31

标签: javascript jquery loops browser infinite

我已经尝试了我能想到的一切。

我在IMVU移动网站上使用注入的JavaScript为IMVU构建了一种聊天机器人。我有一个循环来抓取收到的消息,并搜索某些关键术语,如以斜杠(/)开头的消息来指示对机器人的命令。

当使用某些命令时,我遇到一个问题,即机器人似乎陷入了循环,几乎就像在循环中修改了for循环的索引一样。代码包含在下面。

如果您需要更多,请询问,如果您发现可能导致问题的原因,请告诉我。我的机智已经结束了。

只需注意:jQuery已正确注入,我的所有变量都存在,调试控制台中没有错误,并且在Windows 7 x64上的Chrome 41.0.2272.101m下运行。

function verifyCommand() {
  if (document.getElementsByClassName("message-list-item").length > last_cmd_count && !processing_commands) {
    var new_length = $('.message-list .message-list-item').length;
    console.log("Begin processing commands... ** SYSTEM LOCK **");
    console.log(new_length);
    for (var i = last_cmd_count; i < (new_length); i++) {
      processing_commands = true;
      try {
        var callinguser = $('.message-list .message-list-item .header .username .username-text')[i].innerText.replace("Guest_", "");
        var messagetext = $('.message-list .message-list-item .message .message-text')[i].innerText
        if (callinguser != "USERNAME REMOVED") {
          if (messagetext.substr(0, 1) == "/") {
            if (strContains(callinguser, "IMVU User")) {
              die();
            }
            processCommand(messagetext.substr(1), callinguser);
          } else {
            if (messagetext.toLowerCase().indexOf('roomgreet') > -1 || messagetext.toLowerCase().indexOf('room greet') > -1) {
              if (detectFlirt()) {
                sendMsgRaw('Please do not hit on me, ' + callinguser + '.');
                if (!isAdmin(callinguser)) {
                  logIdiot(callinguser);
                }
              } else if (strContains(messagetext, 'what is ')) {
                sendMsgRaw('Please use /solve or /advsolve for math.');
              } else {
                if (callinguser != "USERNAME REMOVED") {
                  ident();
                }
              }
            }
            if (strContains(messagetext, 'free') && strContains(messagetext, 'credits') && strContains(messagetext, 'http://')) {
              sendMsgFrom("*** SCAM ALERT ***", callinguser);
            }
          }
        }
      } catch (ex) {} finally {}
    }
    processing_commands = false;
    last_cmd_count = new_length;
    console.log("Finish processing commands... ** SYSTEM FREE **");
    if (monitoring) {
      verifyUserMessageCount();
    }
  }
}

可以在 http://common.snftech.tk/imvu/roomgreet-html-sample.htm

找到IMVU移动消息的HTML

2 个答案:

答案 0 :(得分:0)

尝试更改您的函数以使用each()循环遍历每个元素而不是循环。处理完一个元素后,在元素中添加一个“已处理”类,这样我们以后就不会再看它们了。这应该比强迫我们的逻辑更加稳定,以跟上已经处理过的内容。

Here is a jsFiddle,,从您的网页中输入实际导致问题的html并查看是否仍然存在

function verifyCommand() {
  //fixed some logic in here
  if ($(".message-list-item").length > last_cmd_count && !processing_commands) {
    processing_commands = true; // you should set this immediately 
    var new_length = $('.message-list-item').length;
    console.log("Begin processing commands... ** SYSTEM LOCK **");
    console.log('Last command count: '+ last_cmd_count +', New Length: '+new_length);
    var newMessages = $('.message-list-item:not(.processed)'); // get all of the message elements that do not have the class "processed" 
    // loop through each of the message elements
    newMessages.each(function(index, element){
         console.log('Processing new element at index '+index );
         try {
              var callinguser = $(this).find('.username-text').text().replace("Guest_", "");
              var messagetext = $(this).find('.message-text').text();
              $(this).addClass('processed'); // add processed class to the element so we know not to process it again later  
              if (callinguser != "RoomGreet") {
                if (messagetext.match(/^\//)) {
                  if (callinguser.match(/IMVU User/)) {
                    die();
                  }
                  processCommand(messagetext.substr(1), callinguser);
                }
                else {
                    if (detectFlirt(messagetext)) {
                      if (!isAdmin(callinguser)) {
                        sendMsgRaw('Please do not hit on me, ' + callinguser + '.');
                        logIdiot(callinguser);
                      }
                    }
                    else if (messagetext.match('what is ')) {
                      sendMsgRaw('Please use /solve or /advsolve for math.');
                    }
                    else {
                      if (callinguser != "Nezzle" && !isAdmin(callinguser)) {
                        ident();
                      }
                    }
                  if (strContains(messagetext,"imvu") && strContains(messagetext,"credits") && strContains(messagetext,"http://")) {
                    sendMsgFrom("*** SCAM ALERT ***", callinguser);
                  }
                }
              }
         }
         catch (ex) {
             console.log('caught error');
         } 
         finally {
         }
    });
    last_cmd_count = new_length;
    console.log("Finish processing commands... ** SYSTEM FREE **");
    processing_commands = false;
    if (monitoring) {
      verifyUserMessageCount();
    }
  }
}

答案 1 :(得分:-1)

我认为你的问题是这个

if (messagetext.substr(0,1) == "/") {

如果用户在“/”前面有空格,那么它将不会解释为命令,因此您需要处理

var messagetext =  $('.message-list .message-list-item .message .message-text')[i].innerText

从消息文本中删除所有空格,如下所示

messagetext.text().replace(" ", "");

你还应该有更多错误捕获

if (messagetext.substr(0,1) == "/") {