forked from ydcool/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser.go
393 lines (331 loc) · 10.3 KB
/
browser.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//go:generate go run ./lib/utils/install-deps
//go:generate go run ./lib/utils/lint
//go:generate go run ./lib/proto/generate
//go:generate go run ./lib/assets/generate
//go:generate go run ./lib/devices/generate
//go:generate go run ./lib/launcher/revision
package rod
import (
"context"
"encoding/json"
"reflect"
"sync"
"time"
"github.com/go-rod/rod/lib/cdp"
"github.com/go-rod/rod/lib/defaults"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/goob"
)
// Browser implements the proto.Caller interface
var _ proto.Caller = &Browser{}
// Browser represents the browser.
// It doesn't depends on file system, it should work with remote browser seamlessly.
// To check the env var you can use to quickly enable options from CLI, check here:
// https://pkg.go.dev/github.com/go-rod/rod/lib/defaults
type Browser struct {
// these are the handler for ctx
ctx context.Context
sleeper func() utils.Sleeper
// BrowserContextID is the id for incognito window
BrowserContextID proto.BrowserBrowserContextID
slowmotion time.Duration // see defaults.slow
quiet bool // see defaults.Quiet
trace bool // see defaults.Trace
traceLog TraceLog
defaultViewport *proto.EmulationSetDeviceMetricsOverride
client Client
event *goob.Observable // all the browser events from cdp client
targetsLock *sync.Mutex
// stores all the previous cdp call of same type. Browser doesn't have enough API
// for us to retrieve all its internal states. This is an workaround to map them to local.
// For example you can't use cdp API to get the current position of mouse.
states *sync.Map
}
// New creates a controller
func New() *Browser {
return &Browser{
ctx: context.Background(),
sleeper: DefaultSleeper,
slowmotion: defaults.Slow,
quiet: defaults.Quiet,
trace: defaults.Trace,
traceLog: defaultTraceLog,
defaultViewport: &proto.EmulationSetDeviceMetricsOverride{
Width: 1200, Height: 900, DeviceScaleFactor: 1, Mobile: false,
ScreenOrientation: &proto.EmulationScreenOrientation{
Type: proto.EmulationScreenOrientationTypeLandscapePrimary,
},
},
targetsLock: &sync.Mutex{},
states: &sync.Map{},
}
}
// Incognito creates a new incognito browser
func (b *Browser) Incognito() (*Browser, error) {
res, err := proto.TargetCreateBrowserContext{}.Call(b)
if err != nil {
return nil, err
}
incognito := *b
incognito.BrowserContextID = res.BrowserContextID
return &incognito, nil
}
// ControlURL set the url to remote control browser.
func (b *Browser) ControlURL(url string) *Browser {
b.client = cdp.New(url)
return b
}
// Slowmotion set the delay for each control action, such as the simulation of the human inputs
func (b *Browser) Slowmotion(delay time.Duration) *Browser {
b.slowmotion = delay
return b
}
// Trace enables/disables the visual tracing of the input actions on the page
func (b *Browser) Trace(enable bool) *Browser {
b.trace = enable
return b
}
// TraceLog overrides the default log functions for tracing
func (b *Browser) TraceLog(l TraceLog) *Browser {
if l == nil {
b.traceLog = defaultTraceLog
} else {
b.traceLog = l
}
return b
}
// Quiet enables/disables log of the. Only useful when Trace is set to true.
func (b *Browser) Quiet(quiet bool) *Browser {
b.quiet = quiet
return b
}
// Client set the cdp client
func (b *Browser) Client(c Client) *Browser {
b.client = c
return b
}
// DefaultViewport sets the default viewport for new page in the future. Default size is 1200x900.
// Set it to nil to disable it.
func (b *Browser) DefaultViewport(viewport *proto.EmulationSetDeviceMetricsOverride) *Browser {
b.defaultViewport = viewport
return b
}
// Connect doc is similar to the method MustConnect
func (b *Browser) Connect() error {
if b.client == nil {
u := defaults.URL
if defaults.Remote {
if u == "" {
u = "ws://127.0.0.1:9222"
}
b.client = launcher.NewRemote(u).Client()
} else {
if u == "" {
u = launcher.New().Context(b.ctx).MustLaunch()
}
b.client = cdp.New(u)
}
}
err := b.client.Connect(b.ctx)
if err != nil {
return err
}
b.initEvents()
if defaults.Monitor != "" {
launcher.NewBrowser().Open(b.ServeMonitor(defaults.Monitor))
}
return nil
}
// Close doc is similar to the method MustClose
func (b *Browser) Close() error {
return proto.BrowserClose{}.Call(b)
}
// Page doc is similar to the method MustPage
// If url is empty, the default target will be "about:blank".
func (b *Browser) Page(url string) (p *Page, err error) {
target, err := proto.TargetCreateTarget{
URL: "about:blank",
BrowserContextID: b.BrowserContextID,
}.Call(b)
if err != nil {
return nil, err
}
defer func() {
// If Navigate or PageFromTarget fails we should close the target to prevent leak
if err != nil {
_, _ = proto.TargetCloseTarget{TargetID: target.TargetID}.Call(b)
}
}()
p, err = b.PageFromTarget(target.TargetID)
if err == nil && url != "" { // no need to navigate if url is empty
err = p.Navigate(url)
}
return
}
// Pages doc is similar to the method MustPages
func (b *Browser) Pages() (Pages, error) {
list, err := proto.TargetGetTargets{}.Call(b)
if err != nil {
return nil, err
}
pageList := Pages{}
for _, target := range list.TargetInfos {
if target.Type != "page" {
continue
}
page, err := b.PageFromTarget(target.TargetID)
if err != nil {
return nil, err
}
pageList = append(pageList, page)
}
return pageList, nil
}
// Event returns the observable for browser events
func (b *Browser) Event() *goob.Observable {
return b.event
}
// EachEvent of the specified event type, if any callback returns true the event loop will stop.
func (b *Browser) EachEvent(callbacks ...interface{}) (wait func()) {
return b.eachEvent(b.ctx, "", callbacks...)
}
// WaitEvent waits for the next event for one time. It will also load the data into the event object.
func (b *Browser) WaitEvent(e proto.Payload) (wait func()) {
return b.waitEvent(b.ctx, "", e)
}
// If the any callback returns true the event loop will stop.
// It will enable the related domains if not enabled, and recover them after wait ends.
func (b *Browser) eachEvent(
ctx context.Context,
sessionID proto.TargetSessionID,
callbacks ...interface{},
) (wait func()) {
cbValues := make([]reflect.Value, len(callbacks))
eventTypes := make([]reflect.Type, len(callbacks))
recovers := make([]func(), len(callbacks))
for i, cb := range callbacks {
cbValues[i] = reflect.ValueOf(cb)
eType := cbValues[i].Type().In(0).Elem()
eventTypes[i] = eType
// Only enabled domains will emit events to cdp client.
// We enable the domains for the event types if it's not enabled.
// We recover the domains to their previous states after the wait ends.
domain, _ := proto.ParseMethodName(reflect.New(eType).Interface().(proto.Payload).MethodName())
var enable proto.Payload
if domain == "Target" { // only Target domain is special
enable = proto.TargetSetDiscoverTargets{Discover: true}
} else {
enable = reflect.New(proto.GetType(domain + ".enable")).Interface().(proto.Payload)
}
recovers[i] = b.Context(ctx).EnableDomain(sessionID, enable)
}
ctx, cancel := context.WithCancel(ctx)
stream := b.event.Subscribe(ctx)
return func() {
defer func() {
cancel()
stream = nil
for _, recover := range recovers {
recover()
}
}()
if stream == nil {
panic("can't use wait function twice")
}
// Check each event, if an event matches the the type of the arg call the fn with the even.
goob.Each(stream, func(e *cdp.Event) bool {
for i, eType := range eventTypes {
eVal := reflect.New(eType)
if Event(e, eVal.Interface().(proto.Payload)) {
// The type of callback can be one of:
// func(e proto.Payload) bool
// func(e proto.Payload)
res := cbValues[i].Call([]reflect.Value{eVal})
if len(res) > 0 {
return res[0].Bool()
}
break
}
}
return false
})
}
}
// waits for the next event for one time. It will also load the data into the event object.
func (b *Browser) waitEvent(ctx context.Context, sessionID proto.TargetSessionID, e proto.Payload) (wait func()) {
valE := reflect.ValueOf(e)
valTrue := reflect.ValueOf(true)
// dynamically creates a function on runtime:
//
// func(ee proto.Payload) bool {
// *e = *ee
// return true
// }
fnType := reflect.FuncOf([]reflect.Type{valE.Type()}, []reflect.Type{valTrue.Type()}, false)
fnVal := reflect.MakeFunc(fnType, func(args []reflect.Value) []reflect.Value {
valE.Elem().Set(args[0].Elem())
return []reflect.Value{valTrue}
})
return b.eachEvent(ctx, sessionID, fnVal.Interface())
}
// Call raw cdp interface directly
func (b *Browser) Call(ctx context.Context, sessionID, methodName string, params json.RawMessage) (res []byte, err error) {
res, err = b.client.Call(ctx, sessionID, methodName, params)
if err != nil {
return nil, err
}
b.set(proto.TargetSessionID(sessionID), methodName, params)
return
}
// CallContext parameters for proto
func (b *Browser) CallContext() (context.Context, proto.Client, string) {
return b.ctx, b, ""
}
// PageFromTarget creates a Page instance from a targetID
func (b *Browser) PageFromTarget(targetID proto.TargetTargetID) (*Page, error) {
b.targetsLock.Lock()
defer b.targetsLock.Unlock()
page := b.loadPage(targetID)
if page != nil {
return page, nil
}
page = (&Page{
sleeper: b.sleeper,
jsContextLock: &sync.Mutex{},
browser: b,
TargetID: targetID,
executionIDs: map[proto.PageFrameID]proto.RuntimeExecutionContextID{},
}).Context(b.ctx)
page.Mouse = &Mouse{lock: &sync.Mutex{}, page: page, id: utils.RandString(8)}
page.Keyboard = &Keyboard{lock: &sync.Mutex{}, page: page}
err := page.initSession()
if err != nil {
return nil, err
}
if b.defaultViewport != nil {
err = page.Viewport(b.defaultViewport)
if err != nil {
return nil, err
}
}
b.storePage(page)
return page, nil
}
func (b *Browser) initEvents() {
b.event = goob.New()
go func() {
for msg := range b.client.Event() {
b.event.Publish(msg)
}
}()
}
// InfoE of the page
func (b *Browser) pageInfo(id proto.TargetTargetID) (*proto.TargetTargetInfo, error) {
res, err := proto.TargetGetTargetInfo{TargetID: id}.Call(b)
if err != nil {
return nil, err
}
return res.TargetInfo, nil
}