Skip to content
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

feat: add moveDelay option to moveTo ; allow disabling randomization of moveDelay #135

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ Simulates a mouse click at the specified selector or element.
- **options (optional):** Additional options for clicking.
- `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`.
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `2000`. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`.
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.

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

Expand All @@ -106,7 +107,8 @@ Moves the mouse to the specified selector or element.
- **options (optional):** Additional options for moving.
- `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`.
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `0`. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`.
- `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`. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`.
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.

#### `getLocation(): Vector`

Expand Down
33 changes: 25 additions & 8 deletions src/spoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ export interface MoveOptions extends BoxOptions, Pick<PathOptions, 'moveSpeed'>
*/
readonly waitForSelector?: number
/**
* Delay after moving the mouse in milliseconds.
* Delay after moving the mouse in milliseconds. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`.
* @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