Class: Puppeteer::FrameManager
Defined Under Namespace
Classes: FrameNotFoundError, NavigationError
Constant Summary
collapse
- UTILITY_WORLD_NAME =
'__puppeteer_utility_world__'
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#execution_context_by_id(context_id, session) ⇒ Object
-
#frame(frame_id) ⇒ ?Frame
-
#frames ⇒ !Array<!Frame>
-
#handle_attached_to_target(target) ⇒ Object
-
#handle_detached_from_target(target) ⇒ Object
-
#handle_execution_context_created(context_payload, session) ⇒ Object
-
#handle_execution_context_destroyed(execution_context_id, session) ⇒ Object
-
#handle_execution_contexts_cleared(session) ⇒ Object
-
#handle_frame_attached(session, frame_id, parent_frame_id) ⇒ Object
-
#handle_frame_detached(frame_id, reason) ⇒ Object
-
#handle_frame_navigated(frame_payload) ⇒ Object
-
#handle_frame_navigated_within_document(frame_id, url) ⇒ Object
-
#handle_frame_started_loading(frame_id) ⇒ Object
-
#handle_frame_stopped_loading(frame_id) ⇒ Object
-
#handle_frame_tree(session, frame_tree) ⇒ Object
-
#handle_lifecycle_event(event) ⇒ Object
-
#initialize(client, page, ignore_https_errors, timeout_settings) ⇒ FrameManager
constructor
A new instance of FrameManager.
-
#main_frame ⇒ !Frame
-
#navigate_frame(frame, url, referer: nil, timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse
-
#page ⇒ !Puppeteer.Page
-
#wait_for_frame_navigation(frame, timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse
#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener
Methods included from IfPresent
#if_present
Methods included from DebugPrint
#debug_print, #debug_puts
Constructor Details
#initialize(client, page, ignore_https_errors, timeout_settings) ⇒ FrameManager
Returns a new instance of FrameManager.
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
|
# File 'lib/puppeteer/frame_manager.rb', line 15
def initialize(client, page, ignore_https_errors, timeout_settings)
@client = client
@page = page
@network_manager = Puppeteer::NetworkManager.new(client, ignore_https_errors, self)
@timeout_settings = timeout_settings
@frames = {}
@frame_naviigated_received = Set.new
@context_id_to_context = {}
@isolated_worlds = Set.new
@frames_pending_target_init = {}
@frames_pending_attachment = {}
setup_listeners(@client)
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
76
77
78
|
# File 'lib/puppeteer/frame_manager.rb', line 76
def client
@client
end
|
#network_manager ⇒ Object
Returns the value of attribute network_manager.
106
107
108
|
# File 'lib/puppeteer/frame_manager.rb', line 106
def network_manager
@network_manager
end
|
#timeout_settings ⇒ Object
Returns the value of attribute timeout_settings.
76
77
78
|
# File 'lib/puppeteer/frame_manager.rb', line 76
def timeout_settings
@timeout_settings
end
|
Instance Method Details
#execution_context_by_id(context_id, session) ⇒ Object
469
470
471
472
|
# File 'lib/puppeteer/frame_manager.rb', line 469
def execution_context_by_id(context_id, session)
key = "#{session.id}:#{context_id}"
@context_id_to_context[key] or raise "INTERNAL ERROR: missing context with id = #{context_id}"
end
|
#frame(frame_id) ⇒ ?Frame
260
261
262
|
# File 'lib/puppeteer/frame_manager.rb', line 260
def frame(frame_id)
@frames[frame_id]
end
|
#frames ⇒ !Array<!Frame>
254
255
256
|
# File 'lib/puppeteer/frame_manager.rb', line 254
def frames
@frames.values
end
|
#handle_attached_to_target(target) ⇒ Object
184
185
186
187
188
189
190
191
192
|
# File 'lib/puppeteer/frame_manager.rb', line 184
def handle_attached_to_target(target)
return if target.target_info.type != 'iframe'
frame = @frames[target.target_info.target_id]
session = target.session
frame&.send(:update_client, session)
setup_listeners(session)
async_init(target.target_info.target_id, session)
end
|
#handle_detached_from_target(target) ⇒ Object
195
196
197
198
199
200
201
202
|
# File 'lib/puppeteer/frame_manager.rb', line 195
def handle_detached_from_target(target)
frame = @frames[target.target_id]
if frame && frame.oop_frame?
remove_frame_recursively(frame)
end
end
|
#handle_execution_context_created(context_payload, session) ⇒ Object
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
|
# File 'lib/puppeteer/frame_manager.rb', line 409
def handle_execution_context_created(context_payload, session)
frame = if_present(context_payload.dig('auxData', 'frameId')) { |frame_id| @frames[frame_id] }
world = nil
if frame
if context_payload.dig('auxData', 'isDefault')
world = frame.main_world
elsif context_payload['name'] == UTILITY_WORLD_NAME && !frame.puppeteer_world.has_context?
world = frame.puppeteer_world
end
end
if context_payload.dig('auxData', 'type') == 'isolated'
@isolated_worlds << context_payload['name']
end
context = Puppeteer::ExecutionContext.new(frame&._client || @client, context_payload, world)
if world
world.context = context
end
key = "#{session.id}:#{context_payload['id']}"
@context_id_to_context[key] = context
end
|
#handle_execution_context_destroyed(execution_context_id, session) ⇒ Object
443
444
445
446
447
448
449
450
451
|
# File 'lib/puppeteer/frame_manager.rb', line 443
def handle_execution_context_destroyed(execution_context_id, session)
key = "#{session.id}:#{execution_context_id}"
context = @context_id_to_context[key]
return unless context
@context_id_to_context.delete(key)
if context.world
context.world.delete_context(execution_context_id)
end
end
|
#handle_execution_contexts_cleared(session) ⇒ Object
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
# File 'lib/puppeteer/frame_manager.rb', line 454
def handle_execution_contexts_cleared(session)
@context_id_to_context.select! do |execution_context_id, context|
if context.client != session
true else
if context.world
context.world.delete_context(execution_context_id)
end
false end
end
end
|
#handle_frame_attached(session, frame_id, parent_frame_id) ⇒ Object
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
|
# File 'lib/puppeteer/frame_manager.rb', line 267
def handle_frame_attached(session, frame_id, parent_frame_id)
if @frames.has_key?(frame_id)
frame = @frames[frame_id]
if session && frame.oop_frame?
frame.send(:update_client, session)
end
return
end
parent_frame = @frames[parent_frame_id]
if parent_frame
attach_child_frame(parent_frame, parent_frame_id, frame_id, session)
return
end
if @frames_pending_target_init[parent_frame_id]
@frames_pending_attachment[frame_id] ||= resolvable_future
@frames_pending_target_init[parent_frame_id].then do |_|
attach_child_frame(@frames[parent_frame_id], parent_frame_id, frame_id, session)
@frames_pending_attachment.delete(frame_id)&.fulfill(nil)
end
return
end
raise FrameNotFoundError.new("Parent frame #{parent_frame_id} not found.")
end
|
#handle_frame_detached(frame_id, reason) ⇒ Object
393
394
395
396
397
398
399
400
401
402
403
404
405
|
# File 'lib/puppeteer/frame_manager.rb', line 393
def handle_frame_detached(frame_id, reason)
frame = @frames[frame_id]
if reason == 'remove'
if frame
remove_frame_recursively(frame)
end
elsif reason == 'swap'
emit_event(FrameManagerEmittedEvents::FrameSwapped, frame)
end
end
|
#handle_frame_navigated(frame_payload) ⇒ Object
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/puppeteer/frame_manager.rb', line 310
def handle_frame_navigated(frame_payload)
frame_id = frame_payload['id']
is_main_frame = !frame_payload['parentId']
if @frames_pending_attachment[frame_id]
@frames_pending_attachment[frame_id].then do |_|
frame = is_main_frame ? @main_frame : @frames[frame_id]
reattach_frame(frame, frame_id, is_main_frame, frame_payload)
end
else
frame = is_main_frame ? @main_frame : @frames[frame_id]
reattach_frame(frame, frame_id, is_main_frame, frame_payload)
end
end
|
#handle_frame_navigated_within_document(frame_id, url) ⇒ Object
383
384
385
386
387
388
389
|
# File 'lib/puppeteer/frame_manager.rb', line 383
def handle_frame_navigated_within_document(frame_id, url)
frame = @frames[frame_id]
return unless frame
frame.navigated_within_document(url)
emit_event(FrameManagerEmittedEvents::FrameNavigatedWithinDocument, frame)
emit_event(FrameManagerEmittedEvents::FrameNavigated, frame)
end
|
#handle_frame_started_loading(frame_id) ⇒ Object
213
214
215
216
217
|
# File 'lib/puppeteer/frame_manager.rb', line 213
def handle_frame_started_loading(frame_id)
frame = @frames[frame_id]
return if !frame
frame.handle_loading_started
end
|
#handle_frame_stopped_loading(frame_id) ⇒ Object
220
221
222
223
224
225
|
# File 'lib/puppeteer/frame_manager.rb', line 220
def handle_frame_stopped_loading(frame_id)
frame = @frames[frame_id]
return if !frame
frame.handle_loading_stopped
emit_event(FrameManagerEmittedEvents::LifecycleEvent, frame)
end
|
#handle_frame_tree(session, frame_tree) ⇒ Object
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/puppeteer/frame_manager.rb', line 229
def handle_frame_tree(session, frame_tree)
if frame_tree['frame']['parentId']
handle_frame_attached(session, frame_tree['frame']['id'], frame_tree['frame']['parentId'])
end
unless @frame_naviigated_received.delete?(frame_tree['frame']['id'])
handle_frame_navigated(frame_tree['frame'])
end
return if !frame_tree['childFrames']
frame_tree['childFrames'].each do |child|
handle_frame_tree(session, child)
end
end
|
#handle_lifecycle_event(event) ⇒ Object
205
206
207
208
209
210
|
# File 'lib/puppeteer/frame_manager.rb', line 205
def handle_lifecycle_event(event)
frame = @frames[event['frameId']]
return if !frame
frame.handle_lifecycle_event(event['loaderId'], event['name'])
emit_event(FrameManagerEmittedEvents::LifecycleEvent, frame)
end
|
#main_frame ⇒ !Frame
249
250
251
|
# File 'lib/puppeteer/frame_manager.rb', line 249
def main_frame
@main_frame
end
|
#navigate_frame(frame, url, referer: nil, timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse
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
|
# File 'lib/puppeteer/frame_manager.rb', line 114
def navigate_frame(frame, url, referer: nil, timeout: nil, wait_until: nil)
assert_no_legacy_navigation_options(wait_until: wait_until)
navigate_params = {
url: url,
referer: referer || @network_manager.['referer'],
frameId: frame.id,
}.compact
option_wait_until = wait_until || ['load']
option_timeout = timeout || @timeout_settings.navigation_timeout
watcher = Puppeteer::LifecycleWatcher.new(self, frame, option_wait_until, option_timeout)
ensure_new_document_navigation = false
begin
navigate = future do
result = @client.send_message('Page.navigate', navigate_params)
loader_id = result['loaderId']
ensure_new_document_navigation = !!loader_id
if result['errorText']
raise NavigationError.new("#{result['errorText']} at #{url}")
end
end
await_any(
navigate,
watcher.timeout_or_termination_promise,
)
await_any(
watcher.timeout_or_termination_promise,
if ensure_new_document_navigation
watcher.new_document_navigation_promise
else
watcher.same_document_navigation_promise
end,
)
watcher.navigation_response
rescue Puppeteer::TimeoutError => err
raise NavigationError.new(err)
ensure
watcher.dispose
end
end
|
244
245
246
|
# File 'lib/puppeteer/frame_manager.rb', line 244
def page
@page
end
|
#wait_for_frame_navigation(frame, timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/puppeteer/frame_manager.rb', line 162
def wait_for_frame_navigation(frame, timeout: nil, wait_until: nil)
assert_no_legacy_navigation_options(wait_until: wait_until)
option_wait_until = wait_until || ['load']
option_timeout = timeout || @timeout_settings.navigation_timeout
watcher = Puppeteer::LifecycleWatcher.new(self, frame, option_wait_until, option_timeout)
begin
await_any(
watcher.timeout_or_termination_promise,
watcher.same_document_navigation_promise,
watcher.new_document_navigation_promise,
)
watcher.navigation_response
rescue Puppeteer::TimeoutError => err
raise NavigationError.new(err)
ensure
watcher.dispose
end
end
|