-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions_humanize_type.go
More file actions
68 lines (58 loc) · 1.95 KB
/
Copy pathactions_humanize_type.go
File metadata and controls
68 lines (58 loc) · 1.95 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
package browser
import (
"context"
"fmt"
"time"
"github.com/anatolykoptev/go-browser/humanize"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
)
// doTypeTextHumanized types text character by character with human-like delays,
// dispatching real keydown/char/keyup CDP events so keyboard listeners see authentic input.
func doTypeTextHumanized(
ctx context.Context, page *rod.Page, selector, text string, cursor *humanize.Cursor,
) error {
if err := doClickHumanized(ctx, page, selector, cursor); err != nil {
return fmt.Errorf("type_text: focus: %w", err)
}
delays := humanize.TypingDelays(text)
for i, ch := range text {
char := string(ch)
ci := humanize.LookupChar(ch)
code := ci.Code
vk := ci.VK
_ = proto.InputDispatchKeyEvent{
Type: proto.InputDispatchKeyEventTypeRawKeyDown,
Key: char,
Code: code,
WindowsVirtualKeyCode: vk,
}.Call(page)
// Key dwell time (T4 TMX behavioral biometric): hold key for 40-120ms
// before firing char and keyUp. Real humans don't release instantly.
sleepCtx(ctx, time.Duration(humanize.KeyDwellTime())*time.Millisecond)
_ = proto.InputDispatchKeyEvent{
Type: proto.InputDispatchKeyEventTypeChar,
Text: char,
UnmodifiedText: char,
WindowsVirtualKeyCode: vk,
}.Call(page)
_ = proto.InputDispatchKeyEvent{
Type: proto.InputDispatchKeyEventTypeKeyUp,
Key: char,
Code: code,
WindowsVirtualKeyCode: vk,
}.Call(page)
// Word boundary pause: extra 300-500ms at spaces (15% chance)
// simulating inter-word cognitive processing gap.
if ch == ' ' {
if pause := humanize.WordBoundaryPause(); pause > 0 {
sleepCtx(ctx, time.Duration(pause)*time.Millisecond)
}
}
// Inter-key delay (gaussian μ=120ms, σ=40ms).
if i < len(delays) {
sleepCtx(ctx, time.Duration(delays[i])*time.Millisecond)
}
}
return nil
}