forked from CloverLabsAI/playwright-ghost-cursor
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo.ts
More file actions
311 lines (275 loc) Β· 9.96 KB
/
Copy pathdemo.ts
File metadata and controls
311 lines (275 loc) Β· 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Visual Demo Script - Run this to see the human cursor in action!
*
* To run:
* 1. First install Playwright browsers: yarn playwright install chromium
* 2. Then run: yarn ts-node demo.ts
*/
import { chromium } from 'playwright'
import { createCursor } from './src/spoof'
import { installMouseHelper } from './src/mouse-helper'
async function main() {
console.log('π Starting Human Cursor Visual Demo...\n')
const browser = await chromium.launch({
headless: false // Show the browser
})
const page = await browser.newPage()
await page.setViewportSize({ width: 1280, height: 720 })
// Create a demo page with content
await page.setContent(`
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding: 40px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
.container {
max-width: 900px;
margin: 0 auto;
background: white;
padding: 50px;
border-radius: 16px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 42px;
}
.subtitle {
color: #666;
font-size: 18px;
margin-bottom: 40px;
}
.section {
margin: 30px 0;
padding: 30px;
background: #f8f9fa;
border-radius: 8px;
border-left: 4px solid #667eea;
}
.section h2 {
color: #667eea;
margin-top: 0;
}
.button {
display: inline-block;
padding: 15px 30px;
margin: 10px;
background: #667eea;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
text-decoration: none;
}
.button:hover {
background: #5568d3;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}
.button.secondary {
background: #48bb78;
}
.button.secondary:hover {
background: #38a169;
}
.spacer {
height: 800px;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(to bottom, #f8f9fa, #e9ecef);
border-radius: 8px;
margin: 40px 0;
}
.spacer-content {
text-align: center;
color: #666;
}
.footer {
text-align: center;
padding: 40px;
color: #999;
font-size: 14px;
}
.demo-info {
background: #fff3cd;
border: 2px solid #ffc107;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
}
.demo-info strong {
color: #856404;
}
</style>
</head>
<body>
<div class="container">
<h1>π±οΈ Human Cursor Demo</h1>
<p class="subtitle">Watch the cursor move naturally with human-like behavior!</p>
<div class="demo-info">
<strong>π Watch for:</strong>
<ul>
<li>Smooth Bezier curves with natural imperfections</li>
<li>Momentum-based scrolling with ease-out animation</li>
<li>Random variations in positioning (Β±100px)</li>
<li>Natural acceleration and deceleration</li>
</ul>
</div>
<div class="section" id="section1">
<h2>Section 1: Cursor Movement</h2>
<p>The cursor will move to various elements using human-like Bezier curves.</p>
<button class="button" id="btn1">Click Me First</button>
<button class="button secondary" id="btn2">Then Click Me</button>
</div>
<div class="section" id="section2">
<h2>Section 2: Form Interaction</h2>
<p>Watch how the cursor interacts with form elements.</p>
<input type="text" id="input1" placeholder="Name" style="padding: 12px; width: 300px; border: 2px solid #ddd; border-radius: 4px; margin: 10px 0;">
<br>
<input type="email" id="input2" placeholder="Email" style="padding: 12px; width: 300px; border: 2px solid #ddd; border-radius: 4px; margin: 10px 0;">
</div>
<div class="section" id="section3">
<h2>Section 3: Multiple Targets</h2>
<p>The cursor will move between multiple buttons rapidly.</p>
<button class="button" id="btn3">Button A</button>
<button class="button" id="btn4">Button B</button>
<button class="button secondary" id="btn5">Button C</button>
</div>
</div>
<div class="spacer">
<div class="spacer-content">
<h2>β¬οΈ Scroll Down Demo β¬οΈ</h2>
<p>Watch the momentum-based scrolling!</p>
</div>
</div>
<div class="container">
<div class="section" id="section4">
<h2>Section 4: Bottom Content</h2>
<p>The cursor scrolled here with momentum physics!</p>
<button class="button" id="btn6">Final Button</button>
</div>
<div class="footer">
<p>Human Cursor Demo β’ Powered by humancursor algorithm</p>
</div>
</div>
</body>
</html>
`)
// Install mouse helper AFTER content is loaded
await installMouseHelper(page)
// Add a visible custom cursor overlay
await page.evaluate(() => {
// Create cursor element
const cursor = document.createElement('div')
cursor.id = 'custom-cursor'
cursor.style.cssText = `
position: fixed;
width: 16px;
height: 16px;
background: red;
border: 2px solid black;
border-radius: 50%;
pointer-events: none;
z-index: 999999;
transform: translate(-50%, -50%);
`
document.body.appendChild(cursor)
// Update cursor position on mouse move
document.addEventListener('mousemove', (e) => {
cursor.style.left = e.clientX + 'px'
cursor.style.top = e.clientY + 'px'
})
// Also style the default mouse helper
const style = document.createElement('style')
style.innerHTML = `
p-mouse-pointer {
background: rgba(255, 0, 0, 0.8) !important;
border: 2px solid black !important;
width: 16px !important;
height: 16px !important;
margin: -8px 0 0 -8px !important;
}
`
document.head.appendChild(style)
})
console.log('β
Custom cursor installed - you should see a small red cursor with black border!\n')
// Start cursor at center top
const startPosition = { x: 640, y: 100 }
const cursor = createCursor(page, startPosition)
// Move to starting position first to initialize
console.log(`π― Initializing cursor at position: (${startPosition.x}, ${startPosition.y})`)
await cursor.moveTo(startPosition)
await page.waitForTimeout(1000)
console.log(' β Cursor should now be visible at the top center\n')
console.log('π Demo Step 1: Moving to title...')
await cursor.move('h1')
await page.waitForTimeout(500)
console.log('π Demo Step 2: Moving to first button...')
await cursor.move('#btn1')
await page.waitForTimeout(300)
console.log('π±οΈ Demo Step 3: Clicking first button...')
await cursor.click('#btn1', { moveDelay: 0 })
await page.waitForTimeout(300)
console.log('π Demo Step 4: Moving to second button...')
await cursor.move('#btn2')
await page.waitForTimeout(300)
console.log('π±οΈ Demo Step 5: Clicking second button...')
await cursor.click('#btn2', { moveDelay: 0 })
await page.waitForTimeout(500)
console.log('π Demo Step 6: Filling form - Name field...')
await cursor.click('#input1', { moveDelay: 0 })
await page.keyboard.type('John Doe', { delay: 100 })
await page.waitForTimeout(300)
console.log('π Demo Step 7: Filling form - Email field...')
await cursor.click('#input2', { moveDelay: 0 })
await page.keyboard.type('john@example.com', { delay: 100 })
await page.waitForTimeout(500)
console.log('π Demo Step 8: Moving between multiple buttons...')
await cursor.move('#btn3')
await page.waitForTimeout(200)
await cursor.move('#btn4')
await page.waitForTimeout(200)
await cursor.move('#btn5')
await page.waitForTimeout(500)
console.log('π Demo Step 9: Scrolling down with momentum...')
// First scroll down to ensure we need to scroll to see the button
const scrollAmount = await page.evaluate(() => {
const section4 = document.querySelector('#section4')
if (section4) {
const rect = section4.getBoundingClientRect()
return rect.top + window.scrollY - window.innerHeight / 2
}
return 800
})
await cursor.scroll({ y: scrollAmount }, { scrollSpeed: 50 })
await page.waitForTimeout(500)
console.log('π Demo Step 10: Moving to bottom button...')
await cursor.move('#btn6')
await page.waitForTimeout(300)
console.log('π±οΈ Demo Step 11: Clicking bottom button...')
await cursor.click('#btn6', { moveDelay: 0 })
await page.waitForTimeout(500)
console.log('π Demo Step 12: Scrolling back to top...')
await cursor.scrollTo('top', { scrollSpeed: 50 })
await page.waitForTimeout(500)
console.log('π Demo Step 13: Final movement to title...')
await cursor.move('h1')
await page.waitForTimeout(500)
console.log('\nβ
Demo complete! The browser will stay open for 10 more seconds...')
console.log(' Notice how natural the movements and scrolling looked!\n')
await page.waitForTimeout(10000)
await browser.close()
console.log('π Demo finished!')
}
main().catch(console.error)