forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev_helpers.go
196 lines (162 loc) · 4.68 KB
/
dev_helpers.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
// This file defines the helpers to develop automation.
// Such as when running automation we can use trace to visually
// see where the mouse going to click.
package rod
import (
"context"
"fmt"
"html"
"log"
"net/http"
"regexp"
"strings"
"time"
"github.com/go-rod/rod/lib/assets"
"github.com/go-rod/rod/lib/assets/js"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
)
// ServeMonitor starts the monitor server.
// If openBrowser is true, it will try to launcher a browser to play the screenshots.
// The reason why not to use "chrome://inspect/#devices" is one target cannot be driven by multiple controllers.
func (b *Browser) ServeMonitor(host string, openBrowser bool) string {
if host == "" {
return ""
}
u, mux, close := utils.Serve(host)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
httHTML(w, assets.Monitor)
})
mux.HandleFunc("/api/pages", func(w http.ResponseWriter, r *http.Request) {
res, err := proto.TargetGetTargets{}.Call(b)
utils.E(err)
w.WriteHeader(http.StatusOK)
utils.E(w.Write(utils.MustToJSONBytes(res.TargetInfos)))
})
mux.HandleFunc("/page/", func(w http.ResponseWriter, r *http.Request) {
httHTML(w, assets.MonitorPage)
})
mux.HandleFunc("/api/page/", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
info, err := b.pageInfo(proto.TargetTargetID(id))
utils.E(err)
w.WriteHeader(http.StatusOK)
utils.E(w.Write(utils.MustToJSONBytes(info)))
})
mux.HandleFunc("/screenshot/", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
target := proto.TargetTargetID(id)
p := b.MustPageFromTargetID(target)
w.Header().Add("Content-Type", "image/png;")
utils.E(w.Write(p.MustScreenshot()))
})
go func() {
<-b.ctx.Done()
close()
}()
if openBrowser {
launcher.NewBrowser().Open(u)
}
return u
}
// Overlay a rectangle on the main frame with specified message
func (p *Page) Overlay(left, top, width, height float64, msg string) (remove func()) {
root := p.Root()
id := utils.RandString(8)
_, err := root.EvalWithOptions(jsHelper(js.Overlay, JSArgs{
id,
left,
top,
width,
height,
msg,
}))
if err != nil {
p.browser.traceLogErr(err)
}
remove = func() {
_, _ = root.EvalWithOptions(jsHelper(js.RemoveOverlay, JSArgs{id}))
}
return
}
// ExposeJSHelper to page's window object, so you can debug helper.js in the browser console.
// Such as run `rod.elementMatches("div", "ok")` in the browser console to test the Page.ElementMatches.
func (p *Page) ExposeJSHelper() *Page {
p.MustEval(`rod => window.rod = rod`, proto.RuntimeRemoteObjectID(""))
return p
}
// Trace with an overlay on the element
func (el *Element) Trace(msg string) (removeOverlay func()) {
id := utils.RandString(8)
_, err := el.EvalWithOptions(jsHelper(js.ElementOverlay, JSArgs{
id,
msg,
}))
if err != nil {
el.page.browser.traceLogErr(err)
}
removeOverlay = func() {
_, _ = el.EvalWithOptions(jsHelper(js.RemoveOverlay, JSArgs{id}))
}
return
}
// check method and sleep if needed
func (b *Browser) trySlowmotion() {
if b.slowmotion == 0 {
return
}
time.Sleep(b.slowmotion)
}
func (el *Element) tryTrace(msg string) func() {
if !el.page.browser.trace {
return func() {}
}
if !el.page.browser.quiet {
el.page.browser.traceLogAct(msg)
}
return el.Trace(msg)
}
var regHelperJS = regexp.MustCompile(`\A\(rod, \.\.\.args\) => (rod\..+)\.apply\(this, `)
func (p *Page) tryTraceFn(js string, params JSArgs) func() {
if !p.browser.trace {
return func() {}
}
matches := regHelperJS.FindStringSubmatch(js)
if matches != nil {
js = matches[1]
params = params[1:]
}
paramsStr := strings.Trim(mustToJSONForDev(params), "[]\r\n")
if !p.browser.quiet {
p.browser.traceLogJS(js, params)
}
msg := fmt.Sprintf("js <code>%s(%s)</code>", js, html.EscapeString(paramsStr))
return p.Overlay(0, 0, 500, 0, msg)
}
func defaultTraceLogAct(msg string) {
log.Println("act", msg)
}
func defaultTraceLogJS(js string, params JSArgs) {
paramsStr := ""
if len(params) > 0 {
paramsStr = strings.Trim(mustToJSONForDev(params), "[]\r\n")
}
msg := fmt.Sprintf("%s(%s)", js, paramsStr)
log.Println("js", msg)
}
func defaultTraceLogErr(err error) {
if err != context.Canceled && err != context.DeadlineExceeded {
log.Println("[rod trace err]", err)
}
}
func (m *Mouse) initMouseTracer() {
_, _ = m.page.EvalWithOptions(jsHelper(js.InitMouseTracer, JSArgs{m.id, assets.MousePointer}))
}
func (m *Mouse) updateMouseTracer() bool {
res, err := m.page.EvalWithOptions(jsHelper(js.UpdateMouseTracer, JSArgs{m.id, m.x, m.y}))
if err != nil {
return true
}
return res.Value.Bool()
}