forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.go
763 lines (639 loc) · 19.4 KB
/
page.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
package rod
import (
"context"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"sync"
"time"
"github.com/go-rod/rod/lib/devices"
"github.com/go-rod/rod/lib/js"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/goob"
"github.com/ysmood/gson"
)
// Page implements these interfaces
var _ proto.Client = &Page{}
var _ proto.Contextable = &Page{}
var _ proto.Sessionable = &Page{}
// Page represents the webpage.
// We try to hold as less states as possible.
// When a page is closed by Rod or not all the ongoing operations an events on it will abort.
type Page struct {
// TargetID is a unique ID for a remote page.
// It's usually used in events sent from the browser to tell which page an event belongs to.
TargetID proto.TargetTargetID
// FrameID is a unique ID for a browsing context.
// Usually, different FrameID means different javascript execution context.
// Such as an iframe and the page it belongs to will have the same TargetID but different FrameIDs.
FrameID proto.PageFrameID
// SessionID is a unique ID for a page attachment to a controller.
// It's usually used in transport layer to tell which page to send the control signal.
// A page can attached to multiple controllers, the browser uses it distinguish controllers.
SessionID proto.TargetSessionID
e eFunc
ctx context.Context
// Used to abort all ongoing actions when a page closes.
sessionCancel func()
root *Page
sleeper func() utils.Sleeper
browser *Browser
event *goob.Observable
// devices
Mouse *Mouse
Keyboard *Keyboard
Touch *Touch
element *Element // iframe only
jsCtxLock *sync.Mutex
jsCtxID *proto.RuntimeRemoteObjectID // use pointer so that page clones can share the change
helpersLock *sync.Mutex
helpers map[proto.RuntimeRemoteObjectID]map[string]proto.RuntimeRemoteObjectID
}
// String interface
func (p *Page) String() string {
id := p.TargetID
if len(id) > 8 {
id = id[:8]
}
return fmt.Sprintf("<page:%s>", id)
}
// IsIframe tells if it's iframe
func (p *Page) IsIframe() bool {
return p.element != nil
}
// GetSessionID interface
func (p *Page) GetSessionID() proto.TargetSessionID {
return p.SessionID
}
// Browser of the page
func (p *Page) Browser() *Browser {
return p.browser
}
// Info of the page, such as the URL or title of the page
func (p *Page) Info() (*proto.TargetTargetInfo, error) {
return p.browser.pageInfo(p.TargetID)
}
// HTML of the page
func (p *Page) HTML() (string, error) {
el, err := p.Element("html")
if err != nil {
return "", err
}
return el.HTML()
}
// Cookies returns the page cookies. By default it will return the cookies for current page.
// The urls is the list of URLs for which applicable cookies will be fetched.
func (p *Page) Cookies(urls []string) ([]*proto.NetworkCookie, error) {
if len(urls) == 0 {
info, err := p.Info()
if err != nil {
return nil, err
}
urls = []string{info.URL}
}
res, err := proto.NetworkGetCookies{Urls: urls}.Call(p)
if err != nil {
return nil, err
}
return res.Cookies, nil
}
// SetCookies is similar to Browser.SetCookies .
func (p *Page) SetCookies(cookies []*proto.NetworkCookieParam) error {
if cookies == nil {
return proto.NetworkClearBrowserCookies{}.Call(p)
}
return proto.NetworkSetCookies{Cookies: cookies}.Call(p)
}
// SetExtraHeaders whether to always send extra HTTP headers with the requests from this page.
func (p *Page) SetExtraHeaders(dict []string) (func(), error) {
headers := proto.NetworkHeaders{}
for i := 0; i < len(dict); i += 2 {
headers[dict[i]] = gson.New(dict[i+1])
}
return p.EnableDomain(&proto.NetworkEnable{}), proto.NetworkSetExtraHTTPHeaders{Headers: headers}.Call(p)
}
// SetUserAgent (browser brand, accept-language, etc) of the page.
// If req is nil, a default user agent will be used, a typical mac chrome.
func (p *Page) SetUserAgent(req *proto.NetworkSetUserAgentOverride) error {
if req == nil {
req = devices.LaptopWithMDPIScreen.UserAgentEmulation()
}
return req.Call(p)
}
// Navigate to the url. If the url is empty, "about:blank" will be used.
// It will return immediately after the server responds the http header.
func (p *Page) Navigate(url string) error {
if url == "" {
url = "about:blank"
}
err := p.StopLoading()
if err != nil {
return err
}
res, err := proto.PageNavigate{URL: url}.Call(p)
if err != nil {
return err
}
if res.ErrorText != "" {
return &ErrNavigation{res.ErrorText}
}
p.root.unsetJSCtxID()
return nil
}
// NavigateBack history.
func (p *Page) NavigateBack() error {
// Not using cdp API because it doesn't work for iframe
_, err := p.Evaluate(Eval(`() => history.back()`).ByUser())
return err
}
// NavigateForward history.
func (p *Page) NavigateForward() error {
// Not using cdp API because it doesn't work for iframe
_, err := p.Evaluate(Eval(`() => history.forward()`).ByUser())
return err
}
// Reload page.
func (p *Page) Reload() error {
p, cancel := p.WithCancel()
defer cancel()
wait := p.EachEvent(func(e *proto.PageFrameNavigated) bool {
return e.Frame.ID == p.FrameID
})
// Not using cdp API because it doesn't work for iframe
_, err := p.Evaluate(Eval(`() => location.reload()`).ByUser())
if err != nil {
return err
}
wait()
p.unsetJSCtxID()
return nil
}
// Activate (focuses) the page
func (p *Page) Activate() (*Page, error) {
err := proto.TargetActivateTarget{TargetID: p.TargetID}.Call(p.browser)
return p, err
}
func (p *Page) getWindowID() (proto.BrowserWindowID, error) {
res, err := proto.BrowserGetWindowForTarget{TargetID: p.TargetID}.Call(p)
if err != nil {
return 0, err
}
return res.WindowID, err
}
// GetWindow position and size info
func (p *Page) GetWindow() (*proto.BrowserBounds, error) {
id, err := p.getWindowID()
if err != nil {
return nil, err
}
res, err := proto.BrowserGetWindowBounds{WindowID: id}.Call(p)
if err != nil {
return nil, err
}
return res.Bounds, nil
}
// SetWindow location and size
func (p *Page) SetWindow(bounds *proto.BrowserBounds) error {
id, err := p.getWindowID()
if err != nil {
return err
}
err = proto.BrowserSetWindowBounds{WindowID: id, Bounds: bounds}.Call(p)
return err
}
// SetViewport overrides the values of device screen dimensions
func (p *Page) SetViewport(params *proto.EmulationSetDeviceMetricsOverride) error {
if params == nil {
return proto.EmulationClearDeviceMetricsOverride{}.Call(p)
}
return params.Call(p)
}
// SetDocumentContent sets the page document html content
func (p *Page) SetDocumentContent(html string) error {
return proto.PageSetDocumentContent{
FrameID: p.FrameID,
HTML: html,
}.Call(p)
}
// Emulate the device, such as iPhone9. If device is devices.Clear, it will clear the override.
func (p *Page) Emulate(device devices.Device) error {
err := p.SetViewport(device.MetricsEmulation())
if err != nil {
return err
}
err = device.TouchEmulation().Call(p)
if err != nil {
return err
}
return p.SetUserAgent(device.UserAgentEmulation())
}
// StopLoading forces the page stop navigation and pending resource fetches.
func (p *Page) StopLoading() error {
return proto.PageStopLoading{}.Call(p)
}
// Close tries to close page, running its beforeunload hooks, if has any.
func (p *Page) Close() error {
p.browser.targetsLock.Lock()
defer p.browser.targetsLock.Unlock()
success := true
ctx, cancel := context.WithCancel(p.ctx)
defer cancel()
messages := p.browser.Context(ctx).Event()
err := proto.PageClose{}.Call(p)
if err != nil {
return err
}
for msg := range messages {
stop := false
destroyed := proto.TargetTargetDestroyed{}
closed := proto.PageJavascriptDialogClosed{}
if msg.Load(&destroyed) {
stop = destroyed.TargetID == p.TargetID
} else if msg.SessionID == p.SessionID && msg.Load(&closed) {
success = closed.Result
stop = !success
}
if stop {
break
}
}
if success {
p.cleanupStates()
} else {
return &ErrPageCloseCanceled{}
}
return nil
}
// HandleDialog accepts or dismisses next JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload).
// Because modal dialog will block js, usually you have to trigger the dialog in another goroutine.
// For example:
//
// wait, handle := page.MustHandleDialog()
// go page.MustElement("button").MustClick()
// wait()
// handle(true, "")
//
func (p *Page) HandleDialog() (
wait func() *proto.PageJavascriptDialogOpening,
handle func(*proto.PageHandleJavaScriptDialog) error,
) {
restore := p.EnableDomain(&proto.PageEnable{})
var e proto.PageJavascriptDialogOpening
w := p.WaitEvent(&e)
return func() *proto.PageJavascriptDialogOpening {
w()
return &e
}, func(h *proto.PageHandleJavaScriptDialog) error {
defer restore()
return h.Call(p)
}
}
// Screenshot captures the screenshot of current page.
func (p *Page) Screenshot(fullpage bool, req *proto.PageCaptureScreenshot) ([]byte, error) {
if req == nil {
req = &proto.PageCaptureScreenshot{}
}
if fullpage {
metrics, err := proto.PageGetLayoutMetrics{}.Call(p)
if err != nil {
return nil, err
}
oldView := proto.EmulationSetDeviceMetricsOverride{}
set := p.LoadState(&oldView)
view := oldView
view.Width = int(metrics.CSSContentSize.Width)
view.Height = int(metrics.CSSContentSize.Height)
err = p.SetViewport(&view)
if err != nil {
return nil, err
}
defer func() { // try to recover the viewport
if !set {
_ = proto.EmulationClearDeviceMetricsOverride{}.Call(p)
return
}
_ = p.SetViewport(&oldView)
}()
}
shot, err := req.Call(p)
if err != nil {
return nil, err
}
return shot.Data, nil
}
// PDF prints page as PDF
func (p *Page) PDF(req *proto.PagePrintToPDF) (*StreamReader, error) {
req.TransferMode = proto.PagePrintToPDFTransferModeReturnAsStream
res, err := req.Call(p)
if err != nil {
return nil, err
}
return NewStreamReader(p, res.Stream), nil
}
// GetResource content by the url. Such as image, css, html, etc.
// Use the proto.PageGetResourceTree to list all the resources.
func (p *Page) GetResource(url string) ([]byte, error) {
res, err := proto.PageGetResourceContent{
FrameID: p.FrameID,
URL: url,
}.Call(p)
if err != nil {
return nil, err
}
data := res.Content
var bin []byte
if res.Base64Encoded {
bin, err = base64.StdEncoding.DecodeString(data)
utils.E(err)
} else {
bin = []byte(data)
}
return bin, nil
}
// WaitOpen waits for the next new page opened by the current one
func (p *Page) WaitOpen() func() (*Page, error) {
var targetID proto.TargetTargetID
b := p.browser.Context(p.ctx)
wait := b.EachEvent(func(e *proto.TargetTargetCreated) bool {
targetID = e.TargetInfo.TargetID
return e.TargetInfo.OpenerID == p.TargetID
})
return func() (*Page, error) {
defer p.tryTrace(TraceTypeWait, "wait open")()
wait()
return b.PageFromTarget(targetID)
}
}
// EachEvent of the specified event types, if any callback returns true the wait function will resolve,
// The type of each callback is (? means optional):
//
// func(proto.Event, proto.TargetSessionID?) bool?
//
// You can listen to multiple event types at the same time like:
//
// browser.EachEvent(func(a *proto.A) {}, func(b *proto.B) {})
//
// Such as subscribe the events to know when the navigation is complete or when the page is rendered.
// Here's an example to dismiss all dialogs/alerts on the page:
//
// go page.EachEvent(func(e *proto.PageJavascriptDialogOpening) {
// _ = proto.PageHandleJavaScriptDialog{ Accept: false, PromptText: ""}.Call(page)
// })()
//
func (p *Page) EachEvent(callbacks ...interface{}) (wait func()) {
return p.browser.Context(p.ctx).eachEvent(p.SessionID, callbacks...)
}
// WaitEvent waits for the next event for one time. It will also load the data into the event object.
func (p *Page) WaitEvent(e proto.Event) (wait func()) {
defer p.tryTrace(TraceTypeWait, "event", e.ProtoEvent())()
return p.browser.Context(p.ctx).waitEvent(p.SessionID, e)
}
// WaitNavigation wait for a page lifecycle event when navigating.
// Usually you will wait for proto.PageLifecycleEventNameNetworkAlmostIdle
func (p *Page) WaitNavigation(name proto.PageLifecycleEventName) func() {
_ = proto.PageSetLifecycleEventsEnabled{Enabled: true}.Call(p)
wait := p.EachEvent(func(e *proto.PageLifecycleEvent) bool {
return e.Name == name
})
return func() {
defer p.tryTrace(TraceTypeWait, "navigation", name)()
wait()
_ = proto.PageSetLifecycleEventsEnabled{Enabled: false}.Call(p)
}
}
// WaitRequestIdle returns a wait function that waits until no request for d duration.
// Be careful, d is not the max wait timeout, it's the least idle time.
// If you want to set a timeout you can use the "Page.Timeout" function.
// Use the includes and excludes regexp list to filter the requests by their url.
func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string) func() {
if len(includes) == 0 {
includes = []string{""}
}
p, cancel := p.WithCancel()
match := genRegMatcher(includes, excludes)
waitlist := map[proto.NetworkRequestID]string{}
idleCounter := utils.NewIdleCounter(d)
update := p.tryTraceReq(includes, excludes)
update(nil)
checkDone := func(id proto.NetworkRequestID) {
if _, has := waitlist[id]; has {
delete(waitlist, id)
update(waitlist)
idleCounter.Done()
}
}
wait := p.EachEvent(func(sent *proto.NetworkRequestWillBeSent) {
if match(sent.Request.URL) {
// Redirect will send multiple NetworkRequestWillBeSent events with the same RequestID,
// we should filter them out.
if _, has := waitlist[sent.RequestID]; !has {
waitlist[sent.RequestID] = sent.Request.URL
update(waitlist)
idleCounter.Add()
}
}
}, func(e *proto.NetworkLoadingFinished) {
checkDone(e.RequestID)
}, func(e *proto.NetworkLoadingFailed) {
checkDone(e.RequestID)
})
return func() {
go func() {
idleCounter.Wait(p.ctx)
cancel()
}()
wait()
}
}
// WaitIdle waits until the next window.requestIdleCallback is called.
func (p *Page) WaitIdle(timeout time.Duration) (err error) {
_, err = p.Evaluate(evalHelper(js.WaitIdle, timeout.Seconds()).ByPromise())
return err
}
// WaitRepaint waits until the next repaint.
// Doc: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
func (p *Page) WaitRepaint() error {
// we use root here because iframe doesn't trigger requestAnimationFrame
_, err := p.root.Eval(`() => new Promise(r => requestAnimationFrame(r))`)
return err
}
// WaitLoad waits for the `window.onload` event, it returns immediately if the event is already fired.
func (p *Page) WaitLoad() error {
defer p.tryTrace(TraceTypeWait, "load")()
_, err := p.Evaluate(evalHelper(js.WaitLoad).ByPromise())
return err
}
// AddScriptTag to page. If url is empty, content will be used.
func (p *Page) AddScriptTag(url, content string) error {
hash := md5.Sum([]byte(url + content))
id := hex.EncodeToString(hash[:])
_, err := p.Evaluate(evalHelper(js.AddScriptTag, id, url, content).ByPromise())
return err
}
// AddStyleTag to page. If url is empty, content will be used.
func (p *Page) AddStyleTag(url, content string) error {
hash := md5.Sum([]byte(url + content))
id := hex.EncodeToString(hash[:])
_, err := p.Evaluate(evalHelper(js.AddStyleTag, id, url, content).ByPromise())
return err
}
// EvalOnNewDocument Evaluates given script in every frame upon creation (before loading frame's scripts).
func (p *Page) EvalOnNewDocument(js string) (remove func() error, err error) {
res, err := proto.PageAddScriptToEvaluateOnNewDocument{Source: js}.Call(p)
if err != nil {
return
}
remove = func() error {
return proto.PageRemoveScriptToEvaluateOnNewDocument{
Identifier: res.Identifier,
}.Call(p)
}
return
}
// Wait until the js returns true
func (p *Page) Wait(this *proto.RuntimeRemoteObject, js string, params []interface{}) error {
return utils.Retry(p.ctx, p.sleeper(), func() (bool, error) {
opts := Eval(js, params...).ByPromise().This(this)
res, err := p.Evaluate(opts)
if err != nil {
return true, err
}
return res.Value.Bool(), nil
})
}
// WaitElementsMoreThan Wait until there are more than <num> <selector> elements.
func (p *Page) WaitElementsMoreThan(selector string, num int) error {
return p.Wait(nil, `(s, n) => document.querySelectorAll(s).length > n`, []interface{}{selector, num})
}
// ObjectToJSON by object id
func (p *Page) ObjectToJSON(obj *proto.RuntimeRemoteObject) (gson.JSON, error) {
if obj.ObjectID == "" {
return obj.Value, nil
}
res, err := proto.RuntimeCallFunctionOn{
ObjectID: obj.ObjectID,
FunctionDeclaration: `function() { return this }`,
ReturnByValue: true,
}.Call(p)
if err != nil {
return gson.New(nil), err
}
return res.Result.Value, nil
}
// ElementFromObject creates an Element from the remote object id.
func (p *Page) ElementFromObject(obj *proto.RuntimeRemoteObject) (*Element, error) {
// If the element is in an iframe, we need the jsCtxID to inject helper.js to the correct context.
id, err := p.jsCtxIDByObjectID(obj.ObjectID)
if err != nil {
return nil, err
}
pid, err := p.getJSCtxID()
if err != nil {
return nil, err
}
if id != pid {
clone := *p
clone.jsCtxID = &id
p = &clone
}
return &Element{
e: p.e,
ctx: p.ctx,
sleeper: p.sleeper,
page: p,
Object: obj,
}, nil
}
// ElementFromNode creates an Element from the node, NodeID or BackendNodeID must be specified.
func (p *Page) ElementFromNode(node *proto.DOMNode) (*Element, error) {
res, err := proto.DOMResolveNode{
NodeID: node.NodeID,
BackendNodeID: node.BackendNodeID,
}.Call(p)
if err != nil {
return nil, err
}
el, err := p.ElementFromObject(res.Object)
if err != nil {
return nil, err
}
// make sure always return an element node
desc, err := el.Describe(0, false)
if err != nil {
return nil, err
}
if desc.NodeName == "#text" {
el, err = el.Parent()
if err != nil {
return nil, err
}
}
return el, nil
}
// ElementFromPoint creates an Element from the absolute point on the page.
// The point should include the window scroll offset.
func (p *Page) ElementFromPoint(x, y int) (*Element, error) {
node, err := proto.DOMGetNodeForLocation{X: x, Y: y}.Call(p)
if err != nil {
return nil, err
}
return p.ElementFromNode(&proto.DOMNode{
BackendNodeID: node.BackendNodeID,
})
}
// Release the remote object. Usually, you don't need to call it.
// When a page is closed or reloaded, all remote objects will be released automatically.
// It's useful if the page never closes or reloads.
func (p *Page) Release(obj *proto.RuntimeRemoteObject) error {
err := proto.RuntimeReleaseObject{ObjectID: obj.ObjectID}.Call(p)
return err
}
// Call implements the proto.Client
func (p *Page) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error) {
return p.browser.Call(ctx, sessionID, methodName, params)
}
// Event of the page
func (p *Page) Event() <-chan *Message {
dst := make(chan *Message)
s := p.event.Subscribe(p.ctx)
go func() {
defer close(dst)
for {
select {
case <-p.ctx.Done():
return
case msg, ok := <-s:
if !ok {
return
}
select {
case <-p.ctx.Done():
return
case dst <- msg.(*Message):
}
}
}
}()
return dst
}
func (p *Page) initEvents() {
p.event = goob.New(p.ctx)
event := p.browser.Context(p.ctx).Event()
go func() {
for msg := range event {
detached := proto.TargetDetachedFromTarget{}
destroyed := proto.TargetTargetDestroyed{}
if (msg.Load(&detached) && detached.SessionID == p.SessionID) ||
(msg.Load(destroyed) && destroyed.TargetID == p.TargetID) {
p.sessionCancel()
return
}
if msg.SessionID != p.SessionID {
continue
}
p.event.Publish(msg)
}
}()
}