Xetera / ghost-cursor

🖱️ Generate human-like mouse movements with puppeteer or on any 2D plane
MIT License
1.03k stars 120 forks source link

feat: ability to associate timestamps with movements #124

Closed aw1875 closed 3 months ago

aw1875 commented 5 months ago

Take two on a PR I did a few years ago (https://github.com/Xetera/ghost-cursor/pull/42).

This version does essentially the same thing as the original PR but with a more elegant approach by utilizing the options that can be passed to the path function and calculating the time to take using the Trapezoidal rule.

I'm open to any suggestions/changes so please let me know.

Niek commented 5 months ago

Thanks for the PR!

If I understand correctly, this is only showing the timestamps, not actually using it, right?

Maybe we should change the puppeteer mouse.move() event to use CDP Input.dispatchMouseEvent() directly, which supports a timestamp: https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent

aw1875 commented 5 months ago

That's correct it's just for show. However, I wouldn't be against digging more into the Input.dispatchMouseEvent docs to use the generated timestamps in puppeteer if you think it would make sense for me to do.

aw1875 commented 5 months ago

@Niek just checking back in so I don't lose track of this pr. Did you want me to make any changes?

Niek commented 4 months ago

In my opinion this only makes sense if the timestamps are used, meaning we need to switch to manual Input.dispatchMouseEvent() calls.

aw1875 commented 4 months ago

In my opinion this only makes sense if the timestamps are used, meaning we need to switch to manual Input.dispatchMouseEvent() calls.

Honestly, I don't really see any scenario where this would work unless I'm also making changes to functions like move, moveTo, etc. However, something like this could technically take that approach (this is kinda hacky but you get the idea):

   const tracePath = async (
-    vectors: Iterable<Vector>,
+    vectors: Iterable<Vector | TimedVector>,
     abortOnMove: boolean = false
   ): Promise<void> => {
...
-        await page.mouse.move(v.x, v.y)
+        if ('timestamp' in v) {
+          await getCDPClient(page).send('Input.dispatchMouseEvent', {
+            type: 'mouseMoved',
+            x: v.x,
+            y: v.y,
+            timestamp: v.timestamp
+          })
+        } else {
+          await page.mouse.move(v.x, v.y)
+        }
...

Or, we could always use the dispatchMouseEvent with something more along the lines of:

   const tracePath = async (
-    vectors: Iterable<Vector>,
+    vectors: Iterable<Vector | TimedVector>,
     abortOnMove: boolean = false
   ): Promise<void> => {
...
-        await page.mouse.move(v.x, v.y)
+        const dispatchParams: Protocol.Input.DispatchMouseEventRequest = {
+          type: 'mouseMoved',
+          x: v.x,
+          y: v.y,
+        }
+
+        if ('timestamp' in v) dispatchParams.timestamp = v.timestamp
+
+        await getCDPClient(page).send('Input.dispatchMouseEvent', dispatchParams)
...
Niek commented 4 months ago

Yes, this is what I was thinking of. Although it's probably better to have the CDP client initialised once in order to avoid creating thousands of clients.

aw1875 commented 4 months ago

Good call, my brain didn't even process that I was calling that in a loop when I sent that code block

Niek commented 4 months ago

Awesome, thanks! This LGTM, but @bvandercar-vt can you review this as well?

aw1875 commented 3 months ago

Not sure what happened there with the merge conflicts not getting all the changes the first time around and with the whitespace changes on the second one (husky ran properly), if one of you could fix the whitespace I'd appreciate it.

bvandercar-vt commented 3 months ago

Not sure what happened there with the merge conflicts not getting all the changes the first time around and with the whitespace changes on the second one (husky ran properly), if one of you could fix the whitespace I'd appreciate it.

Did you run yarn lint? Should fix the formatting

aw1875 commented 3 months ago

Not sure what happened there with the merge conflicts not getting all the changes the first time around and with the whitespace changes on the second one (husky ran properly), if one of you could fix the whitespace I'd appreciate it.

Did you run yarn lint? Should fix the formatting

Yeah that was post yarn lint (plus husky should be running the linter automatically anyway)

bvandercar-vt commented 3 months ago

Not sure what happened there with the merge conflicts not getting all the changes the first time around and with the whitespace changes on the second one (husky ran properly), if one of you could fix the whitespace I'd appreciate it.

Did you run yarn lint? Should fix the formatting

Yeah that was post yarn lint (plus husky should be running the linter automatically anyway)

Ah yeah yarn lint doesn't fix this whitespace issue on my end either:

image

Definitely a downside of ts-standard then. We could add prettier as a dep if the repo owners are interested.

Update: Not sure if the repo owners will go for it, but I wanted to see how it looked-- made a prettier PR: https://github.com/Xetera/ghost-cursor/pull/142

aw1875 commented 3 months ago

Not sure what happened there with the merge conflicts not getting all the changes the first time around and with the whitespace changes on the second one (husky ran properly), if one of you could fix the whitespace I'd appreciate it.

Did you run yarn lint? Should fix the formatting

Yeah that was post yarn lint (plus husky should be running the linter automatically anyway)

Ah yeah yarn lint doesn't fix this whitespace issue on my end either:

image

Definitely a downside of ts-standard then. We could add prettier as a dep if the repo owners are interested.

Update: Not sure if the repo owners will go for it, but I wanted to see how it looked-- made a prettier PR: #142

super annoying, still a little what caused the whitespace to begin with as I turned off any formatters I have when working in this repo but not sure what else I can do besides manually fix it

aw1875 commented 3 months ago

Should be good now, really odd but oh well

bvandercar-vt commented 3 months ago

@aw1875 PR looks good! Thanks for implementing those changes!

Niek commented 3 months ago

Thanks a lot, merged now!