This repository has been archived by the owner on May 31, 2022. It is now read-only.
forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.go
121 lines (107 loc) · 3.33 KB
/
js.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
package chromedp
import (
"fmt"
"github.com/chromedp/cdproto/cdp"
)
const (
// textJS is a javascript snippet that returns the concatenated innerText of all
// visible (ie, offsetWidth || offsetHeight || getClientRects().length ) children.
textJS = `(function(a) {
var s = '';
for (var i = 0; i < a.length; i++) {
if (a[i].offsetWidth || a[i].offsetHeight || a[i].getClientRects().length) {
s += a[i].innerText;
}
}
return s;
})(%s)`
// textContentJS is a javascript snippet that returns the concatenated textContent
// of all children.
textContentJS = `(function(a) {
var s = '';
for (var i = 0; i < a.length; i++) {
s += a[i].textContent;
}
return s;
})(%s)`
// blurJS is a javscript snippet that blurs the specified element.
blurJS = `(function(a) {
a.blur();
return true;
})(%s)`
// scrollIntoViewJS is a javascript snippet that scrolls the specified node
// into the window's viewport (if needed), returning the actual window x/y
// after execution.
scrollIntoViewJS = `(function(a) {
a.scrollIntoViewIfNeeded(true);
return [window.scrollX, window.scrollY];
})(%s)`
// submitJS is a javascript snippet that will call the containing form's
// submit function, returning true or false if the call was successful.
submitJS = `(function(a) {
if (a.nodeName === 'FORM') {
a.submit();
return true;
} else if (a.form !== null) {
a.form.submit();
return true;
}
return false;
})(%s)`
// resetJS is a javascript snippet that will call the containing form's
// reset function, returning true or false if the call was successful.
resetJS = `(function(a) {
if (a.nodeName === 'FORM') {
a.reset();
return true;
} else if (a.form !== null) {
a.form.reset();
return true;
}
return false;
})(%s)`
// attributeJS is a javascript snippet that returns the attribute of a specified
// node.
attributeJS = `(function(a, n) {
return a[n];
})(%s, %q)`
// setAttributeJS is a javascript snippet that sets the value of the specified
// node, and returns the value.
setAttributeJS = `(function(a, n, v) {
return a[n] = v;
})(%s, %q, %q)`
// visibleJS is a javascript snippet that returns true or false depending on if
// the specified node's offsetWidth, offsetHeight or getClientRects().length is
// not null.
visibleJS = `(function(a) {
return Boolean( a.offsetWidth || a.offsetHeight || a.getClientRects().length );
})(%s)`
)
// snippet builds a Javascript expression snippet.
func snippet(js string, f func(n *cdp.Node) string, sel interface{}, n *cdp.Node, v ...interface{}) string {
switch s := sel.(type) {
case *Selector:
if s != nil && s.raw {
return fmt.Sprintf(js, append([]interface{}{s.selAsString()}, v...)...)
}
}
return fmt.Sprintf(js, append([]interface{}{f(n)}, v...)...)
}
// cashX returns the $x() expression using the node's full xpath value.
func cashX(flatten bool) func(*cdp.Node) string {
return func(n *cdp.Node) string {
if flatten {
return fmt.Sprintf(`$x(%q)[0]`, n.FullXPath())
}
return fmt.Sprintf(`$x(%q)`, n.FullXPath())
}
}
// cashXNode returns the $x(/node()) expression using the node's full xpath value.
func cashXNode(flatten bool) func(*cdp.Node) string {
return func(n *cdp.Node) string {
if flatten {
return fmt.Sprintf(`$x(%q)[0]`, n.FullXPath()+"/node()")
}
return fmt.Sprintf(`$x(%q)`, n.FullXPath()+"/node()")
}
}