Module: Ferrum::Page::Frames

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#main_frameObject (readonly)

Returns the value of attribute main_frame.



8
9
10
# File 'lib/ferrum/page/frames.rb', line 8

def main_frame
  @main_frame
end

Instance Method Details

#frame_by(id: nil, name: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/ferrum/page/frames.rb', line 14

def frame_by(id: nil, name: nil)
  if id
    @frames[id]
  elsif name
    frames.find { |f| f.name == name }
  else
    raise ArgumentError
  end
end

#framesObject



10
11
12
# File 'lib/ferrum/page/frames.rb', line 10

def frames
  @frames.values
end

#frames_subscribeObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ferrum/page/frames.rb', line 24

def frames_subscribe
  on("Page.frameAttached") do |params|
    parent_frame_id, frame_id = params.values_at("parentFrameId", "frameId")
    @frames[frame_id] = Frame.new(frame_id, self, parent_frame_id)
  end

  on("Page.frameStartedLoading") do |params|
    frame = @frames[params["frameId"]]
    frame.state = :started_loading
    @event.reset
  end

  on("Page.frameNavigated") do |params|
    frame_id, name = params["frame"]&.values_at("id", "name")
    frame = @frames[frame_id]
    frame.state = :navigated
    frame.name = name unless name.to_s.empty?
  end

  on("Page.frameStoppedLoading") do |params|
    # `DOM.performSearch` doesn't work without getting #document node first.
    # It returns node with nodeId 1 and nodeType 9 from which descend the
    # tree and we save it in a variable because if we call that again root
    # node will change the id and all subsequent nodes have to change id too.
    if @main_frame.id == params["frameId"]
      @event.set if idling?
      get_document_id
    end

    frame = @frames[params["frameId"]]
    frame.state = :stopped_loading

    @event.set if idling?
  end

  on("Page.navigatedWithinDocument") do
    @event.set if idling?
  end

  on("Network.requestWillBeSent") do |params|
    if params["frameId"] == @main_frame.id
      # Possible types:
      # Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR,
      # Fetch, EventSource, WebSocket, Manifest, SignedExchange, Ping,
      # CSPViolationReport, Other
      @event.reset if params["type"] == "Document"
    end
  end

  on("Runtime.executionContextCreated") do |params|
    setting_up_main_frame = false
    context_id = params.dig("context", "id")
    frame_id = params.dig("context", "auxData", "frameId")

    unless @main_frame.id
      root_frame = command("Page.getFrameTree").dig("frameTree", "frame", "id")
      if frame_id == root_frame
        setting_up_main_frame = true
        @main_frame.id = frame_id
        @frames[frame_id] = @main_frame
      end
    end

    frame = @frames[frame_id] || Frame.new(frame_id, self)
    frame.set_execution_id(context_id)

    # Set event because `execution_id` might raise NoExecutionContextError
    @event.set if setting_up_main_frame

    @frames[frame_id] ||= frame
  end

  on("Runtime.executionContextDestroyed") do |params|
    execution_id = params["executionContextId"]
    frame = frames.find { |f| f.execution_id?(execution_id) }
    frame.reset_execution_id
  end

  on("Runtime.executionContextsCleared") do
    @frames.delete_if { |_, f| !f.main? }
    @main_frame.reset_execution_id
  end
end