Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: delay when < 1, simplify object passing #129

Merged
merged 3 commits into from
May 8, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 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> =>
const delay = async (ms: number): Promise<void> => {
if (ms < 1) return
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to setTimeout docs, if ms < 1, it uses 1. So we don't want to use setTimeout if 0 is passed here.

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,9 +284,7 @@ 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplify passing options

previous = rand
}
if (options?.moveDelay !== undefined && options.moveDelay >= 0) {
Expand Down Expand Up @@ -318,13 +318,9 @@ 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)
Expand Down Expand Up @@ -400,14 +396,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