forked from Ecwid/control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
229 lines (206 loc) · 6.01 KB
/
frame.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
package control
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/ecwid/control/protocol/common"
"github.com/ecwid/control/protocol/page"
"github.com/ecwid/control/protocol/runtime"
"github.com/ecwid/control/transport"
)
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"
)
type Frame struct {
id common.FrameId // readonly
session *Session
}
func (f Frame) Session() *Session {
return f.session
}
func (f Frame) ID() common.FrameId {
return f.id
}
func (f Frame) Call(method string, send, recv interface{}) error {
return f.Session().Call(method, send, recv)
}
func (f Frame) GetLifecycleEvent(event LifecycleEventType) Future {
var initialized = false
return f.session.Observe("Page.lifecycleEvent", func(input transport.Event, resolve func(interface{}), reject func(error)) {
var v = page.LifecycleEvent{}
if err := json.Unmarshal(input.Params, &v); err != nil {
reject(err)
return
}
if v.FrameId == f.id && v.Name == "init" {
initialized = true
}
if initialized && v.FrameId == f.id && v.Name == string(event) {
resolve(v)
}
})
}
func (f Frame) Navigate(url string, waitEvent LifecycleEventType, timeout time.Duration) error {
future := f.GetLifecycleEvent(waitEvent)
defer future.Cancel()
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 ErrAlreadyNavigated
}
_, err = future.Get(timeout)
return err
}
// Reload refresh current page
func (f Frame) Reload(ignoreCache bool, scriptToEvaluateOnLoad string, eventType LifecycleEventType, timeout time.Duration) error {
future := f.GetLifecycleEvent(eventType)
defer future.Cancel()
err := page.Reload(f, page.ReloadArgs{
IgnoreCache: ignoreCache,
ScriptToEvaluateOnLoad: scriptToEvaluateOnLoad,
})
if err != nil {
return err
}
_, err = future.Get(timeout)
return err
}
func safeSelector(v string) string {
v = strings.TrimSpace(v)
v = strings.ReplaceAll(v, `"`, `\"`)
return v
}
func (f Frame) IsExist(selector string) bool {
selector = safeSelector(selector)
val, _ := f.evaluate(`document.querySelector("`+selector+`") != null`, true, false)
if val == nil {
return false
}
b, _ := primitiveRemoteObject(*val).Bool()
return b
}
func (f Frame) QuerySelector(selector string) (*Element, error) {
selector = safeSelector(selector)
var object, err = f.evaluate(`document.querySelector("`+selector+`")`, true, false)
if err != nil {
return nil, err
}
if object.ObjectId == "" {
return nil, NoSuchElementError{Selector: selector}
}
return f.constructElement(object)
}
func (f Frame) QuerySelectorAll(selector string) ([]*Element, error) {
selector = safeSelector(selector)
var array, err = f.evaluate(`document.querySelectorAll("`+selector+`")`, true, false)
if err != nil {
return nil, err
}
if array == nil || array.Description == "NodeList(0)" {
return nil, nil
}
list := make([]*Element, 0)
descriptor, err := f.getProperties(array.ObjectId, true, false)
if err != nil {
return nil, err
}
for _, d := range descriptor {
if !d.Enumerable {
continue
}
el, err1 := f.constructElement(d.Value)
if err1 != nil {
return nil, err1
}
list = append(list, el)
}
return list, nil
}
type RuntimeError runtime.ExceptionDetails
func (r RuntimeError) Error() string {
b, _ := json.Marshal(r)
return fmt.Sprintf("%s", b)
}
func (f Frame) Evaluate(expression string, await, returnByValue bool) (interface{}, error) {
val, err := f.evaluate(expression, await, returnByValue)
if err != nil {
return "", err
}
return val.Value, nil
}
func (f Frame) evaluate(expression string, await, returnByValue bool) (*runtime.RemoteObject, error) {
var uid, ok = f.session.executions.Load(f.id)
if !ok {
return nil, ErrExecutionContextDestroyed
}
val, err := runtime.Evaluate(f, runtime.EvaluateArgs{
Expression: expression,
IncludeCommandLineAPI: true,
UniqueContextId: uid.(string),
AwaitPromise: await,
ReturnByValue: returnByValue,
})
if err != nil {
return nil, err
}
if val.ExceptionDetails != nil {
return nil, RuntimeError(*val.ExceptionDetails)
}
return val.Result, nil
}
// GetNavigationEntry get current tab info
func (f Frame) GetNavigationEntry() (*page.NavigationEntry, error) {
val, err := page.GetNavigationHistory(f)
if err != nil {
return nil, err
}
if val.CurrentIndex == -1 {
return &page.NavigationEntry{Url: Blank}, nil
}
return val.Entries[val.CurrentIndex], nil
}
// NavigateHistory -1 = Back, +1 = Forward
func (f Frame) NavigateHistory(delta int) error {
val, err := page.GetNavigationHistory(f)
if err != nil {
return err
}
move := val.CurrentIndex + delta
if move >= 0 && move < len(val.Entries) {
return page.NavigateToHistoryEntry(f, page.NavigateToHistoryEntryArgs{
EntryId: val.Entries[move].Id,
})
}
return nil
}
func (f Frame) RequestDOMIdle(threshold, timeout time.Duration) error {
script := fmt.Sprintf(functionDOMIdle, threshold.Milliseconds(), timeout.Milliseconds())
_, err := f.Evaluate(script, true, false)
switch v := err.(type) {
case RuntimeError:
if val, _ := v.Exception.Value.(string); val == "timeout" {
return FutureTimeoutError{timeout: timeout}
}
}
return err
}