简体   繁体   中英

Click button when it changes to Active

Im currently trying to find a solution for my problem, I have a website were orders come in and there is a button which turns blue and I need to click it to accept all orders.

The logic behind it, it's a button which stays grey all the time, when a order enters it turns blue and active, and needs to be pressed. It's website in the browser.

window.onload=function() {

  setInterval(autoClick,100);
}

function autoClick() {

  if(document.getElementsByClassName("accept").length>0) {
    document.getElementsByClassName("accept")[0].click();
  }
}

I've started this but don't know how it shoud detect if the button turns blue.

What you can do is query for the id of this exact button and get its classList

Then you check if the classLsit contains the class you are looking for.

It would look something like this.

const btn = document.getElementById("btn");
console.log(btn.classList.contains("basic")); // true or false

so your code must be

window.onload=function()
  {
  const myAcceptButton = document.querySelector('.accept')

  setInterval(autoClick,100);

  function autoClick()
    {
    if (myAcceptButton.classList.contains('neworder'))
      {
      myAcceptButton.click();
      }
    }
  }

or shorter code with arrow function

window.onload=function() {

  const myAcceptButton = document.querySelector('.accept')

  setInterval(() => {
    if (myAcceptButton.classList.contains('neworder'))
      { myAcceptButton.click(); }
  }, 100);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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