Module: Ferrum::Interceptable

Included in:
Page, Worker
Defined in:
lib/ferrum/interceptable.rb

Overview

Shared by Page and Worker: subscribes to the CDP events behind the :request (Fetch-domain request interception) and :auth (proxy/basic auth challenges) pseudo-events, on top of the includer's own client and network. Anything else is passed straight through to client.

Instance Method Summary collapse

Instance Method Details

#off(name, id) ⇒ void

This method returns an undefined value.

Unsubscribes a listener previously registered via #on.

Parameters:

  • name (Symbol, String)
  • id (Integer)

    The subscription id returned by #on.



42
43
44
45
46
47
48
49
50
51
# File 'lib/ferrum/interceptable.rb', line 42

def off(name, id)
  case name
  when :request
    client.off("Fetch.requestPaused", id)
  when :auth
    client.off("Fetch.authRequired", id)
  else
    client.off(name, id)
  end
end

#on(name, &block) ⇒ Integer

Subscribes to a CDP event, or to :request/:auth.

Parameters:

  • name (Symbol, String)

Returns:

  • (Integer)

    The subscription id, used to unsubscribe via #off.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ferrum/interceptable.rb', line 15

def on(name, &block)
  case name
  when :request
    client.on("Fetch.requestPaused") do |params, index, total|
      request = Network::InterceptedRequest.new(client, params)
      exchange = network.find_or_build_exchange(request.network_id)
      exchange.intercepted_request = request
      block.call(request, index, total)
    end
  when :auth
    client.on("Fetch.authRequired") do |params, index, total|
      request = Network::AuthRequest.new(self, params)
      block.call(request, index, total)
    end
  else
    client.on(name, &block)
  end
end

#subscribed?(event) ⇒ Boolean

Whether there's at least one callback registered for the event.

Parameters:

  • event (String)

Returns:

  • (Boolean)


58
59
60
# File 'lib/ferrum/interceptable.rb', line 58

def subscribed?(event)
  client.subscribed?(event)
end