Skip to content

Commit

Permalink
style: minor style updates (#139)
Browse files Browse the repository at this point in the history
* style: minor style updates

* style: ternary

* style: naming

* revert

* style: import type
  • Loading branch information
bvandercar-vt authored May 14, 2024
1 parent dde82e2 commit 2acbb4e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
11 changes: 7 additions & 4 deletions src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ export const overshoot = (coordinate: Vector, radius: number): Vector => {
export const bezierCurve = (
start: Vector,
finish: Vector,
overrideSpread?: number
/**
* Default is length from start to finish, clamped to 2 < x < 200
*/
spreadOverride?: number
): Bezier => {
// could be played around with
const min = 2
const max = 200
const MIN_SPREAD = 2
const MAX_SPREAD = 200
const vec = direction(start, finish)
const length = magnitude(vec)
const spread = overrideSpread ?? clamp(length, min, max)
const spread = spreadOverride ?? clamp(length, MIN_SPREAD, MAX_SPREAD)
const anchors = generateBezierAnchors(start, finish, spread)
return new Bezier(start, ...anchors, finish)
}
25 changes: 12 additions & 13 deletions src/mouse-helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// @ts-nocheck
import type { Page } from 'puppeteer'

// This injects a box into the page that moves with the mouse;
// Useful for debugging
/**
* This injects a box into the page that moves with the mouse.
* Useful for debugging.
*/
async function installMouseHelper (page: Page): Promise<void> {
await page.evaluateOnNewDocument(() => {
const attachListener = (): void => {
Expand Down Expand Up @@ -96,20 +97,18 @@ async function installMouseHelper (page: Page): Promise<void> {
},
true
)
/* eslint-disable */
function updateButtons(buttons) {
function updateButtons (buttons): void {
for (let i = 0; i < 5; i++) {
// @ts-ignore
box.classList.toggle("button-" + String(i), buttons & (1 << i));
box.classList.toggle('button-' + String(i), Boolean(buttons & (1 << i)))
}
}
};
if (document.readyState !== "loading") {
attachListener();
}
if (document.readyState !== 'loading') {
attachListener()
} else {
window.addEventListener("DOMContentLoaded", attachListener, false);
window.addEventListener('DOMContentLoaded', attachListener, false)
}
});
})
}

export default installMouseHelper;
export default installMouseHelper
30 changes: 15 additions & 15 deletions src/spoof.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ElementHandle, Page, BoundingBox, CDPSession } from 'puppeteer'
import type { ElementHandle, Page, BoundingBox, CDPSession } from 'puppeteer'
import debug from 'debug'
import {
Vector,
type Vector,
bezierCurve,
direction,
magnitude,
Expand Down Expand Up @@ -109,7 +109,7 @@ export interface GhostCursor {
getLocation: () => Vector
}

// Helper function to wait a specified number of milliseconds
/** Helper function to wait a specified number of milliseconds */
const delay = async (ms: number): Promise<void> => {
if (ms < 1) return
return await new Promise((resolve) => setTimeout(resolve, ms))
Expand All @@ -127,7 +127,7 @@ const fitts = (distance: number, width: number): number => {
return a + b * id
}

// Get a random point on a box
/** Get a random point on a box */
const getRandomBoxPoint = (
{ x, y, width, height }: BoundingBox,
options?: BoxOptions
Expand All @@ -150,10 +150,10 @@ const getRandomBoxPoint = (
}
}

// The function signature to access the internal CDP client changed in puppeteer 14.4.1
/** The function signature to access the internal CDP client changed in puppeteer 14.4.1 */
const getCDPClient = (page: any): CDPSession => typeof page._client === 'function' ? page._client() : page._client

// Get a random point on a browser window
/** Get a random point on a browser window */
export const getRandomPagePoint = async (page: Page): Promise<Vector> => {
const targetId: string = (page.target() as any)._targetId
const window = await getCDPClient(page).send(
Expand All @@ -168,7 +168,7 @@ export const getRandomPagePoint = async (page: Page): Promise<Vector> => {
})
}

// Using this method to get correct position of Inline elements (elements like <a>)
/** Using this method to get correct position of Inline elements (elements like `<a>`) */
const getElementBox = async (
page: Page,
element: ElementHandle,
Expand Down Expand Up @@ -224,16 +224,16 @@ export function path (start: Vector, end: BoundingBox | Vector, optionsOrSpread?
? { spreadOverride: optionsOrSpread }
: { ...optionsOrSpread }

const defaultWidth = 100
const minSteps = 25
const width = 'width' in end && end.width !== 0 ? end.width : defaultWidth
const DEFAULT_WIDTH = 100
const MIN_STEPS = 25
const width = 'width' in end && end.width !== 0 ? end.width : DEFAULT_WIDTH
const curve = bezierCurve(start, end, optionsResolved.spreadOverride)
const length = curve.length() * 0.8

const speed = optionsResolved.moveSpeed !== undefined && optionsResolved.moveSpeed > 0
? (25 / optionsResolved.moveSpeed)
: Math.random()
const baseTime = speed * minSteps
const baseTime = speed * MIN_STEPS
const steps = Math.ceil((Math.log2(fitts(length, width) + 1) + baseTime) * 3)
const re = curve.getLUT(steps)
return clampPositive(re)
Expand Down Expand Up @@ -307,8 +307,8 @@ export const createCursor = (
} = {}
): GhostCursor => {
// this is kind of arbitrary, not a big fan but it seems to work
const overshootSpread = 10
const overshootRadius = 120
const OVERSHOOT_SPREAD = 10
const OVERSHOOT_RADIUS = 120
let previous: Vector = start

// Initial state: mouse is not moving
Expand Down Expand Up @@ -481,15 +481,15 @@ export const createCursor = (
optionsResolved.overshootThreshold
)
const to = overshooting
? overshoot(destination, overshootRadius)
? overshoot(destination, OVERSHOOT_RADIUS)
: destination

await tracePath(path(previous, to, optionsResolved))

if (overshooting) {
const correction = path(to, { ...dimensions, ...destination }, {
...optionsResolved,
spreadOverride: overshootSpread
spreadOverride: OVERSHOOT_SPREAD
})

await tracePath(correction)
Expand Down

0 comments on commit 2acbb4e

Please sign in to comment.