@@ -11,9 +11,10 @@ module Ferrum
1111 class Network
1212 CLEAR_TYPE = %i[ traffic cache ] . freeze
1313 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
1718 AUTHORIZE_BLOCK_MISSING = "Block is missing, call `authorize(...) { |r| r.continue } " \
1819 "or subscribe to `on(:request)` events before calling it"
1920 AUTHORIZE_TYPE_WRONG = ":type should be in #{ AUTHORIZE_TYPE } "
@@ -187,11 +188,20 @@ def whitelist=(patterns)
187188 # end
188189 # browser.go_to("https://google.com")
189190 #
190- def intercept ( pattern : "*" , resource_type : nil )
191+ def intercept ( pattern : "*" , resource_type : nil , request_stage : nil , handle_auth_requests : true )
191192 pattern = { urlPattern : pattern }
192- pattern [ :resourceType ] = resource_type if resource_type && RESOURCE_TYPES . include? ( resource_type . to_s )
193193
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 )
195205 end
196206
197207 #
0 commit comments