Skip to content

Commit a952ea2

Browse files
feat: element specific destination point (#157)
* feat: element specific destination point * from top-left * add box * style: comment
1 parent ef4a8f9 commit a952ea2

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ Moves the mouse to the specified selector or element.
126126

127127
- **selector:** CSS selector or ElementHandle to identify the target element.
128128
- **options (optional):** Additional options for moving. **Extends the `options` of the `scrollIntoView` function (below)**
129-
- `paddingPercentage (number):` Percentage of padding to be added inside the element. Default is `0` (may move to anywhere within the element). `100` will always move to center of element.
129+
- `paddingPercentage (number):` Percentage of padding to be added inside the element when determining the target point. Default is `0` (may move to anywhere within the element). `100` will always move to center of element.
130+
- `destination (Vector):` Destination to move the cursor to, relative to the top-left corner of the element. If specified, `paddingPercentage` is not used. If not specified (default), destination is random point within the `paddingPercentage`.
130131
- `waitForSelector (number):` Time to wait for the selector to appear in milliseconds. Default is to not wait for selector.
131132
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `0`. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`.
132133
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.

src/spoof.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
magnitude,
1010
origin,
1111
overshoot,
12+
add,
1213
clamp,
1314
scale
1415
} from './math'
@@ -25,6 +26,13 @@ export interface BoxOptions {
2526
* @default 0
2627
*/
2728
readonly paddingPercentage?: number
29+
/**
30+
* Destination to move the cursor to, relative to the top-left corner of the element.
31+
* If specified, `paddingPercentage` is not used.
32+
* If not specified (default), destination is random point within the `paddingPercentage`.
33+
* @default undefined (random point)
34+
*/
35+
readonly destination?: Vector
2836
}
2937

3038
export interface ScrollOptions {
@@ -159,7 +167,7 @@ const fitts = (distance: number, width: number): number => {
159167
/** Get a random point on a box */
160168
const getRandomBoxPoint = (
161169
{ x, y, width, height }: BoundingBox,
162-
options?: BoxOptions
170+
options?: Pick<BoxOptions, 'paddingPercentage'>
163171
): Vector => {
164172
let paddingWidth = 0
165173
let paddingHeight = 0
@@ -540,7 +548,9 @@ export const createCursor = (
540548

541549
const box = await boundingBoxWithFallback(page, elem)
542550
const { height, width } = box
543-
const destination = getRandomBoxPoint(box, optionsResolved)
551+
const destination = (optionsResolved.destination !== undefined)
552+
? add(box, optionsResolved.destination)
553+
: getRandomBoxPoint(box, optionsResolved)
544554
const dimensions = { height, width }
545555
const overshooting = shouldOvershoot(
546556
previous,

0 commit comments

Comments
 (0)