/
page.go
1792 lines (1562 loc) · 66.5 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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package page provides the Chrome DevTools Protocol
// commands, types, and events for the Page domain.
//
// Actions and events related to the inspected page belong to the page
// domain.
//
// Generated by the cdproto-gen command.
package page
// Code generated by cdproto-gen. DO NOT EDIT.
import (
"context"
"encoding/base64"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/debugger"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/io"
"github.com/chromedp/cdproto/runtime"
)
// AddScriptToEvaluateOnNewDocumentParams evaluates given script in every
// frame upon creation (before loading frame's scripts).
type AddScriptToEvaluateOnNewDocumentParams struct {
Source string `json:"source"`
WorldName string `json:"worldName,omitempty"` // If specified, creates an isolated world with the given name and evaluates given script in it. This world name will be used as the ExecutionContextDescription::name when the corresponding event is emitted.
IncludeCommandLineAPI bool `json:"includeCommandLineAPI,omitempty"` // Specifies whether command line API should be available to the script, defaults to false.
RunImmediately bool `json:"runImmediately,omitempty"` // If true, runs the script immediately on existing execution contexts or worlds. Default: false.
}
// AddScriptToEvaluateOnNewDocument evaluates given script in every frame
// upon creation (before loading frame's scripts).
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-addScriptToEvaluateOnNewDocument
//
// parameters:
//
// source
func AddScriptToEvaluateOnNewDocument(source string) *AddScriptToEvaluateOnNewDocumentParams {
return &AddScriptToEvaluateOnNewDocumentParams{
Source: source,
}
}
// WithWorldName if specified, creates an isolated world with the given name
// and evaluates given script in it. This world name will be used as the
// ExecutionContextDescription::name when the corresponding event is emitted.
func (p AddScriptToEvaluateOnNewDocumentParams) WithWorldName(worldName string) *AddScriptToEvaluateOnNewDocumentParams {
p.WorldName = worldName
return &p
}
// WithIncludeCommandLineAPI specifies whether command line API should be
// available to the script, defaults to false.
func (p AddScriptToEvaluateOnNewDocumentParams) WithIncludeCommandLineAPI(includeCommandLineAPI bool) *AddScriptToEvaluateOnNewDocumentParams {
p.IncludeCommandLineAPI = includeCommandLineAPI
return &p
}
// WithRunImmediately if true, runs the script immediately on existing
// execution contexts or worlds. Default: false.
func (p AddScriptToEvaluateOnNewDocumentParams) WithRunImmediately(runImmediately bool) *AddScriptToEvaluateOnNewDocumentParams {
p.RunImmediately = runImmediately
return &p
}
// AddScriptToEvaluateOnNewDocumentReturns return values.
type AddScriptToEvaluateOnNewDocumentReturns struct {
Identifier ScriptIdentifier `json:"identifier,omitempty"` // Identifier of the added script.
}
// Do executes Page.addScriptToEvaluateOnNewDocument against the provided context.
//
// returns:
//
// identifier - Identifier of the added script.
func (p *AddScriptToEvaluateOnNewDocumentParams) Do(ctx context.Context) (identifier ScriptIdentifier, err error) {
// execute
var res AddScriptToEvaluateOnNewDocumentReturns
err = cdp.Execute(ctx, CommandAddScriptToEvaluateOnNewDocument, p, &res)
if err != nil {
return "", err
}
return res.Identifier, nil
}
// BringToFrontParams brings page to front (activates tab).
type BringToFrontParams struct{}
// BringToFront brings page to front (activates tab).
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-bringToFront
func BringToFront() *BringToFrontParams {
return &BringToFrontParams{}
}
// Do executes Page.bringToFront against the provided context.
func (p *BringToFrontParams) Do(ctx context.Context) (err error) {
return cdp.Execute(ctx, CommandBringToFront, nil, nil)
}
// CaptureScreenshotParams capture page screenshot.
type CaptureScreenshotParams struct {
Format CaptureScreenshotFormat `json:"format,omitempty"` // Image compression format (defaults to png).
Quality int64 `json:"quality,omitempty"` // Compression quality from range [0..100] (jpeg only).
Clip *Viewport `json:"clip,omitempty"` // Capture the screenshot of a given region only.
FromSurface bool `json:"fromSurface,omitempty"` // Capture the screenshot from the surface, rather than the view. Defaults to true.
CaptureBeyondViewport bool `json:"captureBeyondViewport,omitempty"` // Capture the screenshot beyond the viewport. Defaults to false.
OptimizeForSpeed bool `json:"optimizeForSpeed,omitempty"` // Optimize image encoding for speed, not for resulting size (defaults to false)
}
// CaptureScreenshot capture page screenshot.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-captureScreenshot
//
// parameters:
func CaptureScreenshot() *CaptureScreenshotParams {
return &CaptureScreenshotParams{}
}
// WithFormat image compression format (defaults to png).
func (p CaptureScreenshotParams) WithFormat(format CaptureScreenshotFormat) *CaptureScreenshotParams {
p.Format = format
return &p
}
// WithQuality compression quality from range [0..100] (jpeg only).
func (p CaptureScreenshotParams) WithQuality(quality int64) *CaptureScreenshotParams {
p.Quality = quality
return &p
}
// WithClip capture the screenshot of a given region only.
func (p CaptureScreenshotParams) WithClip(clip *Viewport) *CaptureScreenshotParams {
p.Clip = clip
return &p
}
// WithFromSurface capture the screenshot from the surface, rather than the
// view. Defaults to true.
func (p CaptureScreenshotParams) WithFromSurface(fromSurface bool) *CaptureScreenshotParams {
p.FromSurface = fromSurface
return &p
}
// WithCaptureBeyondViewport capture the screenshot beyond the viewport.
// Defaults to false.
func (p CaptureScreenshotParams) WithCaptureBeyondViewport(captureBeyondViewport bool) *CaptureScreenshotParams {
p.CaptureBeyondViewport = captureBeyondViewport
return &p
}
// WithOptimizeForSpeed optimize image encoding for speed, not for resulting
// size (defaults to false).
func (p CaptureScreenshotParams) WithOptimizeForSpeed(optimizeForSpeed bool) *CaptureScreenshotParams {
p.OptimizeForSpeed = optimizeForSpeed
return &p
}
// CaptureScreenshotReturns return values.
type CaptureScreenshotReturns struct {
Data string `json:"data,omitempty"` // Base64-encoded image data.
}
// Do executes Page.captureScreenshot against the provided context.
//
// returns:
//
// data - Base64-encoded image data.
func (p *CaptureScreenshotParams) Do(ctx context.Context) (data []byte, err error) {
// execute
var res CaptureScreenshotReturns
err = cdp.Execute(ctx, CommandCaptureScreenshot, p, &res)
if err != nil {
return nil, err
}
// decode
var dec []byte
dec, err = base64.StdEncoding.DecodeString(res.Data)
if err != nil {
return nil, err
}
return dec, nil
}
// CaptureSnapshotParams returns a snapshot of the page as a string. For
// MHTML format, the serialization includes iframes, shadow DOM, external
// resources, and element-inline styles.
type CaptureSnapshotParams struct {
Format CaptureSnapshotFormat `json:"format,omitempty"` // Format (defaults to mhtml).
}
// CaptureSnapshot returns a snapshot of the page as a string. For MHTML
// format, the serialization includes iframes, shadow DOM, external resources,
// and element-inline styles.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-captureSnapshot
//
// parameters:
func CaptureSnapshot() *CaptureSnapshotParams {
return &CaptureSnapshotParams{}
}
// WithFormat format (defaults to mhtml).
func (p CaptureSnapshotParams) WithFormat(format CaptureSnapshotFormat) *CaptureSnapshotParams {
p.Format = format
return &p
}
// CaptureSnapshotReturns return values.
type CaptureSnapshotReturns struct {
Data string `json:"data,omitempty"` // Serialized page data.
}
// Do executes Page.captureSnapshot against the provided context.
//
// returns:
//
// data - Serialized page data.
func (p *CaptureSnapshotParams) Do(ctx context.Context) (data string, err error) {
// execute
var res CaptureSnapshotReturns
err = cdp.Execute(ctx, CommandCaptureSnapshot, p, &res)
if err != nil {
return "", err
}
return res.Data, nil
}
// CreateIsolatedWorldParams creates an isolated world for the given frame.
type CreateIsolatedWorldParams struct {
FrameID cdp.FrameID `json:"frameId"` // Id of the frame in which the isolated world should be created.
WorldName string `json:"worldName,omitempty"` // An optional name which is reported in the Execution Context.
GrantUniveralAccess bool `json:"grantUniveralAccess,omitempty"` // Whether or not universal access should be granted to the isolated world. This is a powerful option, use with caution.
}
// CreateIsolatedWorld creates an isolated world for the given frame.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-createIsolatedWorld
//
// parameters:
//
// frameID - Id of the frame in which the isolated world should be created.
func CreateIsolatedWorld(frameID cdp.FrameID) *CreateIsolatedWorldParams {
return &CreateIsolatedWorldParams{
FrameID: frameID,
}
}
// WithWorldName an optional name which is reported in the Execution Context.
func (p CreateIsolatedWorldParams) WithWorldName(worldName string) *CreateIsolatedWorldParams {
p.WorldName = worldName
return &p
}
// WithGrantUniveralAccess whether or not universal access should be granted
// to the isolated world. This is a powerful option, use with caution.
func (p CreateIsolatedWorldParams) WithGrantUniveralAccess(grantUniveralAccess bool) *CreateIsolatedWorldParams {
p.GrantUniveralAccess = grantUniveralAccess
return &p
}
// CreateIsolatedWorldReturns return values.
type CreateIsolatedWorldReturns struct {
ExecutionContextID runtime.ExecutionContextID `json:"executionContextId,omitempty"` // Execution context of the isolated world.
}
// Do executes Page.createIsolatedWorld against the provided context.
//
// returns:
//
// executionContextID - Execution context of the isolated world.
func (p *CreateIsolatedWorldParams) Do(ctx context.Context) (executionContextID runtime.ExecutionContextID, err error) {
// execute
var res CreateIsolatedWorldReturns
err = cdp.Execute(ctx, CommandCreateIsolatedWorld, p, &res)
if err != nil {
return 0, err
}
return res.ExecutionContextID, nil
}
// DisableParams disables page domain notifications.
type DisableParams struct{}
// Disable disables page domain notifications.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-disable
func Disable() *DisableParams {
return &DisableParams{}
}
// Do executes Page.disable against the provided context.
func (p *DisableParams) Do(ctx context.Context) (err error) {
return cdp.Execute(ctx, CommandDisable, nil, nil)
}
// EnableParams enables page domain notifications.
type EnableParams struct{}
// Enable enables page domain notifications.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-enable
func Enable() *EnableParams {
return &EnableParams{}
}
// Do executes Page.enable against the provided context.
func (p *EnableParams) Do(ctx context.Context) (err error) {
return cdp.Execute(ctx, CommandEnable, nil, nil)
}
// GetAppManifestParams gets the processed manifest for this current
// document. This API always waits for the manifest to be loaded. If manifestId
// is provided, and it does not match the manifest of the current document, this
// API errors out. If there is not a loaded page, this API errors out
// immediately.
type GetAppManifestParams struct {
ManifestID string `json:"manifestId,omitempty"`
}
// GetAppManifest gets the processed manifest for this current document. This
// API always waits for the manifest to be loaded. If manifestId is provided,
// and it does not match the manifest of the current document, this API errors
// out. If there is not a loaded page, this API errors out immediately.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getAppManifest
//
// parameters:
func GetAppManifest() *GetAppManifestParams {
return &GetAppManifestParams{}
}
// WithManifestID [no description].
func (p GetAppManifestParams) WithManifestID(manifestID string) *GetAppManifestParams {
p.ManifestID = manifestID
return &p
}
// GetAppManifestReturns return values.
type GetAppManifestReturns struct {
URL string `json:"url,omitempty"` // Manifest location.
Errors []*AppManifestError `json:"errors,omitempty"`
Data string `json:"data,omitempty"` // Manifest content.
Manifest *WebAppManifest `json:"manifest,omitempty"`
}
// Do executes Page.getAppManifest against the provided context.
//
// returns:
//
// url - Manifest location.
// errors
// data - Manifest content.
// manifest
func (p *GetAppManifestParams) Do(ctx context.Context) (url string, errors []*AppManifestError, data string, manifest *WebAppManifest, err error) {
// execute
var res GetAppManifestReturns
err = cdp.Execute(ctx, CommandGetAppManifest, p, &res)
if err != nil {
return "", nil, "", nil, err
}
return res.URL, res.Errors, res.Data, res.Manifest, nil
}
// GetInstallabilityErrorsParams [no description].
type GetInstallabilityErrorsParams struct{}
// GetInstallabilityErrors [no description].
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getInstallabilityErrors
func GetInstallabilityErrors() *GetInstallabilityErrorsParams {
return &GetInstallabilityErrorsParams{}
}
// GetInstallabilityErrorsReturns return values.
type GetInstallabilityErrorsReturns struct {
InstallabilityErrors []*InstallabilityError `json:"installabilityErrors,omitempty"`
}
// Do executes Page.getInstallabilityErrors against the provided context.
//
// returns:
//
// installabilityErrors
func (p *GetInstallabilityErrorsParams) Do(ctx context.Context) (installabilityErrors []*InstallabilityError, err error) {
// execute
var res GetInstallabilityErrorsReturns
err = cdp.Execute(ctx, CommandGetInstallabilityErrors, nil, &res)
if err != nil {
return nil, err
}
return res.InstallabilityErrors, nil
}
// GetAppIDParams returns the unique (PWA) app id. Only returns values if the
// feature flag 'WebAppEnableManifestId' is enabled.
type GetAppIDParams struct{}
// GetAppID returns the unique (PWA) app id. Only returns values if the
// feature flag 'WebAppEnableManifestId' is enabled.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getAppId
func GetAppID() *GetAppIDParams {
return &GetAppIDParams{}
}
// GetAppIDReturns return values.
type GetAppIDReturns struct {
AppID string `json:"appId,omitempty"` // App id, either from manifest's id attribute or computed from start_url
RecommendedID string `json:"recommendedId,omitempty"` // Recommendation for manifest's id attribute to match current id computed from start_url
}
// Do executes Page.getAppId against the provided context.
//
// returns:
//
// appID - App id, either from manifest's id attribute or computed from start_url
// recommendedID - Recommendation for manifest's id attribute to match current id computed from start_url
func (p *GetAppIDParams) Do(ctx context.Context) (appID string, recommendedID string, err error) {
// execute
var res GetAppIDReturns
err = cdp.Execute(ctx, CommandGetAppID, nil, &res)
if err != nil {
return "", "", err
}
return res.AppID, res.RecommendedID, nil
}
// GetAdScriptIDParams [no description].
type GetAdScriptIDParams struct {
FrameID cdp.FrameID `json:"frameId"`
}
// GetAdScriptID [no description].
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getAdScriptId
//
// parameters:
//
// frameID
func GetAdScriptID(frameID cdp.FrameID) *GetAdScriptIDParams {
return &GetAdScriptIDParams{
FrameID: frameID,
}
}
// GetAdScriptIDReturns return values.
type GetAdScriptIDReturns struct {
AdScriptID *AdScriptID `json:"adScriptId,omitempty"` // Identifies the bottom-most script which caused the frame to be labelled as an ad. Only sent if frame is labelled as an ad and id is available.
}
// Do executes Page.getAdScriptId against the provided context.
//
// returns:
//
// adScriptID - Identifies the bottom-most script which caused the frame to be labelled as an ad. Only sent if frame is labelled as an ad and id is available.
func (p *GetAdScriptIDParams) Do(ctx context.Context) (adScriptID *AdScriptID, err error) {
// execute
var res GetAdScriptIDReturns
err = cdp.Execute(ctx, CommandGetAdScriptID, p, &res)
if err != nil {
return nil, err
}
return res.AdScriptID, nil
}
// GetFrameTreeParams returns present frame tree structure.
type GetFrameTreeParams struct{}
// GetFrameTree returns present frame tree structure.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getFrameTree
func GetFrameTree() *GetFrameTreeParams {
return &GetFrameTreeParams{}
}
// GetFrameTreeReturns return values.
type GetFrameTreeReturns struct {
FrameTree *FrameTree `json:"frameTree,omitempty"` // Present frame tree structure.
}
// Do executes Page.getFrameTree against the provided context.
//
// returns:
//
// frameTree - Present frame tree structure.
func (p *GetFrameTreeParams) Do(ctx context.Context) (frameTree *FrameTree, err error) {
// execute
var res GetFrameTreeReturns
err = cdp.Execute(ctx, CommandGetFrameTree, nil, &res)
if err != nil {
return nil, err
}
return res.FrameTree, nil
}
// GetLayoutMetricsParams returns metrics relating to the layouting of the
// page, such as viewport bounds/scale.
type GetLayoutMetricsParams struct{}
// GetLayoutMetrics returns metrics relating to the layouting of the page,
// such as viewport bounds/scale.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getLayoutMetrics
func GetLayoutMetrics() *GetLayoutMetricsParams {
return &GetLayoutMetricsParams{}
}
// GetLayoutMetricsReturns return values.
type GetLayoutMetricsReturns struct {
LayoutViewport *LayoutViewport `json:"layoutViewport"` // Deprecated metrics relating to the layout viewport. Is in device pixels. Use cssLayoutViewport instead.
VisualViewport *VisualViewport `json:"visualViewport"` // Deprecated metrics relating to the visual viewport. Is in device pixels. Use cssVisualViewport instead.
ContentSize *dom.Rect `json:"contentSize"` // Deprecated size of scrollable area. Is in DP. Use cssContentSize instead.
CSSLayoutViewport *LayoutViewport `json:"cssLayoutViewport"` // Metrics relating to the layout viewport in CSS pixels.
CSSVisualViewport *VisualViewport `json:"cssVisualViewport"` // Metrics relating to the visual viewport in CSS pixels.
CSSContentSize *dom.Rect `json:"cssContentSize"` // Size of scrollable area in CSS pixels.
}
// Do executes Page.getLayoutMetrics against the provided context.
//
// returns:
//
// layoutViewport - Deprecated metrics relating to the layout viewport. Is in device pixels. Use cssLayoutViewport instead.
// visualViewport - Deprecated metrics relating to the visual viewport. Is in device pixels. Use cssVisualViewport instead.
// contentSize - Deprecated size of scrollable area. Is in DP. Use cssContentSize instead.
// cssLayoutViewport - Metrics relating to the layout viewport in CSS pixels.
// cssVisualViewport - Metrics relating to the visual viewport in CSS pixels.
// cssContentSize - Size of scrollable area in CSS pixels.
func (p *GetLayoutMetricsParams) Do(ctx context.Context) (layoutViewport *LayoutViewport, visualViewport *VisualViewport, contentSize *dom.Rect, cssLayoutViewport *LayoutViewport, cssVisualViewport *VisualViewport, cssContentSize *dom.Rect, err error) {
// execute
var res GetLayoutMetricsReturns
err = cdp.Execute(ctx, CommandGetLayoutMetrics, nil, &res)
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
return res.LayoutViewport, res.VisualViewport, res.ContentSize, res.CSSLayoutViewport, res.CSSVisualViewport, res.CSSContentSize, nil
}
// GetNavigationHistoryParams returns navigation history for the current
// page.
type GetNavigationHistoryParams struct{}
// GetNavigationHistory returns navigation history for the current page.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getNavigationHistory
func GetNavigationHistory() *GetNavigationHistoryParams {
return &GetNavigationHistoryParams{}
}
// GetNavigationHistoryReturns return values.
type GetNavigationHistoryReturns struct {
CurrentIndex int64 `json:"currentIndex,omitempty"` // Index of the current navigation history entry.
Entries []*NavigationEntry `json:"entries,omitempty"` // Array of navigation history entries.
}
// Do executes Page.getNavigationHistory against the provided context.
//
// returns:
//
// currentIndex - Index of the current navigation history entry.
// entries - Array of navigation history entries.
func (p *GetNavigationHistoryParams) Do(ctx context.Context) (currentIndex int64, entries []*NavigationEntry, err error) {
// execute
var res GetNavigationHistoryReturns
err = cdp.Execute(ctx, CommandGetNavigationHistory, nil, &res)
if err != nil {
return 0, nil, err
}
return res.CurrentIndex, res.Entries, nil
}
// ResetNavigationHistoryParams resets navigation history for the current
// page.
type ResetNavigationHistoryParams struct{}
// ResetNavigationHistory resets navigation history for the current page.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-resetNavigationHistory
func ResetNavigationHistory() *ResetNavigationHistoryParams {
return &ResetNavigationHistoryParams{}
}
// Do executes Page.resetNavigationHistory against the provided context.
func (p *ResetNavigationHistoryParams) Do(ctx context.Context) (err error) {
return cdp.Execute(ctx, CommandResetNavigationHistory, nil, nil)
}
// GetResourceContentParams returns content of the given resource.
type GetResourceContentParams struct {
FrameID cdp.FrameID `json:"frameId"` // Frame id to get resource for.
URL string `json:"url"` // URL of the resource to get content for.
}
// GetResourceContent returns content of the given resource.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getResourceContent
//
// parameters:
//
// frameID - Frame id to get resource for.
// url - URL of the resource to get content for.
func GetResourceContent(frameID cdp.FrameID, url string) *GetResourceContentParams {
return &GetResourceContentParams{
FrameID: frameID,
URL: url,
}
}
// GetResourceContentReturns return values.
type GetResourceContentReturns struct {
Content string `json:"content,omitempty"` // Resource content.
Base64encoded bool `json:"base64Encoded,omitempty"` // True, if content was served as base64.
}
// Do executes Page.getResourceContent against the provided context.
//
// returns:
//
// content - Resource content.
func (p *GetResourceContentParams) Do(ctx context.Context) (content []byte, err error) {
// execute
var res GetResourceContentReturns
err = cdp.Execute(ctx, CommandGetResourceContent, p, &res)
if err != nil {
return nil, err
}
// decode
var dec []byte
if res.Base64encoded {
dec, err = base64.StdEncoding.DecodeString(res.Content)
if err != nil {
return nil, err
}
} else {
dec = []byte(res.Content)
}
return dec, nil
}
// GetResourceTreeParams returns present frame / resource tree structure.
type GetResourceTreeParams struct{}
// GetResourceTree returns present frame / resource tree structure.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-getResourceTree
func GetResourceTree() *GetResourceTreeParams {
return &GetResourceTreeParams{}
}
// GetResourceTreeReturns return values.
type GetResourceTreeReturns struct {
FrameTree *FrameResourceTree `json:"frameTree,omitempty"` // Present frame / resource tree structure.
}
// Do executes Page.getResourceTree against the provided context.
//
// returns:
//
// frameTree - Present frame / resource tree structure.
func (p *GetResourceTreeParams) Do(ctx context.Context) (frameTree *FrameResourceTree, err error) {
// execute
var res GetResourceTreeReturns
err = cdp.Execute(ctx, CommandGetResourceTree, nil, &res)
if err != nil {
return nil, err
}
return res.FrameTree, nil
}
// HandleJavaScriptDialogParams accepts or dismisses a JavaScript initiated
// dialog (alert, confirm, prompt, or onbeforeunload).
type HandleJavaScriptDialogParams struct {
Accept bool `json:"accept"` // Whether to accept or dismiss the dialog.
PromptText string `json:"promptText,omitempty"` // The text to enter into the dialog prompt before accepting. Used only if this is a prompt dialog.
}
// HandleJavaScriptDialog accepts or dismisses a JavaScript initiated dialog
// (alert, confirm, prompt, or onbeforeunload).
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-handleJavaScriptDialog
//
// parameters:
//
// accept - Whether to accept or dismiss the dialog.
func HandleJavaScriptDialog(accept bool) *HandleJavaScriptDialogParams {
return &HandleJavaScriptDialogParams{
Accept: accept,
}
}
// WithPromptText the text to enter into the dialog prompt before accepting.
// Used only if this is a prompt dialog.
func (p HandleJavaScriptDialogParams) WithPromptText(promptText string) *HandleJavaScriptDialogParams {
p.PromptText = promptText
return &p
}
// Do executes Page.handleJavaScriptDialog against the provided context.
func (p *HandleJavaScriptDialogParams) Do(ctx context.Context) (err error) {
return cdp.Execute(ctx, CommandHandleJavaScriptDialog, p, nil)
}
// NavigateParams navigates current page to the given URL.
type NavigateParams struct {
URL string `json:"url"` // URL to navigate the page to.
Referrer string `json:"referrer,omitempty"` // Referrer URL.
TransitionType TransitionType `json:"transitionType,omitempty"` // Intended transition type.
FrameID cdp.FrameID `json:"frameId,omitempty"` // Frame id to navigate, if not specified navigates the top frame.
ReferrerPolicy ReferrerPolicy `json:"referrerPolicy,omitempty"` // Referrer-policy used for the navigation.
}
// Navigate navigates current page to the given URL.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-navigate
//
// parameters:
//
// url - URL to navigate the page to.
func Navigate(url string) *NavigateParams {
return &NavigateParams{
URL: url,
}
}
// WithReferrer referrer URL.
func (p NavigateParams) WithReferrer(referrer string) *NavigateParams {
p.Referrer = referrer
return &p
}
// WithTransitionType intended transition type.
func (p NavigateParams) WithTransitionType(transitionType TransitionType) *NavigateParams {
p.TransitionType = transitionType
return &p
}
// WithFrameID frame id to navigate, if not specified navigates the top
// frame.
func (p NavigateParams) WithFrameID(frameID cdp.FrameID) *NavigateParams {
p.FrameID = frameID
return &p
}
// WithReferrerPolicy referrer-policy used for the navigation.
func (p NavigateParams) WithReferrerPolicy(referrerPolicy ReferrerPolicy) *NavigateParams {
p.ReferrerPolicy = referrerPolicy
return &p
}
// NavigateReturns return values.
type NavigateReturns struct {
FrameID cdp.FrameID `json:"frameId,omitempty"` // Frame id that has navigated (or failed to navigate)
LoaderID cdp.LoaderID `json:"loaderId,omitempty"` // Loader identifier. This is omitted in case of same-document navigation, as the previously committed loaderId would not change.
ErrorText string `json:"errorText,omitempty"` // User friendly error message, present if and only if navigation has failed.
}
// Do executes Page.navigate against the provided context.
//
// returns:
//
// frameID - Frame id that has navigated (or failed to navigate)
// loaderID - Loader identifier. This is omitted in case of same-document navigation, as the previously committed loaderId would not change.
// errorText - User friendly error message, present if and only if navigation has failed.
func (p *NavigateParams) Do(ctx context.Context) (frameID cdp.FrameID, loaderID cdp.LoaderID, errorText string, err error) {
// execute
var res NavigateReturns
err = cdp.Execute(ctx, CommandNavigate, p, &res)
if err != nil {
return "", "", "", err
}
return res.FrameID, res.LoaderID, res.ErrorText, nil
}
// NavigateToHistoryEntryParams navigates current page to the given history
// entry.
type NavigateToHistoryEntryParams struct {
EntryID int64 `json:"entryId"` // Unique id of the entry to navigate to.
}
// NavigateToHistoryEntry navigates current page to the given history entry.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-navigateToHistoryEntry
//
// parameters:
//
// entryID - Unique id of the entry to navigate to.
func NavigateToHistoryEntry(entryID int64) *NavigateToHistoryEntryParams {
return &NavigateToHistoryEntryParams{
EntryID: entryID,
}
}
// Do executes Page.navigateToHistoryEntry against the provided context.
func (p *NavigateToHistoryEntryParams) Do(ctx context.Context) (err error) {
return cdp.Execute(ctx, CommandNavigateToHistoryEntry, p, nil)
}
// PrintToPDFParams print page as PDF.
type PrintToPDFParams struct {
Landscape bool `json:"landscape,omitempty"` // Paper orientation. Defaults to false.
DisplayHeaderFooter bool `json:"displayHeaderFooter,omitempty"` // Display header and footer. Defaults to false.
PrintBackground bool `json:"printBackground,omitempty"` // Print background graphics. Defaults to false.
Scale float64 `json:"scale,omitempty"` // Scale of the webpage rendering. Defaults to 1.
PaperWidth float64 `json:"paperWidth,omitempty"` // Paper width in inches. Defaults to 8.5 inches.
PaperHeight float64 `json:"paperHeight,omitempty"` // Paper height in inches. Defaults to 11 inches.
MarginTop float64 `json:"marginTop"` // Top margin in inches. Defaults to 1cm (~0.4 inches).
MarginBottom float64 `json:"marginBottom"` // Bottom margin in inches. Defaults to 1cm (~0.4 inches).
MarginLeft float64 `json:"marginLeft"` // Left margin in inches. Defaults to 1cm (~0.4 inches).
MarginRight float64 `json:"marginRight"` // Right margin in inches. Defaults to 1cm (~0.4 inches).
PageRanges string `json:"pageRanges,omitempty"` // Paper ranges to print, one based, e.g., '1-5, 8, 11-13'. Pages are printed in the document order, not in the order specified, and no more than once. Defaults to empty string, which implies the entire document is printed. The page numbers are quietly capped to actual page count of the document, and ranges beyond the end of the document are ignored. If this results in no pages to print, an error is reported. It is an error to specify a range with start greater than end.
HeaderTemplate string `json:"headerTemplate,omitempty"` // HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them: - date: formatted print date - title: document title - url: document location - pageNumber: current page number - totalPages: total pages in the document For example, <span class=title></span> would generate span containing the title.
FooterTemplate string `json:"footerTemplate,omitempty"` // HTML template for the print footer. Should use the same format as the headerTemplate.
PreferCSSPageSize bool `json:"preferCSSPageSize,omitempty"` // Whether or not to prefer page size as defined by css. Defaults to false, in which case the content will be scaled to fit the paper size.
TransferMode PrintToPDFTransferMode `json:"transferMode,omitempty"` // return as stream
GenerateTaggedPDF bool `json:"generateTaggedPDF,omitempty"` // Whether or not to generate tagged (accessible) PDF. Defaults to embedder choice.
GenerateDocumentOutline bool `json:"generateDocumentOutline,omitempty"` // Whether or not to embed the document outline into the PDF.
}
// PrintToPDF print page as PDF.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-printToPDF
//
// parameters:
func PrintToPDF() *PrintToPDFParams {
return &PrintToPDFParams{}
}
// WithLandscape paper orientation. Defaults to false.
func (p PrintToPDFParams) WithLandscape(landscape bool) *PrintToPDFParams {
p.Landscape = landscape
return &p
}
// WithDisplayHeaderFooter display header and footer. Defaults to false.
func (p PrintToPDFParams) WithDisplayHeaderFooter(displayHeaderFooter bool) *PrintToPDFParams {
p.DisplayHeaderFooter = displayHeaderFooter
return &p
}
// WithPrintBackground print background graphics. Defaults to false.
func (p PrintToPDFParams) WithPrintBackground(printBackground bool) *PrintToPDFParams {
p.PrintBackground = printBackground
return &p
}
// WithScale scale of the webpage rendering. Defaults to 1.
func (p PrintToPDFParams) WithScale(scale float64) *PrintToPDFParams {
p.Scale = scale
return &p
}
// WithPaperWidth paper width in inches. Defaults to 8.5 inches.
func (p PrintToPDFParams) WithPaperWidth(paperWidth float64) *PrintToPDFParams {
p.PaperWidth = paperWidth
return &p
}
// WithPaperHeight paper height in inches. Defaults to 11 inches.
func (p PrintToPDFParams) WithPaperHeight(paperHeight float64) *PrintToPDFParams {
p.PaperHeight = paperHeight
return &p
}
// WithMarginTop top margin in inches. Defaults to 1cm (~0.4 inches).
func (p PrintToPDFParams) WithMarginTop(marginTop float64) *PrintToPDFParams {
p.MarginTop = marginTop
return &p
}
// WithMarginBottom bottom margin in inches. Defaults to 1cm (~0.4 inches).
func (p PrintToPDFParams) WithMarginBottom(marginBottom float64) *PrintToPDFParams {
p.MarginBottom = marginBottom
return &p
}
// WithMarginLeft left margin in inches. Defaults to 1cm (~0.4 inches).
func (p PrintToPDFParams) WithMarginLeft(marginLeft float64) *PrintToPDFParams {
p.MarginLeft = marginLeft
return &p
}
// WithMarginRight right margin in inches. Defaults to 1cm (~0.4 inches).
func (p PrintToPDFParams) WithMarginRight(marginRight float64) *PrintToPDFParams {
p.MarginRight = marginRight
return &p
}
// WithPageRanges paper ranges to print, one based, e.g., '1-5, 8, 11-13'.
// Pages are printed in the document order, not in the order specified, and no
// more than once. Defaults to empty string, which implies the entire document
// is printed. The page numbers are quietly capped to actual page count of the
// document, and ranges beyond the end of the document are ignored. If this
// results in no pages to print, an error is reported. It is an error to specify
// a range with start greater than end.
func (p PrintToPDFParams) WithPageRanges(pageRanges string) *PrintToPDFParams {
p.PageRanges = pageRanges
return &p
}
// WithHeaderTemplate HTML template for the print header. Should be valid
// HTML markup with following classes used to inject printing values into them:
// - date: formatted print date - title: document title - url: document location
// - pageNumber: current page number - totalPages: total pages in the document
// For example, <span class=title></span> would generate span containing the
// title.
func (p PrintToPDFParams) WithHeaderTemplate(headerTemplate string) *PrintToPDFParams {
p.HeaderTemplate = headerTemplate
return &p
}
// WithFooterTemplate HTML template for the print footer. Should use the same
// format as the headerTemplate.
func (p PrintToPDFParams) WithFooterTemplate(footerTemplate string) *PrintToPDFParams {
p.FooterTemplate = footerTemplate
return &p
}
// WithPreferCSSPageSize whether or not to prefer page size as defined by
// css. Defaults to false, in which case the content will be scaled to fit the
// paper size.
func (p PrintToPDFParams) WithPreferCSSPageSize(preferCSSPageSize bool) *PrintToPDFParams {
p.PreferCSSPageSize = preferCSSPageSize
return &p
}
// WithTransferMode return as stream.
func (p PrintToPDFParams) WithTransferMode(transferMode PrintToPDFTransferMode) *PrintToPDFParams {
p.TransferMode = transferMode
return &p
}
// WithGenerateTaggedPDF whether or not to generate tagged (accessible) PDF.
// Defaults to embedder choice.
func (p PrintToPDFParams) WithGenerateTaggedPDF(generateTaggedPDF bool) *PrintToPDFParams {
p.GenerateTaggedPDF = generateTaggedPDF
return &p
}
// WithGenerateDocumentOutline whether or not to embed the document outline
// into the PDF.
func (p PrintToPDFParams) WithGenerateDocumentOutline(generateDocumentOutline bool) *PrintToPDFParams {
p.GenerateDocumentOutline = generateDocumentOutline
return &p
}
// PrintToPDFReturns return values.
type PrintToPDFReturns struct {
Data string `json:"data,omitempty"` // Base64-encoded pdf data. Empty if |returnAsStream| is specified.
Stream io.StreamHandle `json:"stream,omitempty"` // A handle of the stream that holds resulting PDF data.
}
// Do executes Page.printToPDF against the provided context.
//
// returns:
//
// data - Base64-encoded pdf data. Empty if |returnAsStream| is specified.
// stream - A handle of the stream that holds resulting PDF data.
func (p *PrintToPDFParams) Do(ctx context.Context) (data []byte, stream io.StreamHandle, err error) {
// execute
var res PrintToPDFReturns
err = cdp.Execute(ctx, CommandPrintToPDF, p, &res)
if err != nil {
return nil, "", err
}
// decode
var dec []byte
dec, err = base64.StdEncoding.DecodeString(res.Data)
if err != nil {
return nil, "", err
}
return dec, res.Stream, nil
}
// ReloadParams reloads given page optionally ignoring the cache.
type ReloadParams struct {
IgnoreCache bool `json:"ignoreCache,omitempty"` // If true, browser cache is ignored (as if the user pressed Shift+refresh).
ScriptToEvaluateOnLoad string `json:"scriptToEvaluateOnLoad,omitempty"` // If set, the script will be injected into all frames of the inspected page after reload. Argument will be ignored if reloading dataURL origin.
}
// Reload reloads given page optionally ignoring the cache.
//
// See: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-reload
//