Class: Puppeteer::FrameManager
- Inherits:
-
Object
- Object
- Puppeteer::FrameManager
- Includes:
- DebugPrint, EventCallbackable, IfPresent
- Defined in:
- lib/puppeteer/frame_manager.rb
Defined Under Namespace
Classes: FrameNotFoundError, NavigationError
Constant Summary collapse
- UTILITY_WORLD_NAME =
'__puppeteer_utility_world__'- CHROME_EXTENSION_PREFIX =
'chrome-extension://'
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#network_manager ⇒ Object
readonly
Returns the value of attribute network_manager.
-
#timeout_settings ⇒ Object
readonly
Returns the value of attribute timeout_settings.
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, network_enabled: true) ⇒ FrameManager
constructor
A new instance of FrameManager.
- #main_frame ⇒ !Frame
- #navigate_frame(frame, url, referer: nil, referrer_policy: nil, timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse
- #page ⇒ !Puppeteer.Page
- #wait_for_frame_navigation(frame, timeout: nil, wait_until: nil, ignore_same_document_navigation: false) ⇒ Puppeteer::HTTPResponse
Methods included from EventCallbackable
#add_event_listener, #emit_event, #observe_first, #off, #on_event, #remove_event_listener
Methods included from IfPresent
Methods included from DebugPrint
Constructor Details
#initialize(client, page, ignore_https_errors, timeout_settings, network_enabled: true) ⇒ 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 41 42 43 |
# File 'lib/puppeteer/frame_manager.rb', line 15 def initialize(client, page, ignore_https_errors, timeout_settings, network_enabled: true) @client = client @page = page @network_manager = Puppeteer::NetworkManager.new(client, ignore_https_errors, self, network_enabled: network_enabled) @timeout_settings = timeout_settings # @type {!Map<string, !Frame>} @frames = {} @frame_naviigated_received = Set.new # @type {!Map<number, !ExecutionContext>} @context_id_to_context = {} # @type {!Set<string>} @isolated_worlds = Set.new # Keeps track of OOPIF targets/frames (target ID == frame ID for OOPIFs) # that are being initialized. @frames_pending_target_init = {} # Keeps track of frames that are in the process of being attached in #onFrameAttached. @frames_pending_attachment = {} @frame_tree_handled_promise = nil @queued_frame_events = [] @frame_tree_mutex = Mutex.new setup_listeners(@client) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
98 99 100 |
# File 'lib/puppeteer/frame_manager.rb', line 98 def client @client end |
#network_manager ⇒ Object (readonly)
Returns the value of attribute network_manager.
136 137 138 |
# File 'lib/puppeteer/frame_manager.rb', line 136 def network_manager @network_manager end |
#timeout_settings ⇒ Object (readonly)
Returns the value of attribute timeout_settings.
98 99 100 |
# File 'lib/puppeteer/frame_manager.rb', line 98 def timeout_settings @timeout_settings end |
Instance Method Details
#execution_context_by_id(context_id, session) ⇒ Object
587 588 589 590 |
# File 'lib/puppeteer/frame_manager.rb', line 587 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
346 347 348 |
# File 'lib/puppeteer/frame_manager.rb', line 346 def frame(frame_id) @frames[frame_id] end |
#frames ⇒ !Array<!Frame>
340 341 342 |
# File 'lib/puppeteer/frame_manager.rb', line 340 def frames @frames.values end |
#handle_attached_to_target(target) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/puppeteer/frame_manager.rb', line 226 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 do async_init(target.target_info.target_id, session).wait rescue => err debug_puts(err) end end |
#handle_detached_from_target(target) ⇒ Object
241 242 243 244 245 246 247 248 |
# File 'lib/puppeteer/frame_manager.rb', line 241 def handle_detached_from_target(target) frame = @frames[target.target_id] if frame && frame.oop_frame? # When an OOP iframe is removed from the page, it # will only get a Target.detachedFromTarget event. remove_frame_recursively(frame) end end |
#handle_execution_context_created(context_payload, session) ⇒ Object
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 |
# File 'lib/puppeteer/frame_manager.rb', line 504 def handle_execution_context_created(context_payload, session) frame = if_present(context_payload.dig('auxData', 'frameId')) { |frame_id| @frames[frame_id] } origin = context_payload['origin'] world = nil if frame # commented out the original implementation for allowing us to use Frame#evaluate on OOP iframe. # # # Only care about execution contexts created for the current session. # return if @client != session if context_payload.dig('auxData', 'isDefault') world = frame.main_world elsif context_payload['name'] == UTILITY_WORLD_NAME && !frame.puppeteer_world.has_context? # In case of multiple sessions to the same target, there's a race between # connections so we might end up creating multiple isolated worlds. # We can use either. world = frame.puppeteer_world elsif extension_origin?(origin) extension_id = extract_extension_id(origin) if extension_id world = frame.extension_worlds[extension_id] unless world world = Puppeteer::IsolaatedWorld.new(frame._client || @client, self, frame, @timeout_settings) frame.extension_worlds[extension_id] = world end world.origin = origin world.world_id = extension_id end 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
563 564 565 566 567 568 569 |
# File 'lib/puppeteer/frame_manager.rb', line 563 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) context.world&.delete_context(context) end |
#handle_execution_contexts_cleared(session) ⇒ Object
572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/puppeteer/frame_manager.rb', line 572 def handle_execution_contexts_cleared(session) session_id = session.id @context_id_to_context.select! do |execution_context_id, context| key_session_id, _context_id = execution_context_id.split(':', 2) # Make sure to only clear execution contexts that belong # to the current session. if key_session_id != session_id true # keep else context.world&.delete_context(context) false # remove end end end |
#handle_frame_attached(session, frame_id, parent_frame_id) ⇒ Object
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 |
# File 'lib/puppeteer/frame_manager.rb', line 353 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? # If an OOP iframes becomes a normal iframe again # it is first attached to the parent page before # the target is removed. 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] ||= Async::Promise.new Async do @frames_pending_target_init[parent_frame_id].wait attach_child_frame(@frames[parent_frame_id], parent_frame_id, frame_id, session) @frames_pending_attachment.delete(frame_id)&.resolve(nil) end return end raise FrameNotFoundError.new("Parent frame #{parent_frame_id} not found.") end |
#handle_frame_detached(frame_id, reason) ⇒ Object
488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/puppeteer/frame_manager.rb', line 488 def handle_frame_detached(frame_id, reason) frame = @frames[frame_id] if reason == 'remove' # Only remove the frame if the reason for the detached event is # an actual removement of the frame. # For frames that become OOP iframes, the reason would be 'swap'. if frame remove_frame_recursively(frame) end elsif reason == 'swap' emit_event(FrameManagerEmittedEvents::FrameSwapped, frame) end end |
#handle_frame_navigated(frame_payload) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/puppeteer/frame_manager.rb', line 397 def handle_frame_navigated(frame_payload) frame_id = frame_payload['id'] is_main_frame = !frame_payload['parentId'] if @frames_pending_attachment[frame_id] Async do @frames_pending_attachment[frame_id].wait 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
478 479 480 481 482 483 484 |
# File 'lib/puppeteer/frame_manager.rb', line 478 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
299 300 301 302 303 |
# File 'lib/puppeteer/frame_manager.rb', line 299 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
306 307 308 309 310 311 |
# File 'lib/puppeteer/frame_manager.rb', line 306 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
315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/puppeteer/frame_manager.rb', line 315 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
251 252 253 254 255 256 |
# File 'lib/puppeteer/frame_manager.rb', line 251 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
335 336 337 |
# File 'lib/puppeteer/frame_manager.rb', line 335 def main_frame @main_frame end |
#navigate_frame(frame, url, referer: nil, referrer_policy: nil, timeout: nil, wait_until: nil) ⇒ Puppeteer::HTTPResponse
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 |
# File 'lib/puppeteer/frame_manager.rb', line 144 def navigate_frame(frame, url, referer: nil, referrer_policy: nil, timeout: nil, wait_until: nil) (wait_until: wait_until) referrer_policy ||= @network_manager.extra_http_headers['referer-policy'] protocol_referrer_policy = referrer_policy_to_protocol(referrer_policy) navigate_params = { url: url, referrer: referer || @network_manager.extra_http_headers['referer'], referrerPolicy: protocol_referrer_policy, frameId: frame.id, }.compact option_wait_until = wait_until || ['load'] option_timeout = timeout || @timeout_settings. watcher = Puppeteer::LifecycleWatcher.new(self, frame, option_wait_until, option_timeout) = false begin navigate = Async do Puppeteer::AsyncUtils.future_with_logging do result = @client.('Page.navigate', navigate_params) loader_id = result['loaderId'] = !!loader_id if result['errorText'] && result['errorText'] != 'net::ERR_HTTP_RESPONSE_CODE_FAILURE' raise NavigationError.new("#{result['errorText']} at #{url}") end end.call end Puppeteer::AsyncUtils.await_promise_race( navigate, watcher.timeout_or_termination_promise, ) Puppeteer::AsyncUtils.await_promise_race( watcher.timeout_or_termination_promise, if watcher. else watcher. end, ) watcher. rescue Puppeteer::TimeoutError => err raise err ensure watcher.dispose end end |
#page ⇒ !Puppeteer.Page
330 331 332 |
# File 'lib/puppeteer/frame_manager.rb', line 330 def page @page end |
#wait_for_frame_navigation(frame, timeout: nil, wait_until: nil, ignore_same_document_navigation: false) ⇒ Puppeteer::HTTPResponse
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 |
# File 'lib/puppeteer/frame_manager.rb', line 197 def (frame, timeout: nil, wait_until: nil, ignore_same_document_navigation: false) (wait_until: wait_until) option_wait_until = wait_until || ['load'] option_timeout = timeout || @timeout_settings. watcher = Puppeteer::LifecycleWatcher.new(self, frame, option_wait_until, option_timeout) begin if Puppeteer::AsyncUtils.await_promise_race( watcher.timeout_or_termination_promise, watcher., ) else Puppeteer::AsyncUtils.await_promise_race( watcher.timeout_or_termination_promise, watcher., watcher., ) end watcher. rescue Puppeteer::TimeoutError => err raise err ensure watcher.dispose end end |