-
Notifications
You must be signed in to change notification settings - Fork 159
feat: Split core and puppeteer features #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,17 @@ | |||||||||||||||||||||||||||||||
| "version": "1.4.1", | ||||||||||||||||||||||||||||||||
| "description": "Move your mouse like a human in puppeteer or generate realistic movements on any 2D plane", | ||||||||||||||||||||||||||||||||
| "repository": "https://github.com/Xetera/ghost-cursor", | ||||||||||||||||||||||||||||||||
| "exports": { | ||||||||||||||||||||||||||||||||
| ".": { | ||||||||||||||||||||||||||||||||
| "types": "./lib/spoof.d.ts", | ||||||||||||||||||||||||||||||||
| "default": "./lib/spoof.js" | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| "./core": { | ||||||||||||||||||||||||||||||||
| "types": "./lib/core.d.ts", | ||||||||||||||||||||||||||||||||
| "default": "./lib/core.js" | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+14
|
||||||||||||||||||||||||||||||||
| }, | |
| "./core": { | |
| "types": "./lib/core.d.ts", | |
| "default": "./lib/core.js" | |
| }, | |
| }, | |
| "./spoof": { | |
| "types": "./lib/spoof.d.ts", | |
| "default": "./lib/spoof.js" | |
| }, | |
| "./core": { | |
| "types": "./lib/core.d.ts", | |
| "default": "./lib/core.js" | |
| }, | |
| "./lib/*": "./lib/*", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think people imported these directly, since they are not documented. Right, @regseb?
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { | ||
| type Vector, | ||
| type TimedVector, | ||
| type Rectangle, | ||
| bezierCurve, | ||
| bezierCurveSpeed, | ||
| extrapolate | ||
| } from './math' | ||
|
|
||
| export interface PathOptions { | ||
| /** | ||
| * Override the spread of the generated path. | ||
| */ | ||
| readonly spreadOverride?: number | ||
| /** | ||
| * Speed of mouse movement. | ||
| * Default is random. | ||
| */ | ||
| readonly moveSpeed?: number | ||
|
|
||
| /** | ||
| * Generate timestamps for each point in the path. | ||
| */ | ||
| readonly useTimestamps?: boolean | ||
| } | ||
|
|
||
| /** | ||
| * Calculate the amount of time needed to move from (x1, y1) to (x2, y2) | ||
| * given the width of the element being clicked on | ||
| * https://en.wikipedia.org/wiki/Fitts%27s_law | ||
| */ | ||
| const fitts = (distance: number, width: number): number => { | ||
| const a = 0 | ||
| const b = 2 | ||
| const id = Math.log2(distance / width + 1) | ||
| return a + b * id | ||
| } | ||
|
|
||
| /** Generates a set of points for mouse movement between two coordinates. */ | ||
| export function path ( | ||
| start: Vector, | ||
| end: Vector | Rectangle, | ||
| /** | ||
| * Additional options for generating the path. | ||
| * Can also be a number which will set `spreadOverride`. | ||
| */ | ||
| // TODO: remove number arg in next major version change, fine to just allow `spreadOverride` in object. | ||
| options?: number | PathOptions): Vector[] | TimedVector[] { | ||
| const optionsResolved: PathOptions = typeof options === 'number' | ||
| ? { spreadOverride: options } | ||
| : { ...options } | ||
|
|
||
| const DEFAULT_WIDTH = 100 | ||
| const MIN_STEPS = 25 | ||
| const width = 'width' in end && end.width !== 0 ? end.width : DEFAULT_WIDTH | ||
| const curve = bezierCurve(start, end, optionsResolved.spreadOverride) | ||
| const length = curve.length() * 0.8 | ||
|
|
||
| const speed = optionsResolved.moveSpeed !== undefined && optionsResolved.moveSpeed > 0 | ||
| ? (25 / optionsResolved.moveSpeed) | ||
| : Math.random() | ||
| const baseTime = speed * MIN_STEPS | ||
| const steps = Math.ceil((Math.log2(fitts(length, width) + 1) + baseTime) * 3) | ||
| const re = curve.getLUT(steps) | ||
| return clampPositive(re, optionsResolved) | ||
| } | ||
|
Comment on lines
+39
to
+66
|
||
|
|
||
| const clampPositive = (vectors: Vector[], options?: PathOptions): Vector[] | TimedVector[] => { | ||
| const clampedVectors = vectors.map((vector) => ({ | ||
| x: Math.max(0, vector.x), | ||
| y: Math.max(0, vector.y) | ||
| })) | ||
|
|
||
| return options?.useTimestamps === true ? generateTimestamps(clampedVectors, options) : clampedVectors | ||
| } | ||
|
|
||
| const generateTimestamps = (vectors: Vector[], options?: PathOptions): TimedVector[] => { | ||
| const speed = options?.moveSpeed ?? (Math.random() * 0.5 + 0.5) | ||
| const timeToMove = (P0: Vector, P1: Vector, P2: Vector, P3: Vector, samples: number): number => { | ||
| let total = 0 | ||
| const dt = 1 / samples | ||
|
|
||
| for (let t = 0; t < 1; t += dt) { | ||
| const v1 = bezierCurveSpeed(t * dt, P0, P1, P2, P3) | ||
| const v2 = bezierCurveSpeed(t, P0, P1, P2, P3) | ||
| total += (v1 + v2) * dt / 2 | ||
| } | ||
|
|
||
| return Math.round(total / speed) | ||
| } | ||
|
|
||
| const timedVectors: TimedVector[] = [] | ||
|
|
||
| for (let i = 0; i < vectors.length; i++) { | ||
| if (i === 0) { | ||
| timedVectors.push({ ...vectors[i], timestamp: Date.now() }) | ||
| } else { | ||
| const P0 = vectors[i - 1] | ||
| const P1 = vectors[i] | ||
| const P2 = i + 1 < vectors.length ? vectors[i + 1] : extrapolate(P0, P1) | ||
| const P3 = i + 2 < vectors.length ? vectors[i + 2] : extrapolate(P1, P2) | ||
| const time = timeToMove(P0, P1, P2, P3, vectors.length) | ||
|
|
||
| timedVectors.push({ | ||
| ...vectors[i], | ||
| timestamp: timedVectors[i - 1].timestamp + time | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return timedVectors | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR metadata/issue link indicates this should fix TS users who only import
path()without havingpuppeteerinstalled, but the root export (".") still points to./lib/spoof.d.ts, which importspuppeteertypes. That meansimport { path } from 'ghost-cursor'will still fail unless consumers switch toghost-cursor/core. Consider either (a) making the root export point tocoreand adding an explicit./spoofsubpath for Puppeteer-specific APIs, or (b) updating public docs (README/changelog) to direct non-Puppeteer users toghost-cursor/coreso the linked issue is actually resolved for readers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hat do you think @regseb?