forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
194 lines (166 loc) · 4.41 KB
/
utils.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
package rod
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"path/filepath"
"reflect"
"regexp"
"time"
"github.com/go-rod/rod/lib/cdp"
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/kit"
)
// Sleeper returns the default sleeper for retry, it uses backoff to grow the interval.
// The growth looks like: A(0) = 100ms, A(n) = A(n-1) * random[1.9, 2.1), A(n) < 1s
var Sleeper = func() kit.Sleeper {
return kit.BackoffSleeper(100*time.Millisecond, time.Second, nil)
}
// Array of any type
type Array []interface{}
// ArrayFromList converts a random list into Array type
func ArrayFromList(list interface{}) Array {
arr := Array{}
val := reflect.ValueOf(list)
for i := 0; i < val.Len(); i++ {
arr = append(arr, val.Index(i).Interface())
}
return arr
}
// SprintFnThis wrap js with this, wrap function call if it's js expression
func SprintFnThis(js string) string {
if detectJSFunction(js) {
return fmt.Sprintf(`function() { return (%s).apply(this, arguments) }`, js)
}
return fmt.Sprintf(`function() { return %s }`, js)
}
const jsHelperID = proto.RuntimeRemoteObjectID("rodJSHelper")
// Convert name and jsArgs to Page.Eval, the name is method name in the "lib/assets/helper.js".
func jsHelper(name string, jsArgs Array) (string, Array) {
jsArgs = append(Array{jsHelperID}, jsArgs...)
js := fmt.Sprintf(`(rod, ...args) => rod.%s.apply(this, args)`, name)
return js, jsArgs
}
// Event helps to convert a cdp.Event to proto.Payload. Returns false if the conversion fails
func Event(msg *cdp.Event, evt proto.Payload) bool {
if msg.Method == evt.MethodName() {
err := json.Unmarshal(msg.Params, evt)
return err == nil
}
return false
}
// Try try fn with recover, return the panic as value
func Try(fn func()) (err error) {
defer func() {
if val := recover(); val != nil {
var ok bool
err, ok = val.(error)
if !ok {
err = fmt.Errorf("%w: %s", newErr(ErrValue, val), kit.MustToJSON(val))
}
}
}()
fn()
return err
}
func isNilContextErr(err error) bool {
if err == nil {
return false
}
cdpErr, ok := err.(*cdp.Error)
return ok && cdpErr.Code == -32000 && cdpErr.Message != "Argument should belong to the same JavaScript world as target object"
}
func matchWithFilter(s string, includes, excludes []string) bool {
for _, include := range includes {
if regexp.MustCompile(include).MatchString(s) {
for _, exclude := range excludes {
if regexp.MustCompile(exclude).MatchString(s) {
return false
}
}
return true
}
}
return false
}
func saveScreenshot(bin []byte, toFile []string) error {
if len(toFile) == 0 {
return nil
}
if toFile[0] == "" {
toFile = []string{"tmp", "screenshots", fmt.Sprintf("%d", time.Now().UnixNano()) + ".png"}
}
return kit.OutputFile(filepath.Join(toFile...), bin, nil)
}
func ginHTML(ctx kit.GinContext, body string) {
ctx.Header("Content-Type", "text/html; charset=utf-8")
_, _ = ctx.Writer.WriteString(body)
}
func mustToJSONForDev(value interface{}) string {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
kit.E(enc.Encode(value))
return buf.String()
}
// detect if a js string is a function definition
var regFn = regexp.MustCompile(`\A\s*function\s*\(`)
// detect if a js string is a function definition
// Samples:
//
// function () {}
// a => {}
// (a, b, c) =>
// ({a: b}, ...list) => {}
func detectJSFunction(js string) bool {
if regFn.MatchString(js) {
return true
}
// The algorithm is pretty simple, the braces before "=>" must be balanced.
// Such as "foo(() => {})", there are 2 "(", but only 1 ")".
// Here we use a simple state machine.
balanced := true
last := ' '
for _, r := range js {
if r == '(' {
if balanced {
balanced = false
} else {
return false
}
}
if r == ')' {
if balanced {
return false
}
balanced = true
}
if last == '=' {
if r == '>' {
if balanced {
return true
}
return false
}
return false
}
last = r
}
return false
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
var regDataURI = regexp.MustCompile(`\Adata:(.+?)?(;base64)?,`)
func parseDataURI(uri string) (string, []byte) {
matches := regDataURI.FindStringSubmatch(uri)
l := len(matches[0])
contentType := matches[1]
codec := matches[2]
if codec == ";base64" {
bin, _ := base64.StdEncoding.DecodeString(uri[l:])
return contentType, bin
}
s, _ := url.PathUnescape(uri[l:])
return matches[1], []byte(s)
}