Skip to content

Commit

Permalink
style: delay when < 1, simplify object passing (#129)
Browse files Browse the repository at this point in the history
* style: no need to destructure object

* style: whitespace

* style: default
  • Loading branch information
bvandercar-vt authored May 8, 2024
1 parent 476e21d commit 6f09e31
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/spoof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ export interface GhostCursor {
}

// Helper function to wait a specified number of milliseconds
const delay = async (ms: number): Promise<void> =>
await new Promise((resolve) => setTimeout(resolve, ms))
const delay = async (ms: number): Promise<void> => {
if (ms < 1) return
return await new Promise((resolve) => setTimeout(resolve, ms))
}

/**
* Calculate the amount of time needed to move from (x1, y1) to (x2, y2)
Expand Down Expand Up @@ -282,16 +284,10 @@ export const createCursor = (
try {
if (!moving) {
const rand = await getRandomPagePoint(page)
await tracePath(path(previous, rand, {
moveSpeed: options?.moveSpeed
}), true)
await tracePath(path(previous, rand, options), true)
previous = rand
}
if (options?.moveDelay !== undefined && options.moveDelay >= 0) {
await delay(Math.random() * options.moveDelay)
} else {
await delay(Math.random() * 2000) // 2s by default
}
await delay(Math.random() * (options?.moveDelay ?? 2000))
randomMove().then(
(_) => {},
(_) => {}
Expand All @@ -318,23 +314,15 @@ export const createCursor = (
}

try {
if (options?.hesitate !== undefined) {
await delay(options.hesitate)
}
await delay(options?.hesitate ?? 0)
await page.mouse.down()
if (options?.waitForClick !== undefined) {
await delay(options.waitForClick)
}
await delay(options?.waitForClick ?? 0)
await page.mouse.up()
} catch (error) {
log('Warning: could not click mouse, error message:', error)
}

if (options?.moveDelay !== undefined && options.moveDelay >= 0) {
await delay(Math.random() * options.moveDelay)
} else {
await delay(Math.random() * 2000) // 2s by default
}
await delay(Math.random() * (options?.moveDelay ?? 2000))

actions.toggleRandomMove(true)
},
Expand Down Expand Up @@ -400,14 +388,12 @@ export const createCursor = (
? overshoot(destination, overshootRadius)
: destination

await tracePath(path(previous, to, {
moveSpeed: options?.moveSpeed
}))
await tracePath(path(previous, to, options))

if (overshooting) {
const correction = path(to, { ...dimensions, ...destination }, {
spreadOverride: overshootSpread,
moveSpeed: options?.moveSpeed
...options,
spreadOverride: overshootSpread
})

await tracePath(correction)
Expand Down

0 comments on commit 6f09e31

Please sign in to comment.