@@ -24,24 +24,47 @@ def frame_by(id: nil, name: nil, execution_id: nil)
2424 end
2525
2626 def frames_subscribe
27+ subscribe_frame_attached
28+ subscribe_frame_started_loading
29+ subscribe_frame_navigated
30+ subscribe_frame_stopped_loading
31+
32+ subscribe_navigated_within_document
33+
34+ subscribe_request_will_be_sent
35+
36+ subscribe_execution_context_created
37+ subscribe_execution_context_destroyed
38+ subscribe_execution_contexts_cleared
39+ end
40+
41+ private
42+
43+ def subscribe_frame_attached
2744 on ( "Page.frameAttached" ) do |params |
2845 parent_frame_id , frame_id = params . values_at ( "parentFrameId" , "frameId" )
2946 @frames [ frame_id ] = Frame . new ( frame_id , self , parent_frame_id )
3047 end
48+ end
3149
50+ def subscribe_frame_started_loading
3251 on ( "Page.frameStartedLoading" ) do |params |
3352 frame = @frames [ params [ "frameId" ] ]
3453 frame . state = :started_loading
3554 @event . reset
3655 end
56+ end
3757
58+ def subscribe_frame_navigated
3859 on ( "Page.frameNavigated" ) do |params |
3960 frame_id , name = params [ "frame" ] &.values_at ( "id" , "name" )
4061 frame = @frames [ frame_id ]
4162 frame . state = :navigated
4263 frame . name = name unless name . to_s . empty?
4364 end
65+ end
4466
67+ def subscribe_frame_stopped_loading
4568 on ( "Page.frameStoppedLoading" ) do |params |
4669 # `DOM.performSearch` doesn't work without getting #document node first.
4770 # It returns node with nodeId 1 and nodeType 9 from which descend the
@@ -57,19 +80,25 @@ def frames_subscribe
5780
5881 @event . set if idling?
5982 end
83+ end
6084
85+ def subscribe_navigated_within_document
6186 on ( "Page.navigatedWithinDocument" ) do
6287 @event . set if idling?
6388 end
89+ end
6490
91+ def subscribe_request_will_be_sent
6592 on ( "Network.requestWillBeSent" ) do |params |
6693 # Possible types:
6794 # Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR,
6895 # Fetch, EventSource, WebSocket, Manifest, SignedExchange, Ping,
6996 # CSPViolationReport, Other
7097 @event . reset if params [ "frameId" ] == @main_frame . id && params [ "type" ] == "Document"
7198 end
99+ end
72100
101+ def subscribe_execution_context_created
73102 on ( "Runtime.executionContextCreated" ) do |params |
74103 context_id = params . dig ( "context" , "id" )
75104 frame_id = params . dig ( "context" , "auxData" , "frameId" )
@@ -87,21 +116,23 @@ def frames_subscribe
87116
88117 @frames [ frame_id ] ||= frame
89118 end
119+ end
90120
121+ def subscribe_execution_context_destroyed
91122 on ( "Runtime.executionContextDestroyed" ) do |params |
92123 execution_id = params [ "executionContextId" ]
93124 frame = frame_by ( execution_id : execution_id )
94125 frame &.execution_id = nil
95126 end
127+ end
96128
129+ def subscribe_execution_contexts_cleared
97130 on ( "Runtime.executionContextsCleared" ) do
98131 @frames . delete_if { |_ , f | !f . main? }
99132 @main_frame . execution_id = nil
100133 end
101134 end
102135
103- private
104-
105136 def idling?
106137 @frames . all? { |_ , f | f . state == :stopped_loading }
107138 end
0 commit comments