Xetera / ghost-cursor

🖱️ Generate human-like mouse movements with puppeteer or on any 2D plane
MIT License
1.03k stars 120 forks source link

Make random movement to avoid headless detection #111

Closed teammakdi closed 6 months ago

teammakdi commented 10 months ago

Hi, I'm looking how to make random mouse movement (random starting and ending points within viewport) to avoid headless detection.

teammakdi commented 10 months ago
import puppeteer from 'puppeteer';
import { createCursor } from 'ghost-cursor'

function getRandomNumber (x) {
  return Math.ceil(Math.random() * x)
}

(async () => {
  const browser = await puppeteer.launch({
    headless: false
  });
  const page = await browser.newPage();

  await page.goto('https://infosimples.github.io/detect-headless/');

  const width = 1080;
  const height = 1024;

  await page.setViewport({width: width, height: height});

  await new Promise(resolve => setTimeout(resolve, 2000));

  const cursor = createCursor(page)
  cursor.toggleRandomMove(true)
  await cursor.moveTo({
    x: getRandomNumber(width),
    y: getRandomNumber(height)
  })
  await new Promise(resolve => setTimeout(resolve, 2000));

  await cursor.moveTo({
    x: getRandomNumber(width),
    y: getRandomNumber(height)
  })
  await new Promise(resolve => setTimeout(resolve, 2000));

  await cursor.moveTo({
    x: getRandomNumber(width),
    y: getRandomNumber(height)
  })
  await new Promise(resolve => setTimeout(resolve, 2000));
  await browser.close();
})();

Not sure if there's any better way to do the same.

marcusdiy commented 8 months ago

For me too its not clear the use of toggleRandomMove(). Does it move randomly, when or how much? Seems like its used to create a random movement before going to the desired position. Btw the esiest solution for you would be to create a wrapper function.

TheGP commented 6 months ago

@marcusdiy it is always doing it while no action for the mouse is set. So if you are typing after mouse action you have to disable it.