forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
324 lines (259 loc) · 7.04 KB
/
input.go
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
package rod
import (
"fmt"
"sync"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/gson"
)
// Keyboard represents the keyboard on a page, it's always related the main frame
type Keyboard struct {
sync.Mutex
page *Page
// modifiers are currently beening pressed
modifiers int
}
func (k *Keyboard) getModifiers() int {
k.Lock()
defer k.Unlock()
return k.modifiers
}
// Down holds the key down
func (k *Keyboard) Down(key rune) error {
k.Lock()
defer k.Unlock()
actions := input.Encode(key)
err := actions[0].Call(k.page)
if err != nil {
return err
}
k.modifiers = actions[0].Modifiers
return nil
}
// Up releases the key
func (k *Keyboard) Up(key rune) error {
k.Lock()
defer k.Unlock()
actions := input.Encode(key)
err := actions[len(actions)-1].Call(k.page)
if err != nil {
return err
}
k.modifiers = 0
return nil
}
// Press keys one by one like a human typing on the keyboard.
// Each press is a combination of Keyboard.Down and Keyboard.Up.
// It can be used to input Chinese or Janpanese characters, you have to use InsertText to do that.
func (k *Keyboard) Press(keys ...rune) error {
k.Lock()
defer k.Unlock()
for _, key := range keys {
defer k.page.tryTrace(TraceTypeInput, "press "+input.Keys[key].Key)()
k.page.browser.trySlowmotion()
actions := input.Encode(key)
k.modifiers = actions[0].Modifiers
defer func() { k.modifiers = 0 }()
for _, action := range actions {
err := action.Call(k.page)
if err != nil {
return err
}
}
}
return nil
}
// InsertText is like pasting text into the page
func (k *Keyboard) InsertText(text string) error {
k.Lock()
defer k.Unlock()
defer k.page.tryTrace(TraceTypeInput, "insert text "+text)()
k.page.browser.trySlowmotion()
err := proto.InputInsertText{Text: text}.Call(k.page)
return err
}
// Mouse represents the mouse on a page, it's always related the main frame
type Mouse struct {
sync.Mutex
page *Page
id string // mouse svg dom element id
x float64
y float64
// the buttons is currently beening pressed, reflects the press order
buttons []proto.InputMouseButton
}
// Move to the absolute position with specified steps
func (m *Mouse) Move(x, y float64, steps int) error {
m.Lock()
defer m.Unlock()
if steps < 1 {
steps = 1
}
stepX := (x - m.x) / float64(steps)
stepY := (y - m.y) / float64(steps)
button, buttons := input.EncodeMouseButton(m.buttons)
for i := 0; i < steps; i++ {
m.page.browser.trySlowmotion()
toX := m.x + stepX
toY := m.y + stepY
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseMoved,
X: toX,
Y: toY,
Button: button,
Buttons: gson.Int(buttons),
Modifiers: m.page.Keyboard.getModifiers(),
}.Call(m.page)
if err != nil {
return err
}
// to make sure set only when call is successful
m.x = toX
m.y = toY
if m.page.browser.trace {
if !m.updateMouseTracer() {
m.initMouseTracer()
m.updateMouseTracer()
}
}
}
return nil
}
// Scroll the relative offset with specified steps
func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error {
m.Lock()
defer m.Unlock()
defer m.page.tryTrace(TraceTypeInput, fmt.Sprintf("scroll (%.2f, %.2f)", offsetX, offsetY))()
m.page.browser.trySlowmotion()
if steps < 1 {
steps = 1
}
button, buttons := input.EncodeMouseButton(m.buttons)
stepX := offsetX / float64(steps)
stepY := offsetY / float64(steps)
for i := 0; i < steps; i++ {
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseWheel,
X: m.x,
Y: m.y,
Button: button,
Buttons: gson.Int(buttons),
Modifiers: m.page.Keyboard.getModifiers(),
DeltaX: stepX,
DeltaY: stepY,
}.Call(m.page)
if err != nil {
return err
}
}
return nil
}
// Down holds the button down
func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
m.Lock()
defer m.Unlock()
toButtons := append(m.buttons, button)
_, buttons := input.EncodeMouseButton(toButtons)
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMousePressed,
Button: button,
Buttons: gson.Int(buttons),
ClickCount: clicks,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.x,
Y: m.y,
}.Call(m.page)
if err != nil {
return err
}
m.buttons = toButtons
return nil
}
// Up releases the button
func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
m.Lock()
defer m.Unlock()
toButtons := []proto.InputMouseButton{}
for _, btn := range m.buttons {
if btn == button {
continue
}
toButtons = append(toButtons, btn)
}
_, buttons := input.EncodeMouseButton(toButtons)
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseReleased,
Button: button,
Buttons: gson.Int(buttons),
ClickCount: clicks,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.x,
Y: m.y,
}.Call(m.page)
if err != nil {
return err
}
m.buttons = toButtons
return nil
}
// Click the button. It's the combination of Mouse.Down and Mouse.Up
func (m *Mouse) Click(button proto.InputMouseButton) error {
m.page.browser.trySlowmotion()
err := m.Down(button, 1)
if err != nil {
return err
}
return m.Up(button, 1)
}
// Touch presents a touch device, such as a hand with fingers, each finger is a proto.InputTouchPoint.
// Touch events is stateless, we use the struct here only as a namespace to make the API style unified.
type Touch struct {
page *Page
}
// Start a touch action
func (t *Touch) Start(points ...*proto.InputTouchPoint) error {
// TODO: https://crbug.com/613219
_ = t.page.WaitRepaint()
_ = t.page.WaitRepaint()
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchStart,
TouchPoints: points,
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// Move touch points. Use the InputTouchPoint.ID (Touch.identifier) to track points.
// Doc: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
func (t *Touch) Move(points ...*proto.InputTouchPoint) error {
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchMove,
TouchPoints: points,
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// End touch action
func (t *Touch) End() error {
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchEnd,
TouchPoints: []*proto.InputTouchPoint{},
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// Cancel touch action
func (t *Touch) Cancel() error {
return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchCancel,
TouchPoints: []*proto.InputTouchPoint{},
Modifiers: t.page.Keyboard.getModifiers(),
}.Call(t.page)
}
// Tap dispatches a touchstart and touchend event.
func (t *Touch) Tap(x, y float64) error {
defer t.page.tryTrace(TraceTypeInput, "touch")()
t.page.browser.trySlowmotion()
p := &proto.InputTouchPoint{X: x, Y: y}
err := t.Start(p)
if err != nil {
return err
}
return t.End()
}