Class: Puppeteer::FrameManager
- Inherits:
-
Object
- Object
- Puppeteer::FrameManager
- Includes:
- DebugPrint, EventCallbackable, IfPresent
- Defined in:
- lib/puppeteer/frame_manager.rb
Defined Under Namespace
Classes: NavigationError
Constant Summary collapse
- UTILITY_WORLD_NAME =
'__puppeteer_utility_world__'
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
- #ensure_isolated_world(name) ⇒ Object
- #execution_context_by_id(context_id) ⇒ Object
- #frame(frame_id) ⇒ ?Frame
- #frames ⇒ !Array<!Frame>
- #handle_execution_context_created(context_payload) ⇒ Object
- #handle_execution_context_destroyed(execution_context_id) ⇒ Object
- #handle_execution_contexts_cleared ⇒ Object
- #handle_frame_attached(frame_id, parent_frame_id) ⇒ Object
- #handle_frame_detached(frame_id) ⇒ Object
- #handle_frame_navigated(frame_payload) ⇒ Object
- #handle_frame_navigated_within_document(frame_id, url) ⇒ Object
- #handle_frame_stopped_loading(frame_id) ⇒ Object
- #handle_frame_tree(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::Response
- #page ⇒ !Puppeteer.Page
- #wait_for_frame_navigation(frame, timeout: nil, wait_until: nil) ⇒ Puppeteer::Response
Methods included from EventCallbackable
#add_event_listener, #emit_event, #on_event, #remove_event_listener
Methods included from IfPresent
Methods included from DebugPrint
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 # @type {!Map<string, !Frame>} @frames = {} # @type {!Map<number, !ExecutionContext>} @context_id_to_context = {} @context_id_created = {} # @type {!Set<string>} @isolated_worlds = Set.new @client.on_event 'Page.frameAttached' do |event| handle_frame_attached(event['frameId'], event['parentFrameId']) end @client.on_event 'Page.frameNavigated' do |event| handle_frame_navigated(event['frame']) end @client.on_event 'Page.navigatedWithinDocument' do |event| handle_frame_navigated_within_document(event['frameId'], event['url']) end @client.on_event 'Page.frameDetached' do |event| handle_frame_detached(event['frameId']) end @client.on_event 'Page.frameStoppedLoading' do |event| handle_frame_stopped_loading(event['frameId']) end @client.on_event 'Runtime.executionContextCreated' do |event| handle_execution_context_created(event['context']) end @client.on_event 'Runtime.executionContextDestroyed' do |event| handle_execution_context_destroyed(event['executionContextId']) end @client.on_event 'Runtime.executionContextsCleared' do |event| handle_execution_contexts_cleared end @client.on_event 'Page.lifecycleEvent' do |event| handle_lifecycle_event(event) end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
60 61 62 |
# File 'lib/puppeteer/frame_manager.rb', line 60 def client @client end |
#network_manager ⇒ Object (readonly)
Returns the value of attribute network_manager.
81 82 83 |
# File 'lib/puppeteer/frame_manager.rb', line 81 def network_manager @network_manager end |
#timeout_settings ⇒ Object (readonly)
Returns the value of attribute timeout_settings.
60 61 62 |
# File 'lib/puppeteer/frame_manager.rb', line 60 def timeout_settings @timeout_settings end |
Instance Method Details
#ensure_isolated_world(name) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/puppeteer/frame_manager.rb', line 262 def ensure_isolated_world(name) return if @isolated_worlds.include?(name) @isolated_worlds << name @client.('Page.addScriptToEvaluateOnNewDocument', source: "//# sourceURL=#{Puppeteer::ExecutionContext::EVALUATION_SCRIPT_URL}", worldName: name, ) create_isolated_worlds_promises = frames.map do |frame| @client.('Page.createIsolatedWorld', frameId: frame.id, grantUniveralAccess: true, worldName: name, ) end await_all(*create_isolated_worlds_promises) end |
#execution_context_by_id(context_id) ⇒ Object
357 358 359 360 361 362 363 |
# File 'lib/puppeteer/frame_manager.rb', line 357 def execution_context_by_id(context_id) context = @context_id_to_context[context_id] if !context raise "INTERNAL ERROR: missing context with id = #{context_id}" end context end |
#frame(frame_id) ⇒ ?Frame
202 203 204 |
# File 'lib/puppeteer/frame_manager.rb', line 202 def frame(frame_id) @frames[frame_id] end |
#frames ⇒ !Array<!Frame>
196 197 198 |
# File 'lib/puppeteer/frame_manager.rb', line 196 def frames @frames.values end |
#handle_execution_context_created(context_payload) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/puppeteer/frame_manager.rb', line 301 def handle_execution_context_created(context_payload) 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.secondary_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.secondary_world end end if context_payload.dig('auxData', 'type') == 'isolated' @isolated_worlds << context_payload['name'] end context = Puppeteer::ExecutionContext.new(@client, context_payload, world) if world world.context = context end @context_id_to_context[context_payload['id']] = context @context_id_created[context_payload['id']] = Time.now end |
#handle_execution_context_destroyed(execution_context_id) ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'lib/puppeteer/frame_manager.rb', line 329 def handle_execution_context_destroyed(execution_context_id) context = @context_id_to_context[execution_context_id] return if !context @context_id_to_context.delete(execution_context_id) @context_id_created.delete(execution_context_id) if context.world context.world.delete_context(execution_context_id) end end |
#handle_execution_contexts_cleared ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/puppeteer/frame_manager.rb', line 339 def handle_execution_contexts_cleared # executionContextsCleared is often notified after executionContextCreated. # D, [2020-04-06T01:47:03.101227 #13823] DEBUG -- : RECV << {"method"=>"Runtime.executionContextCreated", "params"=>{"context"=>{"id"=>5, "origin"=>"https://github.com", "name"=>"", "auxData"=>{"isDefault"=>true, "type"=>"default", "frameId"=>"71C347B70848B89DDDEFAA8AB5B0BC92"}}}, "sessionId"=>"53F088EED260C28001D26A019F95D9E3"} # D, [2020-04-06T01:47:03.101439 #13823] DEBUG -- : RECV << {"method"=>"Page.frameNavigated", "params"=>{"frame"=>{"id"=>"71C347B70848B89DDDEFAA8AB5B0BC92", "loaderId"=>"80338225D035AC96BAE8F6D4E81C7D51", "url"=>"https://github.com/search?q=puppeteer", "securityOrigin"=>"https://github.com", "mimeType"=>"text/html"}}, "sessionId"=>"53F088EED260C28001D26A019F95D9E3"} # D, [2020-04-06T01:47:03.101325 #13823] DEBUG -- : RECV << {"method"=>"Target.targetInfoChanged", "params"=>{"targetInfo"=>{"targetId"=>"71C347B70848B89DDDEFAA8AB5B0BC92", "type"=>"page", "title"=>"https://github.com/search?q=puppeteer", "url"=>"https://github.com/search?q=puppeteer", "attached"=>true, "browserContextId"=>"AF37BC660284CE1552B4ECB147BE9305"}}} # D, [2020-04-06T01:47:03.101269 #13823] DEBUG -- : RECV << {"method"=>"Runtime.executionContextsCleared", "params"=>{}, "sessionId"=>"53F088EED260C28001D26A019F95D9E3"} # it unexpectedly clears the created execution context. # To avoid the problem, just skip recent created ids. now = Time.now context_ids_to_skip = @context_id_created.select { |k, v| now - v < 1 }.keys @context_id_to_context.reject { |k, v| context_ids_to_skip.include?(k) }.each do |execution_context_id, context| if context.world context.world.delete_context(execution_context_id) end end @context_id_to_context.select! { |k, v| context_ids_to_skip.include?(k) } end |
#handle_frame_attached(frame_id, parent_frame_id) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/puppeteer/frame_manager.rb', line 208 def handle_frame_attached(frame_id, parent_frame_id) return if @frames.has_key?(frame_id) if !parent_frame_id raise ArgymentError.new('parent_frame_id must not be nil') end parent_frame = @frames[parent_frame_id] frame = Puppeteer::Frame.new(self, @client, parent_frame, frame_id) @frames[frame_id] = frame emit_event 'Events.FrameManager.FrameAttached', frame end |
#handle_frame_detached(frame_id) ⇒ Object
293 294 295 296 297 298 |
# File 'lib/puppeteer/frame_manager.rb', line 293 def handle_frame_detached(frame_id) frame = @frames[frame_id] if frame remove_frame_recursively(frame) end end |
#handle_frame_navigated(frame_payload) ⇒ Object
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 |
# File 'lib/puppeteer/frame_manager.rb', line 221 def handle_frame_navigated(frame_payload) is_main_frame = !frame_payload['parentId'] frame = if is_main_frame @main_frame else @frames[frame_payload['id']] end if !is_main_frame && !frame raise ArgumentError.new('We either navigate top level or have old version of the navigated frame') end # Detach all child frames first. if frame frame.child_frames.each do |child| remove_frame_recursively(child) end end # Update or create main frame. if is_main_frame if frame # Update frame id to retain frame identity on cross-process navigation. @frames.delete(frame.id) frame.id = frame_payload['id'] else # Initial main frame navigation. frame = Puppeteer::Frame.new(self, @client, nil, frame_payload['id']) end @frames[frame_payload['id']] = frame @main_frame = frame end # Update frame payload. frame.navigated(frame_payload) emit_event 'Events.FrameManager.FrameNavigated', frame end |
#handle_frame_navigated_within_document(frame_id, url) ⇒ Object
282 283 284 285 286 287 288 289 290 |
# File 'lib/puppeteer/frame_manager.rb', line 282 def handle_frame_navigated_within_document(frame_id, url) frame = @frames[frame_id] return if !frame frame.navigated_within_document(url) emit_event 'Events.FrameManager.FrameNavigatedWithinDocument', frame emit_event 'Events.FrameManager.FrameNavigated', frame handle_frame_manager_frame_navigated_within_document(frame) handle_frame_manager_frame_navigated(frame) end |
#handle_frame_stopped_loading(frame_id) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/puppeteer/frame_manager.rb', line 165 def handle_frame_stopped_loading(frame_id) frame = @frames[frame_id] return if !frame frame.handle_loading_stopped emit_event 'Events.FrameManager.LifecycleEvent', frame end |
#handle_frame_tree(frame_tree) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/puppeteer/frame_manager.rb', line 173 def handle_frame_tree(frame_tree) if frame_tree['frame']['parentId'] handle_frame_attached(frame_tree['frame']['id'], frame_tree['frame']['parentId']) end handle_frame_navigated(frame_tree['frame']) return if !frame_tree['childFrames'] frame_tree['childFrames'].each do |child| handle_frame_tree(child) end end |
#handle_lifecycle_event(event) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/puppeteer/frame_manager.rb', line 157 def handle_lifecycle_event(event) frame = @frames[event['frameId']] return if !frame frame.handle_lifecycle_event(event['loaderId'], event['name']) emit_event 'Events.FrameManager.LifecycleEvent', frame end |
#main_frame ⇒ !Frame
191 192 193 |
# File 'lib/puppeteer/frame_manager.rb', line 191 def main_frame @main_frame end |
#navigate_frame(frame, url, referer: nil, timeout: nil, wait_until: nil) ⇒ Puppeteer::Response
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 |
# File 'lib/puppeteer/frame_manager.rb', line 89 def navigate_frame(frame, url, referer: nil, timeout: nil, wait_until: nil) (wait_until: wait_until) navigate_params = { url: url, referer: referer || @network_manager.extra_http_headers['referer'], 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 = future do result = @client.('Page.navigate', navigate_params) loader_id = result['loaderId'] = !!loader_id if result['errorText'] raise NavigationError.new("#{result['errorText']} at #{url}") end end await_any( navigate, watcher.timeout_or_termination_promise, ) = if watcher. else watcher. end await_any( , watcher.timeout_or_termination_promise, ) ensure watcher.dispose end watcher. end |
#page ⇒ !Puppeteer.Page
186 187 188 |
# File 'lib/puppeteer/frame_manager.rb', line 186 def page @page end |
#wait_for_frame_navigation(frame, timeout: nil, wait_until: nil) ⇒ Puppeteer::Response
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/puppeteer/frame_manager.rb', line 137 def (frame, timeout: nil, wait_until: nil) (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 await_any( watcher.timeout_or_termination_promise, watcher., watcher., ) ensure watcher.dispose end watcher. end |