-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
369 lines (330 loc) · 12 KB
/
element.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package browser
import (
"github.com/dop251/goja"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
"github.com/shiroyk/cloudcat/js"
)
// Element represents the DOM element
type Element struct {
*rod.Element
}
// Elements provides some helpers to deal with element list
type Elements []map[string]any
// First returns the first element, if the list is empty returns nil
func (els Elements) First() any {
if els.Empty() {
return nil
}
return els[0]
}
// Last returns the last element, if the list is empty returns nil
func (els Elements) Last() any {
if els.Empty() {
return nil
}
return els[len(els)-1]
}
// Empty returns true if the list is empty
func (els Elements) Empty() bool {
return len(els) == 0
}
// NewElement creates a new Element mapping
func NewElement(ele *rod.Element, vm *goja.Runtime) goja.Value {
return vm.ToValue(mappingElement(Element{ele}))
}
// NewElements creates a new Elements mapping
func NewElements(elements rod.Elements) Elements {
mapping := make(Elements, 0, len(elements))
for _, element := range elements {
mapping = append(mapping, mappingElement(Element{element}))
}
return mapping
}
func mappingElement(element Element) map[string]any {
return map[string]any{
"attribute": element.Attribute,
"backgroundImage": element.BackgroundImage,
"blur": element.Blur,
"click": element.Click,
"containsElement": element.ContainsElement,
"describe": element.Describe,
"disabled": element.Disabled,
"element": element.NElement,
"elementByJS": element.ElementByJS,
"elementR": element.ElementR,
"elements": element.Elements,
"elementsByJS": element.ElementsByJS,
"elementsX": element.ElementsX,
"equal": element.Equal,
"eval": element.Eval,
"evaluate": element.Evaluate,
"focus": element.Focus,
"frame": element.Frame,
"getSessionID": element.GetSessionID,
"getXPath": element.GetXPath,
"has": element.Has,
"hasR": element.HasR,
"hasX": element.HasX,
"hover": element.Hover,
"html": element.HTML,
"input": element.Input,
"inputTime": element.InputTime,
"interactable": element.Interactable,
"keyActions": element.KeyActions,
"matches": element.Matches,
"moveMouseOut": element.MoveMouseOut,
"next": element.Next,
"overlay": element.Overlay,
"page": element.Page,
"parent": element.Parent,
"parents": element.Parents,
"previous": element.Previous,
"property": element.Property,
"release": element.Release,
"remove": element.Remove,
"resource": element.Resource,
"screenshot": element.Screenshot,
"scrollIntoView": element.ScrollIntoView,
"select": element.Select,
"selectAllText": element.SelectAllText,
"selectText": element.SelectText,
"setFiles": element.SetFiles,
"shadowRoot": element.ShadowRoot,
"shape": element.Shape,
"string": element.String,
"tap": element.Tap,
"text": element.Text,
"type": element.Type,
"visible": element.Visible,
"wait": element.Wait,
"waitEnabled": element.WaitEnabled,
"waitInteractable": element.WaitInteractable,
"waitInvisible": element.WaitInvisible,
"waitLoad": element.WaitLoad,
"waitStable": element.WaitStable,
"waitStableRAF": element.WaitStableRAF,
"waitVisible": element.WaitVisible,
"waitWritable": element.WaitWritable,
}
}
// BackgroundImage returns the css background-image of the element
func (el *Element) BackgroundImage(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
image, err := el.Element.BackgroundImage()
if err != nil {
js.Throw(vm, err)
}
return vm.ToValue(vm.NewArrayBuffer(image))
}
// ContainsElement check if the target is equal or inside the element.
func (el *Element) ContainsElement(element goja.Value) (bool, error) {
value := element.Export().(Element)
return el.Element.ContainsElement(value.Element)
}
// Describe the current element. The depth is the maximum depth at which children should be retrieved, defaults to 1,
// use -1 for the entire subtree or provide an integer larger than 0.
// The pierce decides whether or not iframes and shadow roots should be traversed when returning the subtree.
// The returned proto.DOMNode.NodeID will always be empty, because NodeID is not stable (when proto.DOMDocumentUpdated
// is fired all NodeID on the page will be reassigned to another value)
// we don't recommend using the NodeID, instead, use the BackendNodeID to identify the element.
func (el *Element) Describe(call goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
depth := call.Argument(0).ToInteger()
pierce := call.Argument(1).ToBoolean()
describe, err := el.Element.Describe(int(depth), pierce)
if err != nil {
js.Throw(vm, err)
}
return toJSObject(describe, vm)
}
// NElement returns the first child that matches the css selector
func (el *Element) NElement(selector string) (any, error) {
element, err := el.Element.Element(selector)
if err != nil {
return nil, err
}
return mappingElement(Element{element}), nil
}
// ElementByJS returns the element from the return value of the js
func (el *Element) ElementByJS(call goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
target := toGoStruct[EvalOptions](call.Argument(0), vm)
element, err := el.Element.ElementByJS(target.toRodEvalOptions())
if err != nil {
js.Throw(vm, err)
}
return NewElement(element, vm)
}
// ElementR returns the first child element that matches the css selector and its text matches the jsRegex.
func (el *Element) ElementR(selector, jsRegex string) (any, error) {
element, err := el.Element.ElementR(selector, jsRegex)
if err != nil {
return nil, err
}
return mappingElement(Element{element}), nil
}
// Elements returns all elements that match the css selector
func (el *Element) Elements(selector string) (any, error) {
elements, err := el.Element.Elements(selector)
if err != nil {
return nil, err
}
return NewElements(elements), nil
}
// ElementsByJS returns the elements from the return value of the js
func (el *Element) ElementsByJS(call goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
target := toGoStruct[EvalOptions](call.Argument(0), vm)
elements, err := el.Element.ElementsByJS(target.toRodEvalOptions())
if err != nil {
js.Throw(vm, err)
}
return vm.ToValue(NewElements(elements))
}
// ElementsX returns all elements that match the XPath selector
func (el *Element) ElementsX(xpath string) (any, error) {
elements, err := el.Element.ElementsX(xpath)
if err != nil {
return nil, err
}
return NewElements(elements), nil
}
// Equal checks if the two elements are equal.
func (el *Element) Equal(elm goja.Value) (bool, error) {
value := elm.Export().(Element)
return el.Element.Equal(value.Element)
}
// Eval is a shortcut for Element.Evaluate with AwaitPromise, ByValue and AutoExp set to true.
func (el *Element) Eval(call goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
script := call.Argument(0).String()
args := make([]any, 0, len(call.Arguments)-1)
for _, value := range call.Arguments[1:] {
args = append(args, value.Export())
}
value, err := el.Element.Eval(script, args...)
if err != nil {
js.Throw(vm, err)
}
return toJSObject(value, vm)
}
// Evaluate is just a shortcut of Page.Evaluate with This set to current element.
func (el *Element) Evaluate(call goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
target := toGoStruct[EvalOptions](call.Argument(0), vm)
res, err := el.Element.Evaluate(target.toRodEvalOptions())
if err != nil {
js.Throw(vm, err)
}
return vm.ToValue(res)
}
// Frame creates a page instance that represents the iframe
func (el *Element) Frame(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
return vm.ToValue(mappingPage(Page{el.Element.MustFrame().Context(js.VMContext(vm))}))
}
// Has an element that matches the css selector
func (el *Element) Has(selector string) (bool, any, error) {
has, e, err := el.Element.Has(selector)
if err != nil {
return false, nil, err
}
return has, mappingElement(Element{e}), nil
}
// HasR an element that matches the css selector and its display text matches the jsRegex.
func (el *Element) HasR(selector, jsRegex string) (bool, any, error) {
has, e, err := el.Element.HasR(selector, jsRegex)
if err != nil {
return false, Element{}, err
}
return has, mappingElement(Element{e}), nil
}
// HasX an element that matches the XPath selector
func (el *Element) HasX(selector string) (bool, any, error) {
has, e, err := el.Element.HasX(selector)
if err != nil {
return false, nil, err
}
return has, mappingElement(Element{e}), nil
}
// Interactable checks if the element is interactable with cursor.
// The cursor can be mouse, finger, stylus, etc.
// If not interactable err will be ErrNotInteractable, such as when covered by a modal,
func (el *Element) Interactable(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
interactable, err := el.Element.Interactable()
if err != nil {
js.Throw(vm, err)
}
return toJSObject(interactable, vm)
}
// Next returns the next sibling element in the DOM tree
func (el *Element) Next() any {
return mappingElement(Element{el.Element.MustNext()})
}
// Page of the element
func (el *Element) Page() any {
return mappingPage(Page{el.Element.Page()})
}
// Parent returns the parent element in the DOM tree
func (el *Element) Parent() any {
return mappingElement(Element{el.Element.MustParent()})
}
// Parents that match the selector
func (el *Element) Parents(selector string) any {
return NewElements(el.Element.MustParents(selector))
}
// Previous returns the previous sibling element in the DOM tree
func (el *Element) Previous() any {
return mappingElement(Element{el.Element.MustPrevious()})
}
// Resource returns the "src" content of current element. Such as the jpg of <img src="a.jpg">
func (el *Element) Resource(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
image, err := el.Element.Resource()
if err != nil {
js.Throw(vm, err)
}
return vm.ToValue(vm.NewArrayBuffer(image))
}
// Screenshot of the area of the element
func (el *Element) Screenshot(call goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
var quality int
if !goja.IsUndefined(call.Argument(1)) {
quality = int(call.Argument(1).ToInteger())
}
target := toGoStruct[proto.PageCaptureScreenshotFormat](call.Argument(0), vm)
screenshot, err := el.Element.Screenshot(target, quality)
if err != nil {
js.Throw(vm, err)
}
return vm.ToValue(vm.NewArrayBuffer(screenshot))
}
// ShadowRoot returns the shadow root of this element
func (el *Element) ShadowRoot() any {
return mappingElement(Element{el.Element.MustShadowRoot()})
}
// Shape of the DOM element content. The shape is a group of 4-sides polygons.
// A 4-sides polygon is not necessary a rectangle. 4-sides polygons can be apart from each other.
// For example, we use 2 4-sides polygons to describe the shape below:
//
// ____________ ____________
// / ___/ = /___________/ + _________
// /________/ /________/
func (el *Element) Shape(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
shape, err := el.Element.Shape()
if err != nil {
js.Throw(vm, err)
}
return toJSObject(shape, vm)
}
// Wait until the js returns true
func (el *Element) Wait(call goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
target := toGoStruct[EvalOptions](call.Argument(0), vm)
err := el.Element.Wait(target.toRodEvalOptions())
if err != nil {
js.Throw(vm, err)
}
return goja.Undefined()
}
// WaitInteractable waits for the element to be interactable.
// It will try to scroll to the element on each try.
func (el *Element) WaitInteractable(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value) {
interactable, err := el.Element.WaitInteractable()
if err != nil {
js.Throw(vm, err)
}
return toJSObject(interactable, vm)
}