【发布时间】:2019-02-04 21:24:01
【问题描述】:
我正在控制台中运行一个脚本,以便使用以下脚本在 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();
【问题讨论】:
-
您的
random_boolean看起来只计算了一次。 -
我怎样才能让它像一个无限循环一样?
-
var random_boolean = Math.random() < 0.7;不会神奇地重新计算该值。使用function random_boolean() {return Math.random() < 0.7}和if (random_boolean())或简单地使用if (Math.random() < 0.7) -
使用它仍然无法正常工作:
function random_boolean() { return Math.random() < 0.7 }if (random_boolean()){ click("recsGamepad__button--like") }else{ click("recsGamepad__button--dislike") }
标签: javascript random boolean