Skip to content

Commit

Permalink
feat: options for moveTo (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvandercar-vt authored May 9, 2024
1 parent f9337a2 commit 58a6984
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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

0 comments on commit 58a6984

Please sign in to comment.