Module: Ferrum::Interceptable

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

Overview

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

Defined Under Namespace

Classes: Chain

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.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ferrum/interceptable.rb', line 67

def off(name, id)
  case name
  when :console
    console.off(id)
  when :request, :auth
    chain(name).remove(id)
  when :response
    client.off("Network.loadingFinished", id)
  else
    client.off(name, id)
  end
end

#on(arg0) ⇒ Integer #on(arg0) ⇒ Integer #on(arg0) ⇒ Integer #on(arg0) ⇒ Integer #on(name) ⇒ Integer

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

:request and :auth callbacks form a chain: they run in the order they were added, and the rest are skipped as soon as one of them handles the object (continue, respond, abort). An object nobody handled is continued, an auth challenge gets Chrome's default response. The first :request or :auth callback turns request interception on, call network.intercept first only to narrow what it pauses.

Examples:

page.on(:console) { |message| warn(message) if message.error? }
page.on(:request) { |r| r.abort if r.match?(/ads/) }
page.on(:request) { |r| r.respond(body: "stub") if r.match?(/api/) }

Overloads:

  • #on(arg0) ⇒ Integer

    Parameters:

    • arg0 (:request)

    Returns:

    • (Integer)
  • #on(arg0) ⇒ Integer

    Parameters:

    • arg0 (:response)

    Returns:

    • (Integer)
  • #on(arg0) ⇒ Integer

    Parameters:

    • arg0 (:auth)

    Returns:

    • (Integer)
  • #on(arg0) ⇒ Integer

    Parameters:

    • arg0 (:console)

    Returns:

    • (Integer)
  • #on(name) ⇒ Integer

    Parameters:

    • name (Symbol, String)

    Returns:

    • (Integer)

Parameters:

  • name (Symbol, String)

Yields:

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (Integer) —

    The subscription id, used to unsubscribe via #off.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ferrum/interceptable.rb', line 41

def on(name, &block)
  case name
  when :console
    console.on(&block)
  when :request, :auth
    id = chain(name).add(&block)
    network.ensure_interception(auth: name == :auth)
    id
  when :response
    client.on("Network.loadingFinished") do |params|
      exchange = network.select(params["requestId"]).last
      block.call(exchange) if exchange
    end
  else
    client.on(name, &block)
  end
end

#subscribe_interception ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Listens to paused requests and auth challenges for good, so the ones no callback handles are still answered, even after the last one is removed.



95
96
97
98
# File 'lib/ferrum/interceptable.rb', line 95

def subscribe_interception
  chain(:request)
  chain(:auth)
end

#subscribed?(event) ⇒ Boolean

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

Parameters:

  • event (String)

Returns:

  • (Boolean)


85
86
87
# File 'lib/ferrum/interceptable.rb', line 85

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