Xetera / ghost-cursor

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

Ghost Cursor

Generate realistic, human-like mouse movement data between coordinates or navigate between elements with puppeteer like the definitely-not-robot you are.

Oh yeah? Could a robot do this?

Installation

yarn add ghost-cursor

or with npm

npm install ghost-cursor

Usage

Generating movement data between 2 coordinates.

import { path } from "ghost-cursor"

const from = { x: 100, y: 100 }
const to = { x: 600, y: 700 }

const route = path(from, to)

/**
 * [
 *   { x: 100, y: 100 },
 *   { x: 108.75573501957051, y: 102.83608396351725 },
 *   { x: 117.54686481838543, y: 106.20019239793275 },
 *   { x: 126.3749821408895, y: 110.08364505509256 },
 *   { x: 135.24167973152743, y: 114.47776168684264 }
 *   ... and so on
 * ]
 */

Generating movement data between 2 coordinates with timestamps.

import { path } from "ghost-cursor"

const from = { x: 100, y: 100 }
const to = { x: 600, y: 700 }

const route = path(from, to, { useTimestamps: true })

/**
 * [
 *   { x: 100, y: 100, timestamp: 1711850430643 },
 *   { x: 114.78071695023473, y: 97.52340709495319, timestamp: 1711850430697 },
 *   { x: 129.1362373468682, y: 96.60141853603243, timestamp: 1711850430749 },
 *   { x: 143.09468422606352, y: 97.18676354029148, timestamp: 1711850430799 },
 *   { x: 156.68418062398405, y: 99.23217132478408, timestamp: 1711850430848 },
 *   ... and so on
 * ]
 */

Usage with puppeteer:

import { createCursor } from "ghost-cursor"
import puppeteer from "puppeteer"

const run = async (url) => {
  const selector = "#sign-up button"
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage()
  const cursor = createCursor(page)
  await page.goto(url)
  await page.waitForSelector(selector)
  await cursor.click(selector)
  // shorthand for
  // await cursor.move(selector)
  // await cursor.click()
}

Puppeteer-specific behavior


ghost-cursor in action

Ghost cursor in action on a form

Methods

createCursor(page: puppeteer.Page, start?: Vector, performRandomMoves?: boolean, defaultOptions?: DefaultOptions): GhostCursor

Creates the ghost cursor. Returns cursor action functions.

toggleRandomMove(random: boolean): void

Toggles random mouse movements on or off.

click(selector?: string | ElementHandle, options?: ClickOptions): Promise<void>

Simulates a mouse click at the specified selector or element.

move(selector: string | ElementHandle, options?: MoveOptions): Promise<void>

Moves the mouse to the specified selector or element.

moveTo(destination: Vector, options?: MoveToOptions): Promise<void>

Moves the mouse to the specified destination point.

getLocation(): Vector

Get current location of the cursor.

Other Utility Methods

installMouseHelper(page: Page): Promise<void>

Installs a mouse helper on the page. Makes pointer visible. Use for debugging only.

getRandomPagePoint(page: Page): Promise<Vector>

Gets a random point on the browser window.

path(point: Vector, target: Vector, options?: number | PathOptions): Vector[] | TimedVector[]

Generates a set of points for mouse movement between two coordinates.

How does it work

Bezier curves do almost all the work here. They let us create an infinite amount of curves between any 2 points we want and they look quite human-like. (At least moreso than alternatives like perlin or simplex noise)

The magic comes from being able to set multiple points for the curve to go through. This is done by picking 2 coordinates randomly in a limited area above and under the curve.

However, we don't want wonky looking cubic curves when using this method because nobody really moves their mouse that way, so only one side of the line is picked when generating random points.

When calculating how fast the mouse should be moving we use Fitts's Law to determine the amount of points we should be returning relative to the width of the element being clicked on and the distance between the mouse and the object.

To turn on logging, please set your DEBUG env variable like so: