Skip to content

Commit

Permalink
feat: overshootThreshold option (#127)
Browse files Browse the repository at this point in the history
* feat: allow overshootThreshold option

* feat: readme

* style: docstring
  • Loading branch information
bvandercar-vt authored May 8, 2024
1 parent 6f09e31 commit 788f692
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Moves the mouse to the specified selector or element.
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `2000`.
- `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`.

#### `moveTo(destination: Vector): Promise<void>`

Expand Down
26 changes: 22 additions & 4 deletions src/spoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface MoveOptions extends BoxOptions {
* Default is random.
*/
readonly moveSpeed?: number
/**
* Distance from current location to destination that triggers overshoot to
* occur. (Below this distance, no overshoot will occur).
* @default 500
*/
readonly overshootThreshold?: number
}

export interface ClickOptions extends MoveOptions {
Expand Down Expand Up @@ -218,9 +224,8 @@ const clampPositive = (vectors: Vector[]): Vector[] => {
})
}

const overshootThreshold = 500
const shouldOvershoot = (a: Vector, b: Vector): boolean =>
magnitude(direction(a, b)) > overshootThreshold
const shouldOvershoot = (a: Vector, b: Vector, threshold: number): boolean =>
magnitude(direction(a, b)) > threshold

const intersectsElement = (vec: Vector, box: BoundingBox): boolean => {
return (
Expand All @@ -247,7 +252,16 @@ const boundingBoxWithFallback = async (

export const createCursor = (
page: Page,
/**
* Cursor start position.
* @default { x: 0, y: 0 }
*/
start: Vector = origin,
/**
* Initially perform random movements.
* If `move`,`click`, etc. is performed, these random movements end.
* @default false
*/
performRandomMoves: boolean = false
): GhostCursor => {
// this is kind of arbitrary, not a big fan but it seems to work
Expand Down Expand Up @@ -383,7 +397,11 @@ export const createCursor = (
const { height, width } = box
const destination = getRandomBoxPoint(box, options)
const dimensions = { height, width }
const overshooting = shouldOvershoot(previous, destination)
const overshooting = shouldOvershoot(
previous,
destination,
options?.overshootThreshold ?? 500
)
const to = overshooting
? overshoot(destination, overshootRadius)
: destination
Expand Down

0 comments on commit 788f692

Please sign in to comment.