Search code examples
javascriptgoogle-chrome-extension

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


I am trying to make chrome extension to play a game.

I was injecting scripts using my extension which plays the game but the page hangs and stops working after in inject the code

The code injected is:

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();

the code i wanted to run first was

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

but this was overloading the page with continues while loop so i added wait and sleep which again gives the same problem


Solution

  • You are doing what we call a "busy wait" wich means that instead of just waiting, you are blocking the thread execution by keeping it busy in an empty loop, if the game uses the same thread then it will be blocked too, you should probably try using

    setTimeout()

    or even

    setInterval()

    if you want to run code periodicly, if that is not the issue i can't really help since i don't know how the game works.