@@ -11,9 +11,10 @@ module Ferrum
11
11
class Network
12
12
CLEAR_TYPE = %i[ traffic cache ] . freeze
13
13
AUTHORIZE_TYPE = %i[ server proxy ] . freeze
14
- RESOURCE_TYPES = %w[ Document Stylesheet Image Media Font Script TextTrack
15
- XHR Fetch EventSource WebSocket Manifest
16
- SignedExchange Ping CSPViolationReport Other ] . freeze
14
+ REQUEST_STAGES = %i[ Request Response ] . freeze
15
+ RESOURCE_TYPES = %i[ Document Stylesheet Image Media Font Script TextTrack
16
+ XHR Fetch Prefetch EventSource WebSocket Manifest
17
+ SignedExchange Ping CSPViolationReport Preflight Other ] . freeze
17
18
AUTHORIZE_BLOCK_MISSING = "Block is missing, call `authorize(...) { |r| r.continue } " \
18
19
"or subscribe to `on(:request)` events before calling it"
19
20
AUTHORIZE_TYPE_WRONG = ":type should be in #{ AUTHORIZE_TYPE } "
@@ -187,11 +188,20 @@ def whitelist=(patterns)
187
188
# end
188
189
# browser.go_to("https://google.com")
189
190
#
190
- def intercept ( pattern : "*" , resource_type : nil )
191
+ def intercept ( pattern : "*" , resource_type : nil , request_stage : nil , handle_auth_requests : true )
191
192
pattern = { urlPattern : pattern }
192
- pattern [ :resourceType ] = resource_type if resource_type && RESOURCE_TYPES . include? ( resource_type . to_s )
193
193
194
- @page . command ( "Fetch.enable" , handleAuthRequests : true , patterns : [ pattern ] )
194
+ if resource_type && RESOURCE_TYPES . none? ( resource_type . to_sym )
195
+ raise ArgumentError , "Unknown resource type '#{ resource_type } ' must be #{ RESOURCE_TYPES . join ( ' | ' ) } "
196
+ end
197
+
198
+ if request_stage && REQUEST_STAGES . none? ( request_stage . to_sym )
199
+ raise ArgumentError , "Unknown request stage '#{ request_stage } ' must be #{ REQUEST_STAGES . join ( ' | ' ) } "
200
+ end
201
+
202
+ pattern [ :resourceType ] = resource_type if resource_type
203
+ pattern [ :requestStage ] = request_stage if request_stage
204
+ @page . command ( "Fetch.enable" , patterns : [ pattern ] , handleAuthRequests : handle_auth_requests )
195
205
end
196
206
197
207
#
0 commit comments