Module: Ferrum::Page::Net

Included in:
Ferrum::Page
Defined in:
lib/ferrum/page/net.rb

Constant Summary collapse

RESOURCE_TYPES =
%w[Document Stylesheet Image Media Font Script TextTrack
XHR Fetch EventSource WebSocket Manifest
SignedExchange Ping CSPViolationReport Other]

Instance Method Summary collapse

Instance Method Details

#abort_request(interception_id) ⇒ Object



73
74
75
# File 'lib/ferrum/page/net.rb', line 73

def abort_request(interception_id)
  continue_request(interception_id, errorReason: "Aborted")
end

#authorize(user, password) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ferrum/page/net.rb', line 30

def authorize(user, password)
  @authorized_ids ||= []

  intercept_request do |request, index, total|
    if request.auth_challenge?(:server)
      response = authorized_response(@authorized_ids,
                                     request.interception_id,
                                     user, password)

      @authorized_ids << request.interception_id
      request.continue(authChallengeResponse: response)
    elsif index + 1 < total
      next # There are other callbacks that can handle this, skip
    else
      request.continue
    end
  end
end

#continue_request(interception_id, options = nil) ⇒ Object



67
68
69
70
71
# File 'lib/ferrum/page/net.rb', line 67

def continue_request(interception_id, options = nil)
  options ||= {}
  options = options.merge(interceptionId: interception_id)
  command("Network.continueInterceptedRequest", **options)
end

#intercept_request(pattern: "*", resource_type: nil, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/ferrum/page/net.rb', line 49

def intercept_request(pattern: "*", resource_type: nil, &block)
  pattern = { urlPattern: pattern }
  if resource_type && RESOURCE_TYPES.include?(resource_type.to_s)
    pattern[:resourceType] = resource_type
  end

  command("Network.setRequestInterception", patterns: [pattern])

  on_request_intercepted(&block) if block_given?
end

#on_request_intercepted(&block) ⇒ Object



60
61
62
63
64
65
# File 'lib/ferrum/page/net.rb', line 60

def on_request_intercepted(&block)
  @client.on("Network.requestIntercepted") do |params, index, total|
    request = Network::InterceptedRequest.new(self, params)
    block.call(request, index, total)
  end
end

#proxy_authorize(user, password) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ferrum/page/net.rb', line 10

def proxy_authorize(user, password)
  @proxy_authorized_ids ||= []

  if user && password
    intercept_request do |request, index, total|
      if request.auth_challenge?(:proxy)
        response = authorized_response(@proxy_authorized_ids,
                                       request.interception_id,
                                       user, password)
        @proxy_authorized_ids << request.interception_id
        request.continue(authChallengeResponse: response)
      elsif index + 1 < total
        next # There are other callbacks that can handle this, skip
      else
        request.continue
      end
    end
  end
end