forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
197 lines (167 loc) · 4.61 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
package chromedp
import (
"context"
"errors"
"time"
"github.com/knq/chromedp/cdp"
"github.com/knq/chromedp/cdp/dom"
"github.com/knq/chromedp/cdp/input"
)
// Error types.
var (
ErrInvalidDimensions = errors.New("invalid box dimensions")
)
// MouseAction is a mouse action.
func MouseAction(typ input.MouseType, x, y int64, opts ...MouseOption) Action {
f := input.DispatchMouseEvent(typ, x, y)
for _, o := range opts {
f = o(f)
}
return f
}
// MouseClickXY sends a left mouse button click at the X, Y location.
func MouseClickXY(x, y int64, opts ...MouseOption) Action {
return Tasks{
MouseAction(input.MousePressed, x, y, append(opts, Button(input.ButtonLeft), ClickCount(1))...),
MouseAction(input.MouseReleased, x, y, append(opts, Button(input.ButtonLeft), ClickCount(1))...),
}
}
// MouseActionNode dispatches a mouse event at the center of a specified node.
func MouseActionNode(n *cdp.Node, opts ...MouseOption) Action {
return ActionFunc(func(ctxt context.Context, h cdp.FrameHandler) error {
box, err := dom.GetBoxModel(n.NodeID).Do(ctxt, h)
if err != nil {
return err
}
c := len(box.Content)
if c%2 != 0 {
return ErrInvalidDimensions
}
var x, y int64
for i := 0; i < c; i += 2 {
x += int64(box.Content[i])
y += int64(box.Content[i+1])
}
return MouseClickXY(x/int64(c/2), y/int64(c/2), opts...).Do(ctxt, h)
})
}
// MouseOption is a mouse action option.
type MouseOption func(*input.DispatchMouseEventParams) *input.DispatchMouseEventParams
// Button is a mouse action option to set the button to click.
func Button(button input.ButtonType) MouseOption {
return func(p *input.DispatchMouseEventParams) *input.DispatchMouseEventParams {
return p.WithButton(button)
}
}
// ButtonString is a mouse action option to set the button to click as a
// string.
func ButtonString(btn string) MouseOption {
return Button(input.ButtonType(btn))
}
// ButtonModifiers is a mouse action option to add additional modifiers for the
// button.
func ButtonModifiers(modifiers ...input.Modifier) MouseOption {
return func(p *input.DispatchMouseEventParams) *input.DispatchMouseEventParams {
for _, m := range modifiers {
p.Modifiers |= m
}
return p
}
}
// ClickCount is a mouse action option to set the click count.
func ClickCount(n int) MouseOption {
return func(p *input.DispatchMouseEventParams) *input.DispatchMouseEventParams {
return p.WithClickCount(int64(n))
}
}
// KeyAction contains information about a key action.
type KeyAction struct {
v string
opts []KeyOption
}
// KeyCode are known system key codes.
type KeyCode string
// KeyCode values.
const (
KeyCodeBackspace = "\b"
KeyCodeTab = "\t"
KeyCodeCR = "\r"
KeyCodeLF = "\n"
KeyCodeLeft = "\x25"
KeyCodeUp = "\x26"
KeyCodeRight = "\x27"
KeyCodeDown = "\x28"
)
const (
keyRuneCR = '\r'
)
// keyCodeNames is the map of key code values to their respective named
// identifiers.
var keyCodeNames = map[KeyCode]string{
KeyCodeBackspace: "Backspace",
KeyCodeTab: "Tab",
KeyCodeCR: "Enter",
KeyCodeLF: "Enter",
KeyCodeLeft: "Left",
KeyCodeUp: "Up",
KeyCodeRight: "Right",
KeyCodeDown: "Down",
}
// Do satisfies Action interface.
func (ka *KeyAction) Do(ctxt context.Context, h cdp.FrameHandler) error {
var err error
// apply opts
sysP := input.DispatchKeyEvent(input.KeyRawDown)
keyP := input.DispatchKeyEvent(input.KeyChar)
for _, o := range ka.opts {
sysP = o(sysP)
keyP = o(keyP)
}
for _, r := range ka.v {
s := string(r)
keyS := KeyCode(r)
if n, ok := keyCodeNames[keyS]; ok {
kc := int64(r)
if keyS == KeyCodeLF {
s = string(keyRuneCR)
kc = int64(keyRuneCR)
}
err = sysP.WithKey(n).
WithNativeVirtualKeyCode(kc).
WithWindowsVirtualKeyCode(kc).
WithKeyIdentifier(s).
WithIsSystemKey(true).
Do(ctxt, h)
if err != nil {
return err
}
}
err = keyP.WithText(s).Do(ctxt, h)
if err != nil {
return err
}
// FIXME
time.Sleep(100 * time.Millisecond)
}
return nil
}
// KeyActionNode dispatches a key event on a node.
func KeyActionNode(n *cdp.Node, v string, opts ...KeyOption) Action {
return Tasks{
dom.Focus(n.NodeID),
MouseActionNode(n),
&KeyAction{v, opts},
}
}
// KeyOption is a key action option.
type KeyOption func(*input.DispatchKeyEventParams) *input.DispatchKeyEventParams
// KeyModifiers is a key action option to add additional modifiers on the key
// press.
func KeyModifiers(modifiers ...input.Modifier) KeyOption {
return func(p *input.DispatchKeyEventParams) *input.DispatchKeyEventParams {
for _, m := range modifiers {
p.Modifiers |= m
}
return p
}
}