forked from kkoooqq/fakebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FakeUserAction.ts
551 lines (451 loc) · 15.7 KB
/
FakeUserAction.ts
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// noinspection JSUnusedGlobalSymbols,JSUnusedLocalSymbols,PointlessArithmeticExpressionJS
import {strict as assert} from 'assert'
import {BoundingBox, ElementHandle, KeyInput, Page, Point} from 'puppeteer'
import {helper} from './helper'
import {FakeBrowser} from './FakeBrowser'
import {PptrToolkit} from './PptrToolkit'
import {FakeDeviceDescriptor} from './DeviceDescriptor'
import {Touchscreen} from './TouchScreen'
export class FakeUserAction {
private _mouseCurrPos: Point
// WeakRef needs node >= 14.6.0
// private _fakeBrowser: WeakRef<FakeBrowser> | null
private _fakeBrowser: FakeBrowser | null
constructor(fb: FakeBrowser) {
this._mouseCurrPos = {x: helper.rd(0, 1280), y: helper.rd(0, 700)}
// this._fakeBrowser = new WeakRef<FakeBrowser>(fb)
this._fakeBrowser = fb
}
/**
* Fake mouse movement track
* @param startPos
* @param endPos
* @param maxPoints
* @param cpDelta
*/
private static mouseMovementTrack(
startPos: Point,
endPos: Point,
maxPoints = 30,
cpDelta = 1,
): Point[] {
// reference: https://github.com/mtsee/Bezier/blob/master/src/bezier.js
let nums = []
let maxNum = 0
let moveStep = 1
// Simulates the user's mouse movement acceleration / constant speed / deceleration
for (let n = 0; n < maxPoints; ++n) {
nums.push(maxNum)
// noinspection PointlessArithmeticExpressionJS
if (n < maxPoints * 1 / 10) {
moveStep += helper.rd(60, 100)
} else if (n >= maxPoints * 9 / 10) {
moveStep -= helper.rd(60, 100)
moveStep = Math.max(20, moveStep)
}
maxNum += moveStep
}
const result = []
const p1 = [
startPos.x,
startPos.y,
]
const cp1 = [
(startPos.x + endPos.x) / 2 + helper.rd(30, 100, true) * cpDelta,
(startPos.y + endPos.y) / 2 + helper.rd(30, 100, true) * cpDelta,
]
const cp2 = [
(startPos.x + endPos.x) / 2 + helper.rd(30, 100, true) * cpDelta,
(startPos.y + endPos.y) / 2 + helper.rd(30, 100, true) * cpDelta,
]
const p2 = [
endPos.x,
endPos.y,
]
for (let num of nums) {
const [x, y] = helper.threeBezier(num / maxNum, p1, cp1, cp2, p2)
result.push({x, y})
}
return result
}
/**
* Simulate mouse movement
* @param page
* @param options
*/
private static async simMouseMove(page: Page, options: {
startPos: Point,
endPos: Point,
maxPoints?: number,
timestamp?: number,
cpDelta?: number,
}) {
const points = this.mouseMovementTrack(
options.startPos,
options.endPos,
options.maxPoints || helper.rd(15, 30),
options.cpDelta || 1,
)
for (let n = 0; n < points.length; n += 1) {
const point = points[n]
await page.mouse.move(
point.x,
point.y,
{steps: helper.rd(1, 2)},
)
await helper.sleep((options.timestamp || helper.rd(300, 800)) / points.length)
}
}
get fakeBrowser(): FakeBrowser | null {
// @ts-ignore
if (!this._fakeBrowser || this._fakeBrowser._zombie) {
return null
}
// WeakRef:
// const fb: FakeBrowser | undefined = this._fakeBrowser.deref()
const fb: FakeBrowser | undefined = this._fakeBrowser
if (!fb) {
this._fakeBrowser = null
return null
}
return fb
}
async simMouseMoveTo(
endPos: Point,
maxPoints?: number,
timestamp?: number,
cpDelta?: number,
): Promise<boolean> {
const fb = this.fakeBrowser
if (!fb) {
return false
}
if (fb.isMobileBrowser) {
// We don't need to simulate mouse slide.
await helper.sleepRd(300, 800)
return true
}
// Get the current page of the browser
const currPage = await fb.getActivePage()
assert(currPage)
// first move to a close position, then finally move to the target position
const closeToEndPos: Point = {
x: endPos.x + helper.rd(5, 30, true),
y: endPos.y + helper.rd(5, 20, true),
}
await FakeUserAction.simMouseMove(currPage, {
startPos: this._mouseCurrPos,
endPos: closeToEndPos,
maxPoints,
timestamp,
cpDelta,
})
// The last pos must correction
await currPage.mouse.move(
endPos.x,
endPos.y,
{steps: helper.rd(5, 13)},
)
this._mouseCurrPos = endPos
return true
}
async simRandomMouseMove(): Promise<boolean> {
const fb = this.fakeBrowser
if (!fb) {
return false
}
if (fb.isMobileBrowser) {
// We don't need to simulate mouse slide.
await helper.sleepRd(200, 500)
return true
}
const fakeDD = fb.driverParams.fakeDeviceDesc
assert(fakeDD)
const innerWidth = fakeDD.window.innerWidth
const innerHeight = fakeDD.window.innerHeight
// -----------------
// | 1/6 |
// | 1/4 1/4 |
// | 1/6 |
// -----------------
const startX = innerWidth / 4
const startY = innerHeight / 6
const endX = innerWidth * 3 / 4
const endY = innerHeight * 5 / 6
const endPos = {x: helper.rd(startX, endX), y: helper.rd(startY, endY)}
await this.simMouseMoveTo(endPos)
await helper.sleepRd(300, 800)
return true
}
async simClick(options = {
pauseAfterMouseUp: true,
}): Promise<boolean> {
const fb = this.fakeBrowser
if (!fb) {
return false
}
const currPage = await fb.getActivePage()
assert(currPage)
if (fb.isMobileBrowser) {
// We can't use mouse obj, we have to use touchscreen
await currPage.touchscreen.tap(this._mouseCurrPos.x, this._mouseCurrPos.y)
} else {
await currPage.mouse.down()
await helper.sleepRd(30, 80)
await currPage.mouse.up()
}
if (options && options.pauseAfterMouseUp) {
await helper.sleepRd(150, 600)
}
return true
}
async simMoveToAndClick(
endPos: Point,
options = {
pauseAfterMouseUp: true,
},
): Promise<boolean> {
const fb = this.fakeBrowser
if (!fb) {
return false
}
const currPage = await fb.getActivePage()
assert(currPage)
if (!fb.isMobileBrowser) {
await this.simMouseMoveTo(endPos)
await currPage.mouse.move(
endPos.x + helper.rd(-10, 10),
endPos.y,
{steps: helper.rd(8, 20)},
)
}
this._mouseCurrPos = endPos
await helper.sleepRd(300, 800)
return this.simClick(options)
}
async simMouseMoveToElement(eh: ElementHandle): Promise<boolean> {
const fb = this.fakeBrowser
if (!fb) {
return false
}
const fakeDD = fb.driverParams.fakeDeviceDesc
assert(fakeDD)
const currPage = await fb.getActivePage()
assert(currPage)
let box: BoundingBox | null
if (fb.isMobileBrowser) {
box = await FakeUserAction.adjustElementPositionWithTouchscreen(eh, currPage, fakeDD)
} else {
box = await FakeUserAction.adjustElementPositionWithMouse(eh, currPage, fakeDD)
}
if (box) {
// The position of each element click should not be the center of the element
// size of the clicked element must larger than 10 x 10
const endPos: Point = {
x: box.x + box.width / 2 + helper.rd(0, 5, true),
y: box.y + box.height / 2 + helper.rd(0, 5, true),
}
await this.simMouseMoveTo(endPos)
// Pause
await helper.sleepRd(300, 800)
return true
}
return false
}
async simClickElement(
eh: ElementHandle,
options = {
pauseAfterMouseUp: true,
},
): Promise<boolean> {
const moveToEl = await this.simMouseMoveToElement(eh)
if (!moveToEl) {
return false
}
// click
if (await this.simClick(options)) {
return true
} else {
return false
}
}
private static async adjustElementPositionWithMouse(
eh: ElementHandle<Element>,
currPage: Page,
fakeDD: FakeDeviceDescriptor,
): Promise<BoundingBox | null> {
let box = null
for (; ;) {
box = await PptrToolkit.boundingBox(eh)
if (box) {
// Check the node is in the visible area
// @ts-ignore
let deltaX: number = 0
let deltaY: number = 0
let viewportAdjust = false
// If the top of the node is less than 0
if (box.y <= 0) {
// deltaY always positive
// ---------------------
// 30px |
// [ ] |
// .. Distance to be moved
// .. |
// .. |
// ---------------------body top
deltaY = Math.min(
-(box.y - 30) - 0,
helper.rd(150, 300),
)
deltaY = -deltaY
viewportAdjust = true
} else if (box.y + box.height >= fakeDD.window.innerHeight) {
// If the bottom is beyond
deltaY = Math.min(
box.y + box.height + 30 - fakeDD.window.innerHeight,
helper.rd(150, 300),
)
viewportAdjust = true
}
// if (box.x <= 0) {
// // If the top of the button is less than 0
// deltaX = Math.min(-box.x + 30, sh.rd(100, 400))
// deltaX = -deltaX
// viewportAdjust = true
// } else if (box.x + box.width >= fakeDD.window.innerWidth) {
// // If the bottom is beyond
// deltaX = Math.min(box.x + box.width - fakeDD.window.innerWidth + 30, sh.rd(100, 400))
// viewportAdjust = true
// }
if (viewportAdjust) {
// await currPage.mouse.wheel({deltaX})
await currPage.mouse.wheel({deltaY})
await helper.sleepRd(100, 400)
} else {
break
}
} else {
break
}
}
return box
}
private static async adjustElementPositionWithTouchscreen(
eh: ElementHandle<Element>,
currPage: Page,
fakeDD: FakeDeviceDescriptor,
): Promise<BoundingBox | null> {
let box = null
for (; ;) {
box = await PptrToolkit.boundingBox(eh)
if (box) {
// @ts-ignore
let deltaX: number = 0
let deltaY: number = 0
let viewportAdjust = false
if (box.y <= 0) {
deltaY = Math.min(-box.y + 30, helper.rd(100, 300))
deltaY = -deltaY
viewportAdjust = true
} else if (box.y + box.height >= fakeDD.window.innerHeight) {
deltaY = Math.min(box.y + box.height - fakeDD.window.innerHeight + 30, helper.rd(100, 300))
viewportAdjust = true
}
if (viewportAdjust) {
// noinspection TypeScriptValidateTypes
const _patchTouchscreenDesc = Object.getOwnPropertyDescriptor(currPage, '_patchTouchscreen')
assert(_patchTouchscreenDesc)
const touchscreen: Touchscreen = _patchTouchscreenDesc.value
assert(touchscreen)
// if deltaY is negative, drop down, otherwise drop up
const startX: number = fakeDD.window.innerWidth / 2 + helper.rd(0, fakeDD.window.innerWidth / 6)
const endX: number = fakeDD.window.innerWidth / 2 + helper.rd(0, fakeDD.window.innerWidth / 6)
let startY: number
let endY: number
if (deltaY < 0) {
startY = helper.rd(0, fakeDD.window.innerHeight - (-deltaY))
endY = startY + deltaY
} else {
startY = helper.rd(deltaY, fakeDD.window.innerHeight)
endY = startY - deltaY
}
await touchscreen.drag({
x: startX, y: startY,
}, {
x: endX, y: endY,
})
await helper.sleepRd(100, 300)
} else {
break
}
} else {
break
}
}
return box
}
async simKeyboardPress(
text: KeyInput,
options = {
pauseAfterKeyUp: true,
},
): Promise<boolean> {
const fb = this.fakeBrowser
if (!fb) {
return false
}
const currPage = await fb.getActivePage()
assert(currPage)
await currPage.keyboard.press(text)
if (options && options.pauseAfterKeyUp) {
await helper.sleepRd(300, 1000)
}
return true
}
async simKeyboardEnter(options = {
pauseAfterKeyUp: true,
}): Promise<boolean> {
return await this.simKeyboardPress('Enter', options)
}
async simKeyboardEsc(options = {
pauseAfterKeyUp: true,
}): Promise<boolean> {
return await this.simKeyboardPress('Escape', options)
}
async simKeyboardType(
text: string,
options = {
pauseAfterLastKeyUp: true,
},
): Promise<boolean> {
const fb = this.fakeBrowser
if (!fb) {
return false
}
const currPage = await fb.getActivePage()
assert(currPage)
const needsShiftKey = '~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:"ZXCVBNM<>?'
// TODO: check if shiftKey, alt, ctrl can be fired in mobile browsers
for (let ch of text) {
let needsShift = false
if (needsShiftKey.includes(ch)) {
needsShift = true
await currPage.keyboard.down('ShiftLeft')
await helper.sleepRd(500, 1000)
}
// if a Chinese character
const isCh = ch.match(/^[\u4e00-\u9fa5]/)
const delay = isCh ? helper.rd(200, 800) : helper.rd(30, 100)
await currPage.keyboard.type('' + ch, {delay})
if (needsShift) {
await helper.sleepRd(150, 450)
await currPage.keyboard.up('ShiftLeft')
}
await helper.sleepRd(30, 100)
}
if (options && options.pauseAfterLastKeyUp) {
await helper.sleepRd(300, 1000)
}
return true
}
}