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

Implemented a feature that allows to enable/disable logging when needed. #107

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ that way, so only one side of the line is picked when generating random points.
When calculating how fast the mouse should be moving we use <a href="https://en.wikipedia.org/wiki/Fitts%27s_law">Fitts's Law</a>
to determine the amount of points we should be returning relative to the width of the element being clicked on and the distance
between the mouse and the object.

## To turn on logging, please set your DEBUG env variable like so:

- OSX: `DEBUG="ghost-cursor:*"`
- Linux: `DEBUG="ghost-cursor:*"`
- Windows CMD: `set DEBUG=ghost-cursor:*`
- Windows PowerShell: `$env:DEBUG = "ghost-cursor:*"`
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
],
"dependencies": {
"@types/bezier-js": "4",
"bezier-js": "^6.1.3"
"bezier-js": "^6.1.3",
"debug": "^4.3.4"
},
"devDependencies": {
"@swc/core": "^1.2.194",
"@swc/jest": "^0.2.21",
"@types/debug": "^4.1.9",
"@types/jest": "29",
"husky": "8",
"jest": "29",
Expand Down
13 changes: 8 additions & 5 deletions src/spoof.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ElementHandle, Page, BoundingBox, CDPSession } from 'puppeteer'
import debug from 'debug'
import {
Vector,
bezierCurve,
Expand All @@ -9,6 +10,8 @@ import {
} from './math'
export { default as installMouseHelper } from './mouse-helper'

const log = debug('ghost-cursor')

export interface BoxOptions {
readonly paddingPercentage?: number
}
Expand Down Expand Up @@ -143,7 +146,7 @@ const getElementBox = async (

return elementBox
} catch (_) {
console.debug('Quads not found, trying regular boundingBox')
log('Quads not found, trying regular boundingBox')
return await element.boundingBox()
}
}
Expand Down Expand Up @@ -234,7 +237,7 @@ export const createCursor = (
// Exit function if the browser is no longer connected
if (!page.browser().isConnected()) return

console.debug('Warning: could not move mouse, error message:', error)
log('Warning: could not move mouse, error message:', error)
}
}
}
Expand All @@ -258,7 +261,7 @@ export const createCursor = (
(_) => {}
) // fire and forget, recursive function
} catch (_) {
console.debug('Warning: stopping random mouse movements')
log('Warning: stopping random mouse movements')
}
}

Expand All @@ -285,7 +288,7 @@ export const createCursor = (
}
await page.mouse.up()
} catch (error) {
console.debug('Warning: could not click mouse, error message:', error)
log('Warning: could not click mouse, error message:', error)
}

if (options?.moveDelay !== undefined && options.moveDelay >= 0) {
Expand Down Expand Up @@ -343,7 +346,7 @@ export const createCursor = (
})
} catch (e) {
// use regular JS scroll method as a fallback
console.debug('Falling back to JS scroll method', e)
log('Falling back to JS scroll method', e)
await elem.evaluate((e) => e.scrollIntoView({ block: 'center' }))
await new Promise((resolve) => setTimeout(resolve, 2000)) // Wait a bit until the scroll has finished
}
Expand Down