import type { ElementHandle, Page, BoundingBox, CDPSession, Protocol } from 'puppeteer'; import { type Vector, type TimedVector } from './math'; import { installMouseHelper } from './mouse-helper'; export { installMouseHelper }; export interface BoxOptions { /** * Percentage of padding to be added inside the element when determining the target point. * Example: * - `0` = may be anywhere within the element. * - `100` = will always be center of element. * @default 0 */ readonly paddingPercentage?: number; /** * 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`. * @default undefined (random point) */ readonly destination?: Vector; } export interface GetElementOptions { /** * Time to wait for the selector to appear in milliseconds. * Default is to not wait for selector. */ readonly waitForSelector?: number; } export interface ScrollOptions { /** * Scroll speed. 0 to 100. 100 is instant. * @default 100 */ readonly scrollSpeed?: number; /** * Time to wait after scrolling. * @default 200 */ readonly scrollDelay?: number; } export interface ScrollIntoViewOptions extends ScrollOptions, GetElementOptions { /** * Scroll speed (when scrolling occurs). 0 to 100. 100 is instant. * @default 100 */ readonly scrollSpeed?: number; /** * Time to wait after scrolling (when scrolling occurs). * @default 200 */ readonly scrollDelay?: number; /** * Margin (in px) to add around the element when ensuring it is in the viewport. * (Does not take effect if CDP scroll fails.) * @default 0 */ readonly inViewportMargin?: number; } export interface MoveOptions extends BoxOptions, ScrollIntoViewOptions, Pick { /** * Delay after moving the mouse in milliseconds. If `randomizeMoveDelay=true`, delay is randomized from 0 to `moveDelay`. * @default 0 */ readonly moveDelay?: number; /** * Randomize delay between actions from `0` to `moveDelay`. See `moveDelay` docs. * @default true */ readonly randomizeMoveDelay?: boolean; /** * Maximum number of attempts to mouse-over the element. * @default 10 */ readonly maxTries?: 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 { /** * Delay before initiating the click action in milliseconds. * @default 0 */ readonly hesitate?: number; /** * Delay between mousedown and mouseup in milliseconds. * @default 0 */ readonly waitForClick?: number; /** * @default 2000 */ readonly moveDelay?: number; /** * @default "left" */ readonly button?: Protocol.Input.MouseButton; /** * @default 1 */ readonly clickCount?: number; } export interface PathOptions { /** * Override the spread of the generated path. */ readonly spreadOverride?: number; /** * Speed of mouse movement. * Default is random. */ readonly moveSpeed?: number; /** * Generate timestamps for each point in the path. */ readonly useTimestamps?: boolean; } export interface RandomMoveOptions extends Pick { /** * @default 2000 */ readonly moveDelay?: number; } export interface MoveToOptions extends PathOptions, Pick { /** * @default 0 */ readonly moveDelay?: number; } export type ScrollToDestination = Partial | 'top' | 'bottom' | 'left' | 'right'; export type MouseButtonOptions = Pick; /** * Default options for cursor functions. */ export interface DefaultOptions { /** * Default options for the `randomMove` function that occurs when `performRandomMoves=true` * @default RandomMoveOptions */ randomMove?: RandomMoveOptions; /** * Default options for the `move` function * @default MoveOptions */ move?: MoveOptions; /** * Default options for the `moveTo` function * @default MoveToOptions */ moveTo?: MoveToOptions; /** * Default options for the `click` function * @default ClickOptions */ click?: ClickOptions; /** * Default options for the `scrollIntoView`, `scrollTo`, and `scroll` functions * @default ScrollIntoViewOptions */ scroll?: ScrollOptions & ScrollIntoViewOptions; /** * Default options for the `getElement` function * @default GetElementOptions */ getElement?: GetElementOptions; } /** The function signature to access the internal CDP client changed in puppeteer 14.4.1 */ export declare const getCDPClient: (page: Page) => CDPSession; /** Get a random point on a browser window */ export declare const getRandomPagePoint: (page: Page) => Promise; /** Get correct position of Inline elements (elements like ``). Has fallback. */ export declare const getElementBox: (page: Page, element: ElementHandle, relativeToMainFrame?: boolean) => Promise; /** Generates a set of points for mouse movement between two coordinates. */ export declare function path(start: Vector, end: Vector | BoundingBox, /** * Additional options for generating the path. * Can also be a number which will set `spreadOverride`. */ options?: number | PathOptions): Vector[] | TimedVector[]; export declare class GhostCursor { readonly page: Page; /** Default options for cursor functions. */ defaultOptions: DefaultOptions; /** Location of the cursor. */ private location; /** Whether mouse is moving via `click`, `move`, `moveTo`, etc. (does not include random movements). Initial state: not moving. */ private moving; /** Make the cursor no longer visible. Defined only if `visible=true` was passed, or `installMouseHelper` ran later. */ private removeMouseHelperFn; private static readonly OVERSHOOT_SPREAD; private static readonly OVERSHOOT_RADIUS; constructor(page: Page, { start, performRandomMoves, defaultOptions, visible }?: { /** * Cursor start position. * @default { x: 0, y: 0 } */ start?: Vector; /** * Initially perform random movements. * If `move`,`click`, etc. is performed, these random movements end. * @default false */ performRandomMoves?: boolean; /** * Set custom default options for cursor action functions. * Default values are described in the type JSdocs. */ defaultOptions?: DefaultOptions; /** * Whether cursor should be made visible using `installMouseHelper`. * @default false */ visible?: boolean; }); /** * Install mouse helper (visible cursor). */ installMouseHelper(): Promise; /** * Make the cursor no longer visible. * Only has an effect if `visible=true` was passed, or this.installMouseHelper performed manually. */ removeMouseHelper(): Promise; /** Move the mouse to a point, getting the vectors via `path(previous, newLocation, options)` */ private moveMouse; /** Start random mouse movements. Function recursively calls itself. */ private randomMove; private mouseButtonAction; /** Mouse button down */ mouseDown(options?: MouseButtonOptions): Promise; /** Mouse button up (release) */ mouseUp(options?: MouseButtonOptions): Promise; /** Toggles random mouse movements on or off. */ toggleRandomMove(random: boolean): void; /** Get current location of the cursor. */ getLocation(): Vector; /** * Simulates a mouse click at the specified selector or element. * Default is to click at current location, don't move. */ click(selector?: string | ElementHandle, /** @default defaultOptions.click */ options?: ClickOptions): Promise; /** Moves the mouse to the specified selector or element. */ move(selector: string | ElementHandle, /** @default defaultOptions.move */ options?: MoveOptions): Promise; /** Moves the mouse to the specified destination point. */ moveTo(destination: Vector, /** @default defaultOptions.moveTo */ options?: MoveToOptions): Promise; /** Moves the mouse by a specified amount */ moveBy(delta: Partial, options?: MoveToOptions): Promise; /** Scrolls the element into view. If already in view, no scroll occurs. */ scrollIntoView(selector: string | ElementHandle, /** @default defaultOptions.scroll */ options?: ScrollIntoViewOptions): Promise; /** Scrolls the page the distance set by `delta`. */ scroll(delta: Partial, /** @default defaultOptions.scroll */ options?: ScrollOptions): Promise; /** Scrolls to the specified destination point. */ scrollTo(destination: ScrollToDestination, /** @default defaultOptions.scroll */ options?: ScrollOptions): Promise; /** Gets the element via a selector. Can use an XPath. */ getElement(selector: string | ElementHandle, /** @default defaultOptions.getElement */ options?: GetElementOptions): Promise>; } /** * @deprecated * TODO: Remove on next major version change. Prefer to just do `new GhostCursor` instead of this function. * Is here because removing would be breaking. */ export declare const createCursor: (page: Page, /** * Cursor start position. * @default { x: 0, y: 0 } */ start?: Vector, /** * Initially perform random movements. * If `move`,`click`, etc. is performed, these random movements end. * @default false */ performRandomMoves?: boolean, /** * Default options for cursor functions. */ defaultOptions?: DefaultOptions, /** * Whether cursor should be made visible using `installMouseHelper`. * @default false */ visible?: boolean) => GhostCursor;