Skip to content

Commit

Permalink
feat: allow disabling randomization of moveDelay
Browse files Browse the repository at this point in the history
  • Loading branch information
bvandercar-vt committed May 9, 2024
1 parent 2cdc5c8 commit 4065a8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Simulates a mouse click at the specified selector or element.
- `hesitate (number):` Delay before initiating the click action in milliseconds. Default is `0`.
- `waitForClick (number):` Delay between mousedown and mouseup in milliseconds. Default is `0`.
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `2000`.
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.

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

Expand All @@ -107,6 +108,7 @@ Moves the mouse to the specified selector or element.
- `paddingPercentage (number):` Percentage of padding to be added around the element. Default is `0`.
- `waitForSelector (number):` Time to wait for the selector to appear in milliseconds. Default is to not wait for selector.
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `0`.
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.
- `maxTries (number):` Maximum number of attempts to mouse-over the element. Default is `10`.
- `moveSpeed (number):` Speed of mouse movement. Default is random.
- `overshootThreshold (number):` Distance from current location to destination that triggers overshoot to occur. (Below this distance, no overshoot will occur). Default is `500`.
Expand All @@ -118,6 +120,8 @@ Moves the mouse to the specified destination point.
- **destination:** An object with `x` and `y` coordinates representing the target position. For example, `{ x: 500, y: 300 }`.
- **options (optional):** Additional options for moving.
- `moveSpeed (number):` Speed of mouse movement. Default is random.
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `0`.
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.

#### `getLocation(): Vector`

Expand Down
31 changes: 24 additions & 7 deletions src/spoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export interface MoveOptions extends BoxOptions, Pick<PathOptions, 'moveSpeed'>
* @default 0
*/
readonly moveDelay?: number
/**
* Randomize delay between actions from `0` to `moveDelay`. See `moveDelay` docs.
* @default true
*/
readonly randomizeMoveDelay?: boolean
/**
* Maximum number of attempts to mouse-over the element.
* @default 10
Expand All @@ -56,7 +61,6 @@ export interface ClickOptions extends MoveOptions {
*/
readonly waitForClick?: number
/**
* @extends moveDelay
* @default 2000
*/
readonly moveDelay?: number
Expand All @@ -74,15 +78,19 @@ export interface PathOptions {
readonly moveSpeed?: number
}

export interface RandomMoveOptions extends Pick<MoveOptions, 'moveDelay' | 'moveSpeed'> {
export interface RandomMoveOptions extends Pick<MoveOptions, 'moveDelay' | 'randomizeMoveDelay' | 'moveSpeed'> {
/**
* @extends moveDelay
* @default 2000
*/
readonly moveDelay?: number
}

export interface MoveToOptions extends PathOptions {}
export interface MoveToOptions extends PathOptions, Pick<MoveOptions, 'moveDelay' | 'randomizeMoveDelay'> {
/**
* @default 0
*/
readonly moveDelay?: number
}

export interface GhostCursor {
toggleRandomMove: (random: boolean) => void
Expand Down Expand Up @@ -330,6 +338,7 @@ export const createCursor = (
const randomMove = async (options?: RandomMoveOptions): Promise<void> => {
const optionsResolved = {
moveDelay: 2000,
randomizeMoveDelay: true,
...defaultOptions?.randomMove,
...options
} satisfies RandomMoveOptions
Expand All @@ -340,7 +349,7 @@ export const createCursor = (
await tracePath(path(previous, rand, optionsResolved), true)
previous = rand
}
await delay(Math.random() * optionsResolved.moveDelay)
await delay(optionsResolved.moveDelay * (optionsResolved.randomizeMoveDelay ? Math.random() : 1))
randomMove(options).then(
(_) => {},
(_) => {}
Expand All @@ -367,6 +376,7 @@ export const createCursor = (
moveDelay: 2000,
hesitate: 0,
waitForClick: 0,
randomizeMoveDelay: true,
...defaultOptions?.click,
...options
} satisfies ClickOptions
Expand All @@ -391,10 +401,11 @@ export const createCursor = (
log('Warning: could not click mouse, error message:', error)
}

await delay(Math.random() * optionsResolved.moveDelay)
await delay(optionsResolved.moveDelay * (optionsResolved.randomizeMoveDelay ? Math.random() : 1))

actions.toggleRandomMove(wasRandom)
},

async move (
selector: string | ElementHandle,
options?: MoveOptions
Expand All @@ -403,6 +414,7 @@ export const createCursor = (
moveDelay: 0,
maxTries: 10,
overshootThreshold: 500,
randomizeMoveDelay: true,
...defaultOptions?.move,
...options
} satisfies MoveOptions
Expand Down Expand Up @@ -499,10 +511,13 @@ export const createCursor = (

actions.toggleRandomMove(wasRandom)

await delay(Math.random() * optionsResolved.moveDelay)
await delay(optionsResolved.moveDelay * (optionsResolved.randomizeMoveDelay ? Math.random() : 1))
},

async moveTo (destination: Vector, options?: MoveToOptions): Promise<void> {
const optionsResolved = {
moveDelay: 0,
randomizeMoveDelay: true,
...defaultOptions?.moveTo,
...options
} satisfies MoveToOptions
Expand All @@ -511,6 +526,8 @@ export const createCursor = (
actions.toggleRandomMove(false)
await tracePath(path(previous, destination, optionsResolved))
actions.toggleRandomMove(wasRandom)

await delay(optionsResolved.moveDelay * (optionsResolved.randomizeMoveDelay ? Math.random() : 1))
}
}

Expand Down

0 comments on commit 4065a8e

Please sign in to comment.