抖音点赞
web 脚本
Javascript
main();
async function main() {
try {
log('start');
const run = createConcurrent(2);
await waitFor(
async () => {
const target = [document.querySelector('#island_d3bbb'), document.querySelector('#island_d3bbb > div.LO5TGkc0')];
await Promise.all(target.map(item => run(() => item.click())));
log('click end ');
},
() => getRandomIntInclusive(1, 5) * 1e3
);
log('end');
} catch (err) {
console.error('err:', err);
}
}
客户端模拟点击
Javascript
const robot = require('robotjs');
main();
async function main() {
try {
log('start');
const run = createConcurrent(2);
await waitFor(
async () => {
// 获取鼠标当前位置
let mouse = robot.getMousePos();
log('Mouse is at x:' + mouse.x + ' y:' + mouse.y);
// 移动鼠标到指定位置 (例如:x = 500, y = 300)
const x = 631 + getRandomIntInclusive(0, 60),
y = 366 + getRandomIntInclusive(0, 50);
robot.moveMouse(x, y);
log('Mouse moved to', x, y);
await Promise.all(
new Array(getRandomIntInclusive(1, 30)).fill(0).map((item, index) =>
run(async () => {
// 执行鼠标左键点击
robot.mouseClick();
await delay(getRandomIntInclusive(1, 10) * 1e2);
robot.moveMouse(
x + getRandomIntInclusive(0, 50),
y + getRandomIntInclusive(0, 50)
);
log('click', index);
})
)
);
// 将鼠标移回原位置
log('click end ');
},
() => getRandomIntInclusive(1, 7) * 1e3
);
log('end');
} catch (err) {
console.error('err:', err);
}
}
common libs
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function log(...args) {
return console.log(`[${new Date().toLocaleTimeString()}]: `, ...args);
}
async function waitFor(fn, msFn, maxCount = -1) {
let pollCount = 0;
async function callback(resolve, reject) {
try {
const res = await Promise.resolve(fn());
if (res) {
return resolve(res);
}
} catch (err) {
log('fn poll err', fn?.name, err);
}
pollCount++;
if (maxCount != -1 && pollCount > maxCount) {
return reject(`maxCount limit, ${maxCount}`);
}
await delay(msFn());
callback(resolve, reject);
}
await new Promise((resolve, reject) => callback(resolve, reject));
}
async function delay(ms) {
return new Promise((resolve) => {
const id = setTimeout(() => {
clearTimeout(id);
resolve(1);
}, ms);
});
}
function createConcurrent(concurrency) {
let pendingCount = 0;
const queue = [];
async function concurrentWrapper(callback) {
if (pendingCount >= concurrency) {
await new Promise((resolve) => queue.push(resolve));
}
pendingCount++;
try {
await Promise.resolve(callback());
} finally {
pendingCount--;
if (queue.length > 0) {
const resolve = queue.shift();
resolve();
}
}
}
return concurrentWrapper;
}