File size: 5,177 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package human

import (
	"context"
	"fmt"
	"math"
	"math/rand"
	"time"

	"github.com/chromedp/cdproto/cdp"
	"github.com/chromedp/cdproto/dom"
	"github.com/chromedp/cdproto/input"
	"github.com/chromedp/chromedp"
)

var humanRand = rand.New(rand.NewSource(time.Now().UnixNano()))

func SetHumanRandSeed(seed int64) {
	humanRand = rand.New(rand.NewSource(seed))
}

// Config allows injecting a custom random source for testing
type Config struct {
	Rand *rand.Rand
}

// getRand returns the configured random source or the global default
func (c *Config) getRand() *rand.Rand {
	if c != nil && c.Rand != nil {
		return c.Rand
	}
	return humanRand
}

func MouseMove(ctx context.Context, fromX, fromY, toX, toY float64) error {
	distance := math.Sqrt((toX-fromX)*(toX-fromX) + (toY-fromY)*(toY-fromY))
	baseDuration := 100 + (distance/2000)*200
	duration := baseDuration + float64(humanRand.Intn(100))

	steps := int(duration / 20)
	if steps < 5 {
		steps = 5
	}
	if steps > 30 {
		steps = 30
	}

	cp1X := fromX + (toX-fromX)*0.25 + (humanRand.Float64()-0.5)*50
	cp1Y := fromY + (toY-fromY)*0.25 + (humanRand.Float64()-0.5)*50
	cp2X := fromX + (toX-fromX)*0.75 + (humanRand.Float64()-0.5)*50
	cp2Y := fromY + (toY-fromY)*0.75 + (humanRand.Float64()-0.5)*50

	for i := 0; i <= steps; i++ {
		t := float64(i) / float64(steps)

		oneMinusT := 1 - t
		x := oneMinusT*oneMinusT*oneMinusT*fromX +
			3*oneMinusT*oneMinusT*t*cp1X +
			3*oneMinusT*t*t*cp2X +
			t*t*t*toX

		y := oneMinusT*oneMinusT*oneMinusT*fromY +
			3*oneMinusT*oneMinusT*t*cp1Y +
			3*oneMinusT*t*t*cp2Y +
			t*t*t*toY

		x += (humanRand.Float64() - 0.5) * 2
		y += (humanRand.Float64() - 0.5) * 2

		if err := chromedp.Run(ctx,
			chromedp.ActionFunc(func(ctx context.Context) error {
				return input.DispatchMouseEvent(input.MouseMoved, x, y).Do(ctx)
			}),
		); err != nil {
			return err
		}

		delay := time.Duration(16+humanRand.Intn(8)) * time.Millisecond
		time.Sleep(delay)
	}

	return nil
}

func Click(ctx context.Context, x, y float64) error {
	startOffsetX := (humanRand.Float64()-0.5)*200 + 50
	startOffsetY := (humanRand.Float64()-0.5)*200 + 50
	startX := x + startOffsetX
	startY := y + startOffsetY

	distance := math.Sqrt(startOffsetX*startOffsetX + startOffsetY*startOffsetY)
	if distance > 30 {
		if err := chromedp.Run(ctx,
			chromedp.ActionFunc(func(ctx context.Context) error {
				return input.DispatchMouseEvent(input.MouseMoved, startX, startY).Do(ctx)
			}),
		); err != nil {
			return err
		}

		if err := MouseMove(ctx, startX, startY, x, y); err != nil {
			return err
		}
	}

	time.Sleep(time.Duration(50+humanRand.Intn(150)) * time.Millisecond)

	if err := chromedp.Run(ctx,
		chromedp.ActionFunc(func(ctx context.Context) error {
			return input.DispatchMouseEvent(input.MousePressed, x, y).
				WithButton(input.Left).
				WithClickCount(1).
				Do(ctx)
		}),
	); err != nil {
		return err
	}

	time.Sleep(time.Duration(30+humanRand.Intn(90)) * time.Millisecond)

	releaseX := x + (humanRand.Float64()-0.5)*2
	releaseY := y + (humanRand.Float64()-0.5)*2

	return chromedp.Run(ctx,
		chromedp.ActionFunc(func(ctx context.Context) error {
			return input.DispatchMouseEvent(input.MouseReleased, releaseX, releaseY).
				WithButton(input.Left).
				WithClickCount(1).
				Do(ctx)
		}),
	)
}

// ClickElement clicks on an element identified by its backend DOM node ID.
// This uses the backendDOMNodeId from the accessibility tree, NOT a regular DOM nodeId.
func ClickElement(ctx context.Context, backendNodeID cdp.BackendNodeID) error {
	var box *dom.BoxModel
	if err := chromedp.Run(ctx,
		chromedp.ActionFunc(func(ctx context.Context) error {
			var err error
			box, err = dom.GetBoxModel().WithBackendNodeID(backendNodeID).Do(ctx)
			return err
		}),
	); err != nil {
		return err
	}

	if len(box.Content) < 8 {
		return fmt.Errorf("invalid box model")
	}

	centerX := (box.Content[0] + box.Content[2]) / 2
	centerY := (box.Content[1] + box.Content[5]) / 2

	offsetX := (humanRand.Float64() - 0.5) * 10
	offsetY := (humanRand.Float64() - 0.5) * 10

	return Click(ctx, centerX+offsetX, centerY+offsetY)
}

func Type(text string, fast bool) []chromedp.Action {
	return TypeWithConfig(text, fast, nil)
}

// TypeWithConfig generates typing actions with optional custom random source
func TypeWithConfig(text string, fast bool, cfg *Config) []chromedp.Action {
	rng := cfg.getRand()
	actions := []chromedp.Action{}

	baseDelay := 80
	if fast {
		baseDelay = 40
	}

	chars := []rune(text)
	for i, char := range chars {
		actions = append(actions, chromedp.KeyEvent(string(char)))
		delay := baseDelay + rng.Intn(baseDelay/2)
		if rng.Float64() < 0.05 {
			delay += rng.Intn(500)
		}
		if i > 0 && chars[i-1] == char {
			delay = delay / 2
		}
		actions = append(actions, chromedp.Sleep(time.Duration(delay)*time.Millisecond))

		if rng.Float64() < 0.03 && i < len(chars)-1 {
			wrongChar := rune('a' + rng.Intn(26))
			actions = append(actions,
				chromedp.KeyEvent(string(wrongChar)),
				chromedp.Sleep(time.Duration(50+rng.Intn(100))*time.Millisecond),
				chromedp.KeyEvent("\b"),
				chromedp.Sleep(time.Duration(30+rng.Intn(70))*time.Millisecond),
			)
		}
	}
	return actions
}