Skip to content

Commit f2c1d2a

Browse files
committed
check isIntersectingViewport
1 parent c12fca4 commit f2c1d2a

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ Toggles random mouse movements on or off.
115115
Simulates a mouse click at the specified selector or element.
116116

117117
- **selector (optional):** CSS selector or ElementHandle to identify the target element.
118-
- **options (optional):** Additional options for clicking.
118+
- **options (optional):** Additional options for clicking. **Extends the `options` of the `move` function (below)**
119119
- `hesitate (number):` Delay before initiating the click action in milliseconds. Default is `0`.
120120
- `waitForClick (number):` Delay between mousedown and mouseup in milliseconds. Default is `0`.
121121
- `moveDelay (number):` Delay after moving the mouse in milliseconds. Default is `2000`. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`.
122-
- `randomizeMoveDelay (boolean):` Randomize delay between actions from `0` to `moveDelay`. Default is `true`.
123122

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

@@ -134,6 +133,8 @@ Moves the mouse to the specified selector or element.
134133
- `maxTries (number):` Maximum number of attempts to mouse-over the element. Default is `10`.
135134
- `moveSpeed (number):` Speed of mouse movement. Default is random.
136135
- `overshootThreshold (number):` Distance from current location to destination that triggers overshoot to occur. (Below this distance, no overshoot will occur). Default is `500`.
136+
- `scrollBehavior (ScrollBehavior):` Scroll behavior when target element is outside the visible window. Default is `undefined` (browser default).
137+
- `scrollWait (number):` Time to wait after scrolling (when scrolling occurs due to target element being outside the visible window). Default is `200`.
137138

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

src/__test__/spoof.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const cursorDefaultOptions = {
1212
moveDelay: 0,
1313
moveSpeed: 99,
1414
hesitate: 0,
15-
waitForClick: 0
15+
waitForClick: 0,
16+
scrollWait: 0
1617
} as const satisfies ClickOptions
1718

1819
describe('Mouse movements', () => {

src/spoof.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export interface MoveOptions extends BoxOptions, Pick<PathOptions, 'moveSpeed'>
5959
readonly scrollBehavior?: ScrollBehavior
6060
/**
6161
* Time to wait after scrolling (when scrolling occurs due to target element being outside the visible window)
62-
* @default 2000
62+
* @default 200
6363
*/
6464
readonly scrollWait?: number
6565
}
@@ -76,7 +76,7 @@ export interface ClickOptions extends MoveOptions {
7676
*/
7777
readonly waitForClick?: number
7878
/**
79-
* @default 200
79+
* @default 2000
8080
*/
8181
readonly moveDelay?: number
8282
}
@@ -525,17 +525,20 @@ export const createCursor = (
525525
}
526526

527527
// Make sure the object is in view
528-
const objectId = elem.remoteObject().objectId
529-
if (objectId !== undefined) {
530-
const scrollElemIntoView = async (): Promise<void> => await elem.evaluate((e, scrollBehavior) => e.scrollIntoView({
531-
block: 'center',
532-
behavior: scrollBehavior
533-
}), optionsResolved.scrollBehavior)
528+
if (!(await elem.isIntersectingViewport())) {
529+
const scrollElemIntoView = async (): Promise<void> =>
530+
await elem.evaluate((e, scrollBehavior) => e.scrollIntoView({
531+
block: 'center',
532+
behavior: scrollBehavior
533+
}), optionsResolved.scrollBehavior)
534534

535535
if (optionsResolved.scrollBehavior !== undefined) {
536+
// DOM.scrollIntoViewIfNeeded is instant scroll, so do the JS scroll if scrollBehavior passed
536537
await scrollElemIntoView()
537538
} else {
538539
try {
540+
const { objectId } = elem.remoteObject()
541+
if (objectId === undefined) throw new Error()
539542
await getCDPClient(page).send('DOM.scrollIntoViewIfNeeded', {
540543
objectId
541544
})
@@ -545,7 +548,6 @@ export const createCursor = (
545548
await scrollElemIntoView()
546549
}
547550
}
548-
549551
await delay(optionsResolved.scrollWait)
550552
}
551553

0 commit comments

Comments
 (0)