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: options for moveTo function #133

Merged
merged 2 commits into from
May 9, 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Creates the ghost cursor. Returns cursor action functions.
- **page:** Puppeteer `page`.
- **start (optional):** Cursor start position. Default is `{ x: 0, y: 0 }`.
- **performRandomMoves (optional):** Initially perform random movements. Default is `false`.
- **defaultOptions (optional):** Set custom default options for `click`, `move`, and `randomMove` functions. Default values are described below.
- **defaultOptions (optional):** Set custom default options for `click`, `move`, `moveTo`, and `randomMove` functions. Default values are described below.

#### `toggleRandomMove(random: boolean): void`

Expand Down Expand Up @@ -111,11 +111,13 @@ Moves the mouse to the specified selector or element.
- `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`.

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

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.

#### `getLocation(): Vector`

Expand Down
20 changes: 16 additions & 4 deletions src/spoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ export interface PathOptions {
readonly moveSpeed?: number
}

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

export interface MoveToOptions extends PathOptions {}

export interface GhostCursor {
toggleRandomMove: (random: boolean) => void
click: (
Expand All @@ -92,7 +94,7 @@ export interface GhostCursor {
selector: string | ElementHandle,
options?: MoveOptions
) => Promise<void>
moveTo: (destination: Vector) => Promise<void>
moveTo: (destination: Vector, options?: MoveToOptions) => Promise<void>
getLocation: () => Vector
}

Expand Down Expand Up @@ -283,6 +285,11 @@ export const createCursor = (
* @default MoveOptions
*/
move?: MoveOptions
/**
* Default options for the `moveTo` function
* @default MoveToOptions
*/
moveTo?: MoveToOptions
/**
* Default options for the `click` function
* @default ClickOptions
Expand Down Expand Up @@ -494,10 +501,15 @@ export const createCursor = (

await delay(Math.random() * optionsResolved.moveDelay)
},
async moveTo (destination: Vector): Promise<void> {
async moveTo (destination: Vector, options?: MoveToOptions): Promise<void> {
const optionsResolved = {
...defaultOptions?.moveTo,
...options
} satisfies MoveToOptions

const wasRandom = !moving
actions.toggleRandomMove(false)
await tracePath(path(previous, destination))
await tracePath(path(previous, destination, optionsResolved))
actions.toggleRandomMove(wasRandom)
}
}
Expand Down