forked from Ecwid/gosnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.go
223 lines (205 loc) · 6.55 KB
/
dom.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
package cdp
// Node https://chromedevtools.github.io/devtools-protocol/tot/DOM#type-Node
type Node struct {
NodeID int64 `json:"nodeId"`
ParentID int64 `json:"parentId"`
BackendNodeID int64 `json:"backendNodeId"`
NodeType int64 `json:"nodeType"`
NodeName string `json:"nodeName"`
LocalName string `json:"localName"`
NodeValue string `json:"nodeValue"`
ChildNodeCount int64 `json:"childNodeCount"`
Children []*Node `json:"children"`
Attributes []string `json:"attributes"`
DocumentURL string `json:"documentURL"`
BaseURL string `json:"baseURL"`
PublicID string `json:"publicId"`
SystemID string `json:"systemId"`
InternalSubset string `json:"internalSubset"`
XMLVersion string `json:"xmlVersion"`
Name string `json:"name"`
Value string `json:"value"`
PseudoType string `json:"pseudoType"`
ShadowRootType string `json:"shadowRootType"`
FrameID string `json:"frameId"`
ContentDocument *Node `json:"contentDocument"`
ShadowRoots []*Node `json:"shadowRoots"`
TemplateContent *Node `json:"templateContent"`
PseudoElements []*Node `json:"pseudoElements"`
ImportedDocument *Node `json:"importedDocument"`
IsSVG bool `json:"isSVG"`
}
// EventListener https://chromedevtools.github.io/devtools-protocol/tot/DOMDebugger#type-EventListener
type EventListener struct {
Type string `json:"type"`
UseCapture bool `json:"useCapture"`
Passive bool `json:"passive"`
Once bool `json:"once"`
ScriptID string `json:"scriptId"`
LineNumber int64 `json:"lineNumber"`
ColumnNumber int64 `json:"columnNumber"`
Handler *RemoteObject `json:"handler"`
OriginalHandler *RemoteObject `json:"originalHandler"`
BackendNodeID int64 `json:"backendNodeId"`
}
type quad []float64
type highlightConfig struct {
ShowInfo bool `json:"showInfo,omitempty"`
ShowStyles bool `json:"showStyles,omitempty"`
ShowRulers bool `json:"showRulers,omitempty"`
ShowExtensionLines bool `json:"showExtensionLines,omitempty"`
ContentColor *rgba `json:"contentColor,omitempty"`
PaddingColor *rgba `json:"paddingColor,omitempty"`
BorderColor *rgba `json:"borderColor,omitempty"`
MarginColor *rgba `json:"marginColor,omitempty"`
EventTargetColor *rgba `json:"eventTargetColor,omitempty"`
ShapeColor *rgba `json:"shapeColor,omitempty"`
ShapeMarginColor *rgba `json:"shapeMarginColor,omitempty"`
CSSGridColor *rgba `json:"cssGridColor,omitempty"`
}
// Rect https://chromedevtools.github.io/devtools-protocol/tot/DOM#type-Rect
type Rect struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Width float64 `json:"width"`
Height float64 `json:"height"`
}
type rgba struct {
R int64 `json:"r"`
G int64 `json:"g"`
B int64 `json:"b"`
A float64 `json:"a,omitempty"`
}
func (q quad) middle() (float64, float64) {
x := 0.0
y := 0.0
for i := 0; i < 8; i += 2 {
x += q[i]
y += q[i+1]
}
return x / 4, y / 4
}
func (session *Session) focus(objectID string) error {
_, err := session.blockingSend("DOM.focus", &Params{"objectId": objectID})
return err
}
func (session *Session) getFrameOwner(frameID string) (int64, error) {
msg, err := session.blockingSend("DOM.getFrameOwner", &Params{"frameId": frameID})
if err != nil {
return 0, err
}
nbf := msg["backendNodeId"].(float64)
return int64(nbf), nil
}
func (session *Session) getNodeForLocation(x, y float64) (int64, error) {
msg, err := session.blockingSend("DOM.getNodeForLocation", &Params{
"x": x,
"y": y,
"includeUserAgentShadowDOM": true,
})
if err != nil {
return 0, err
}
nbf := msg["backendNodeId"].(float64)
return int64(nbf), nil
}
func (session *Session) describeNode(objectID string) (*Node, error) {
raw, err := session.blockingSend("DOM.describeNode", &Params{
"objectId": objectID,
"depth": 1,
})
if err != nil {
return nil, err
}
node := &Node{}
unmarshal(raw["node"], node)
return node, nil
}
func (session *Session) requestNode(objectID string) (int64, error) {
msg, err := session.blockingSend("DOM.requestNode", &Params{
"objectId": objectID,
})
if err != nil {
return -1, err
}
return int64(msg["nodeId"].(float64)), nil
}
func (session *Session) resolveNode(backendNodeID int64) (*RemoteObject, error) {
raw, err := session.blockingSend("DOM.resolveNode", &Params{
"backendNodeId": backendNodeID,
"executionContextId": session.contextID,
})
if err != nil {
return nil, err
}
ro := &RemoteObject{}
unmarshal(raw["object"], ro)
return ro, nil
}
func (session *Session) highlightNode(objectID string) error {
_, err := session.blockingSend("Overlay.highlightNode", &Params{
"objectId": objectID,
"highlightConfig": &highlightConfig{
ShowRulers: true,
BorderColor: &rgba{R: 255, G: 1, B: 1},
},
})
return err
}
func (session *Session) highlightQuad(q quad, color *rgba) error {
_, err := session.blockingSend("Overlay.highlightQuad", &Params{
"quad": q,
"outlineColor": color,
})
return err
}
func (session *Session) getContentQuads(backendNodeID int64, objectID string) (quad, error) {
params := &Params{}
if backendNodeID > 0 {
(*params)["backendNodeId"] = backendNodeID
}
if objectID != "" {
(*params)["objectId"] = objectID
}
obj, err := session.blockingSend("DOM.getContentQuads", params)
if err != nil {
return nil, err
}
qs := make([]quad, 0)
if len(qs) > 1 {
panic("todo few quads support")
}
unmarshal(obj["quads"], &qs)
return qs[0], nil
}
func (session *Session) setFileInputFiles(files []string, objectID string) error {
_, err := session.blockingSend("DOM.setFileInputFiles", &Params{
"files": files,
"objectId": objectID,
})
return err
}
func (session *Session) getAttributes(nodeID int64) ([]string, error) {
msg, err := session.blockingSend("DOM.getAttributes", &Params{
"nodeId": nodeID,
})
if err != nil {
return nil, err
}
attributes := make([]string, 0)
unmarshal(msg["attributes"], &attributes)
return attributes, err
}
func (session *Session) getEventListeners(objectID string) ([]EventListener, error) {
raw, err := session.blockingSend("DOMDebugger.getEventListeners", &Params{
"objectId": objectID,
"depth": 1,
"pierce": true,
})
if err != nil {
return nil, err
}
events := make([]EventListener, 0)
unmarshal(raw["listeners"], &events)
return events, nil
}