Случайный логический 70/30 внутри функции? - PullRequest
0 голосов
/ 05 февраля 2019

Я запускаю в своей консоли скрипт для перелистывания на веб-странице Tinder со следующим скриптом:

https://github.com/dhrubesh/Tinder/blob/master/tinder.js

Но теперь его можно рассматривать как бот, поскольку он постоянно хранитсяпроведя пальцем вправо, и поэтому я хочу иметь правило 70/30 для считывания и изменил следующее:

var random_boolean = Math.random() < 0.7; //Random boolean added
let run = true, time_step = 1000;
const wait = (n = 1) => new Promise((rs, rj) => run ? setTimeout(rs, n) : rj()),
  click = async (cn, i = 0) => {
    document.getElementsByClassName(cn)[i].click();
    return wait(time_step)
  };
document.onkeydown = (e = window.event) => {
  if (e.key === "a") run = run ? true : explore() || true;
  else if (e.key === "z") console.log(time_step *= 0.9);
  else if (e.key === "x") console.log(time_step *= 1.1);
  else run = false;
};
const explore = async () =>
  click("recCard__info").then(() =>
    Array.from(document.getElementsByClassName("bullet")).reduce((p, e) =>
      p.then(() => {
        e.click();
        return wait(time_step)
      }), Promise.resolve())
  ).then(() => {
    // This if statement below doesn't work
    if(random_boolean){
      click("recsGamepad__button--like")
    }else{
      click("recsGamepad__button--dislike")
    }
    //click("recsGamepad__button--like")  <-- this works if there is no if statement
  }

  ).then(() => explore());
explore();

1 Ответ

0 голосов
/ 05 февраля 2019

Вам нужно поместить присвоение внутри функции, чтобы переменная пересчитывалась каждый раз.

).then(() => {
    var random_boolean = Math.random() < 0.7;
    if(random_boolean){
      click("recsGamepad__button--like")
    }else{
      click("recsGamepad__button--dislike")
    }
...