Skip to content

Commit

Permalink
Merge branch 'master' into moveTo-options
Browse files Browse the repository at this point in the history
  • Loading branch information
bvandercar-vt committed May 9, 2024
2 parents d7844c9 + f9337a2 commit dfa5a29
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ Moves the mouse to the specified destination point.
- **options (optional):** Additional options for moving.
- `moveSpeed (number):` Speed of mouse movement. Default is random.

#### `getLocation(): Vector`

Get current location of the cursor.

### Other Utility Methods

#### `installMouseHelper(page: Page): Promise<void>`
Expand Down
8 changes: 2 additions & 6 deletions src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ export const bezierCurve = (
const max = 200
const vec = direction(start, finish)
const length = magnitude(vec)
const spread = clamp(length, min, max)
const anchors = generateBezierAnchors(
start,
finish,
overrideSpread ?? spread
)
const spread = overrideSpread ?? clamp(length, min, max)
const anchors = generateBezierAnchors(start, finish, spread)
return new Bezier(start, ...anchors, finish)
}
20 changes: 15 additions & 5 deletions src/spoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface ClickOptions extends MoveOptions {
*/
readonly waitForClick?: number
/**
* Delay after performing the click in milliseconds.
* @extends moveDelay
* @default 2000
*/
readonly moveDelay?: number
Expand All @@ -76,7 +76,7 @@ export interface PathOptions {

export interface RandomMoveOptions extends Pick<MoveOptions, 'moveDelay' | 'moveSpeed'> {
/**
* Delay after performing the click in milliseconds.
* @extends moveDelay
* @default 2000
*/
readonly moveDelay?: number
Expand All @@ -95,6 +95,7 @@ export interface GhostCursor {
options?: MoveOptions
) => Promise<void>
moveTo: (destination: Vector, options?: MoveToOptions) => Promise<void>
getLocation: () => Vector
}

// Helper function to wait a specified number of milliseconds
Expand Down Expand Up @@ -354,6 +355,10 @@ export const createCursor = (
moving = !random
},

getLocation (): Vector {
return previous
},

async click (
selector?: string | ElementHandle,
options?: ClickOptions
Expand All @@ -366,6 +371,7 @@ export const createCursor = (
...options
} satisfies ClickOptions

const wasRandom = !moving
actions.toggleRandomMove(false)

if (selector !== undefined) {
Expand All @@ -374,7 +380,6 @@ export const createCursor = (
// apply moveDelay after click, but not after actual move
moveDelay: 0
})
actions.toggleRandomMove(false)
}

try {
Expand All @@ -388,7 +393,7 @@ export const createCursor = (

await delay(Math.random() * optionsResolved.moveDelay)

actions.toggleRandomMove(true)
actions.toggleRandomMove(wasRandom)
},
async move (
selector: string | ElementHandle,
Expand All @@ -402,6 +407,8 @@ export const createCursor = (
...options
} satisfies MoveOptions

const wasRandom = !moving

const go = async (iteration: number): Promise<void> => {
if (iteration > (optionsResolved.maxTries)) {
throw Error('Could not mouse-over element within enough tries')
Expand Down Expand Up @@ -490,6 +497,8 @@ export const createCursor = (
}
await go(0)

actions.toggleRandomMove(wasRandom)

await delay(Math.random() * optionsResolved.moveDelay)
},
async moveTo (destination: Vector, options?: MoveToOptions): Promise<void> {
Expand All @@ -498,9 +507,10 @@ export const createCursor = (
...options
} satisfies MoveToOptions

const wasRandom = !moving
actions.toggleRandomMove(false)
await tracePath(path(previous, destination, optionsResolved))
actions.toggleRandomMove(true)
actions.toggleRandomMove(wasRandom)
}
}

Expand Down
20 changes: 10 additions & 10 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"skipLibCheck": true,
"lib": ["es2015", "dom"],
"outDir": "./lib" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"outDir": "./lib" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"declaration": true,
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
"removeComments": true /* Do not emit comments to output. */,
"removeComments": false /* Keep comments-- want JSDocs in types */,
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
"downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
"downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
Expand All @@ -38,7 +38,7 @@
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
Expand All @@ -50,7 +50,7 @@
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["src/**/*"],
"exclude": ["node_modules"],
Expand Down

0 comments on commit dfa5a29

Please sign in to comment.