-
Notifications
You must be signed in to change notification settings - Fork 6
/
page.go
147 lines (123 loc) · 3.57 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
package control
import (
"errors"
"github.com/ecwid/control/protocol/common"
"github.com/ecwid/control/protocol/page"
)
type LifecycleEventType string
const (
LifecycleDOMContentLoaded LifecycleEventType = "DOMContentLoaded"
LifecycleIdleNetwork LifecycleEventType = "networkIdle"
LifecycleFirstContentfulPaint LifecycleEventType = "firstContentfulPaint"
LifecycleFirstMeaningfulPaint LifecycleEventType = "firstMeaningfulPaint"
LifecycleFirstMeaningfulPaintCandidate LifecycleEventType = "firstMeaningfulPaintCandidate"
LifecycleFirstPaint LifecycleEventType = "firstPaint"
LifecycleFirstTextPaint LifecycleEventType = "firstTextPaint"
LifecycleInit LifecycleEventType = "init"
LifecycleLoad LifecycleEventType = "load"
LifecycleNetworkAlmostIdle LifecycleEventType = "networkAlmostIdle"
)
var ErrNavigateNoLoader = errors.New("navigation to the same address")
type Queryable interface {
Query(string) Optional[*Node]
MustQuery(string) *Node
QueryAll(string) Optional[NodeList]
MustQueryAll(string) NodeList
OwnerFrame() *Frame
}
type Frame struct {
node *Node
session *Session
id common.FrameId
parent *Frame
}
func (f Frame) GetSession() *Session {
return f.session
}
func (f Frame) GetID() common.FrameId {
return f.id
}
func (f Frame) executionContextID() string {
if value, ok := f.session.frames.Load(f.id); ok {
return value.(string)
}
return ""
}
func (f Frame) Call(method string, send, recv any) error {
return f.session.Call(method, send, recv)
}
func (f *Frame) OwnerFrame() *Frame {
return f
}
func (f *Frame) Parent() *Frame {
return f.parent
}
func (f Frame) Log(msg string, args ...any) {
args = append(args, "frameId", f.id)
f.session.Log(msg, args...)
}
func (f Frame) Navigate(url string) error {
nav, err := page.Navigate(f, page.NavigateArgs{
Url: url,
FrameId: f.id,
})
if err != nil {
return err
}
if nav.ErrorText != "" {
return errors.New(nav.ErrorText)
}
if nav.LoaderId == "" {
return ErrNavigateNoLoader
}
return nil
}
func (f Frame) MustNavigate(url string) {
if err := f.Navigate(url); err != nil {
panic(err)
}
}
func (f Frame) Reload(ignoreCache bool, scriptToEvaluateOnLoad string) error {
return page.Reload(f, page.ReloadArgs{
IgnoreCache: ignoreCache,
ScriptToEvaluateOnLoad: scriptToEvaluateOnLoad,
})
}
func (f Frame) MustReload(ignoreCache bool, scriptToEvaluateOnLoad string) {
if err := f.Reload(ignoreCache, scriptToEvaluateOnLoad); err != nil {
panic(err)
}
}
func (f Frame) Evaluate(expression string, awaitPromise bool) Optional[any] {
return optional[any](f.evaluate(expression, awaitPromise))
}
func (f Frame) Document() Optional[*Node] {
opt := optional[*Node](f.evaluate("document", true))
if opt.err == nil && opt.value == nil {
opt.err = NoSuchSelectorError("document")
}
if opt.value != nil {
opt.value.requestedSelector = "document"
}
return opt
}
func (f Frame) MustQuery(cssSelector string) *Node {
return f.Query(cssSelector).MustGetValue()
}
func (f Frame) Query(cssSelector string) Optional[*Node] {
doc, err := f.Document().Unwrap()
if err != nil {
return Optional[*Node]{err: err}
}
return doc.Query(cssSelector)
}
func (f Frame) MustQueryAll(cssSelector string) NodeList {
return f.QueryAll(cssSelector).MustGetValue()
}
func (f Frame) QueryAll(cssSelector string) Optional[NodeList] {
doc, err := f.Document().Unwrap()
if err != nil {
return Optional[NodeList]{err: err}
}
return doc.QueryAll(cssSelector)
}