为什么在我 运行 我的 chrome 扩展程序注入脚本后页面停止

Why does the page stop after I run my chrome extension which injects scripts

我正在尝试 chrome 扩展来玩游戏。

我正在使用玩游戏的扩展程序注入脚本,但在注入代码后页面挂起并停止工作

注入的代码是:

function sleep(milliseconds) {
  const date = Date.now();
  let currentDate = null;
  do {
    currentDate = Date.now();
  } while (currentDate - date < milliseconds);
}
function wait() {
  while (
    !String(document.getElementById("showpoke").innerHTML).includes(
      "Try moving to another spot."
    ) ||
    !document.getElementById("catch")
  ) {
    sleep(1000);
  }
}
document.getElementById("dr-n").click();
wait();
document.getElementById("dr-s").click();

我首先想要 运行 的代码是

while (run) {
  document.getElementById("dr-n").click, 2000;
  if (document.getElementById("catch")) {
    document.getElementById("catch").click();
    run = false;
  }
}

但是这会导致页面超载并继续 while 循环,所以我添加了 wait 和 sleep,这又出现了同样的问题

你正在做我们称之为“忙等待”的事情,这意味着你不只是等待,而是通过让线程在空循环中保持忙碌来阻止线程执行,如果游戏使用相同的线程,那么它将也被阻止了,你应该尝试使用

setTimeout()

甚至

setInterval()

如果您想 运行 定期编写代码,如果这不是问题,我真的帮不上忙,因为我不知道游戏是如何运行的。