-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.v
264 lines (235 loc) · 6.37 KB
/
page.v
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
module cdv
import x.json2 as json
import net.urllib
@[heap]
pub struct Page {
pub mut:
next_id int = 1
target_id string
browser &Browser = unsafe { nil }
session_id string
var_id int = 1
deps []string
mut:
keyboard ?&Keyboard
mouse ?&Mouse
}
pub fn (mut page Page) str() string {
return 'Page{
next_id: ${page.next_id}
target_id: ${page.target_id}
browser: &Browser{...}
session_id: ${page.session_id}
deps: ${page.deps}
var_id: ${page.var_id}
}'
}
pub struct Viewport {
pub mut:
x ?f64
y ?f64
width ?f64
height ?f64
scale ?f64
}
pub fn (mut bwr Browser) new_page_opt(data MessageParams) !&Page {
mut target_id := ''
if bwr.has_init && data.params == none && bwr.browser_context_id == none {
target_id = bwr.target_id
bwr.has_init = false
} else {
mut params := data.params or {
map[string]json.Any{}
}
.clone()
params['url'] = 'about:blank'
if ctx_id := bwr.browser_context_id {
params['browserContextId'] = ctx_id
}
new_target := bwr.send('Target.createTarget', params: params)!.result
target_id = new_target['targetId']!.str()
}
attach := bwr.send('Target.attachToTarget',
params: {
'targetId': json.Any(target_id)
'flatten': true
}
)!.result
session_id := attach['sessionId']!.str()
mut page := &Page{
target_id: target_id
browser: bwr
session_id: session_id
}
page.enable(['Page', 'DOM', 'Network', 'Runtime'])
if bwr.use_pages {
bwr.add_page(mut page)
}
return page
}
pub fn (mut bwr Browser) new_page(data MessageParams) &Page {
return bwr.new_page_opt(data) or { bwr.noop(err) }
}
pub fn (mut page Page) send(method string, params MessageParams) !Result {
id := page.get_next_id(params.id)
if !isnil(params.cb) {
mut data := &DataMessagePage{params.cb, params.ref, page}
cb := fn (mut msg Message, mut data DataMessagePage) !bool {
msg.page = data.page
return data.cb(mut msg, data.ref)!
}
return page.browser.send(method, MessageParams{
...params
ref: data
cb: cb
id: id
session_id: page.session_id
})!
}
return page.browser.send(method, MessageParams{
...params
id: id
session_id: page.session_id
})!
}
fn (mut page Page) send_or_noop(method string, params MessageParams) Result {
return page.send(method, params) or { page.noop(err) }
}
fn (mut page Page) send_warn(method string, params MessageParams) Result {
res := page.send_or_noop(method, params)
if res.is_error {
msg := json.Any(res.error).prettify_json_str()
eprintln(msg)
}
return res
}
fn (mut page Page) struct_to_json_any[T](d T) json.Any {
return page.browser.struct_to_json_any(d)
}
pub fn (mut page Page) send_event(method string, msg MessageParams) !Result {
return page.send(method, MessageParams{ ...msg, typ: .event })!
}
fn (mut page Page) send_event_or_noop(method string, msg MessageParams) Result {
return page.send_event(method, msg) or { page.noop(err) }
}
@[params]
pub struct CloseTargetConfig {
pub:
force_close_browser bool
}
pub fn (mut page Page) close(opts CloseTargetConfig) {
if opts.force_close_browser || page.target_id != page.browser.target_id {
page.close_target()
return
}
page.send('Page.navigate',
params: {
'url': 'about:blank'
}
) or {}
}
fn (mut page Page) close_target() {
page.send('Target.closeTarget',
params: {
'targetId': page.target_id
}
) or {
eprintln('tab is closed')
return
}
}
fn (mut page Page) get_next_id(current_id int) int {
mut id := current_id
if id == -1 {
id = page.next_id++
}
return id
}
pub fn (mut page Page) enable(domain Strings, msg MessageParams) {
if domain.type_name() == 'string' {
domain_str := domain as string
if !page.deps.contains(domain_str) {
page.send_or_noop('${domain_str}.enable', msg)
page.deps << domain_str
}
return
}
for m in domain as []string {
page.enable(m)
}
}
pub fn (mut page Page) disable(domain Strings, msg MessageParams) {
if domain.type_name() == 'string' {
domain_str := domain as string
page.send_or_noop('${domain_str}.disable', msg)
idx := page.deps.index(domain_str)
if page.deps.contains(domain_str) && idx != -1 {
page.deps.delete(idx)
}
return
}
for m in domain as []string {
page.disable(m)
}
}
@[params]
pub struct PageNavigateParams {
pub:
url string @[json: 'url']
referrer ?string @[json: 'referrer']
transition_type ?string @[json: 'transitionType']
frame_id ?string @[json: 'frameId']
referrer_policy ?string @[json: 'referrerPolicy']
cb EventFunc = unsafe { nil } @[json: '-']
ref voidptr @[json: '-']
}
pub fn (mut page Page) navigate_opt(url string, opts PageNavigateParams) !Result {
params := struct_to_json_any(PageNavigateParams{ ...opts, url: url })!.as_map()
return page.send('Page.navigate', params: params)!
}
@[params]
pub struct PageReloadParams {
pub:
ignore_cache ?bool @[json: 'ignoreCache']
script_to_evaluate_on_load ?string @[json: 'scriptToEvaluateOnLoad']
loader_id ?string @[json: 'loaderId']
cb EventFunc = unsafe { nil } @[json: '-']
ref voidptr @[json: '-']
}
pub fn (mut page Page) reload(opts PageReloadParams) Result {
params := page.struct_to_json_any(opts).as_map()
return page.send_or_noop('Page.reload', params: params)
}
@[noreturn]
fn (mut page Page) noop(err IError) {
page.browser.noop(err)
}
pub fn (mut page Page) navigate(url string, opts PageNavigateParams) Result {
return page.navigate_opt(url, opts) or { page.noop(err) }
}
pub fn (mut page Page) from_file(pathfile string, opts PageNavigateParams) Result {
mut file := get_file_url(pathfile) or { page.noop(err) }
return page.navigate(file, opts)
}
pub fn (mut page Page) from_html(html_str string, opts PageNavigateParams) Result {
return page.navigate('data:text/html,${urllib.path_escape(html_str)}', opts)
}
pub fn (mut page Page) wait_until_opt(params MessageParams) ! {
mut method := params.method
if method == '' {
method = 'Page.loadEventFired'
}
page.send_event(method, params)!
page.off_all()
}
pub fn (mut page Page) wait_until(params MessageParams) {
page.wait_until_opt(params) or { page.noop(err) }
}
pub fn (mut page Page) wait_for(timeout i64, params MessageParams) {
mut method := params.method
if method == '' {
method = 'CDV.waitFor'
}
page.send_event_or_noop(method, MessageParams{ ...params, timeout: timeout })
page.off_all()
}