← back to Pattern Vault

wpb-uploaders/lib/human.js

37 lines

/*
 * Shared anti-bot human-behavior helpers for every WPB marketplace uploader.
 * Enforces the marketplace-uploader-anti-bot-cadence rule: human-like typing
 * (typos + backspace, junk-then-retype), variable delays. Reused by all platforms.
 */
const rnd = (a, b) => Math.floor(a + Math.random() * (b - a));
const sleep = ms => new Promise(r => setTimeout(r, ms));

// type with occasional typo + backspace correction and variable cadence
async function humanType(el, text) {
  for (const ch of text) {
    if (Math.random() < 0.06) { // slip: type a wrong char then fix it
      await el.type(String.fromCharCode(97 + rnd(0, 26)), { delay: rnd(40, 140) });
      await sleep(rnd(120, 320));
      await el.press('Backspace');
      await sleep(rnd(80, 220));
    }
    await el.type(ch, { delay: rnd(45, 165) });
  }
}

// sometimes type junk, pause, select-all, delete, then type the real value
async function messyType(el, text) {
  if (Math.random() < 0.4) {
    const junk = 'asdf'.slice(0, rnd(2, 4));
    await humanType(el, junk);
    await sleep(rnd(300, 900));
    await el.press('Control+A').catch(() => {});
    await el.press('Meta+A').catch(() => {});
    await el.press('Backspace');
    await sleep(rnd(200, 600));
  }
  await humanType(el, text);
}

module.exports = { rnd, sleep, humanType, messyType };