Skip to content

Commit 280a6dc

Browse files
authored
fix: remove frame when frameDetached event is sent (#306)
1 parent fafc3a1 commit 280a6dc

File tree

3 files changed

+96
-83
lines changed

3 files changed

+96
-83
lines changed

lib/ferrum/frame/dom.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@
2020
module Ferrum
2121
class Frame
2222
module DOM
23+
SCRIPT_SRC_TAG = <<~JS
24+
const script = document.createElement("script");
25+
script.src = arguments[0];
26+
script.type = arguments[1];
27+
script.onload = arguments[2];
28+
document.head.appendChild(script);
29+
JS
30+
SCRIPT_TEXT_TAG = <<~JS
31+
const script = document.createElement("script");
32+
script.text = arguments[0];
33+
script.type = arguments[1];
34+
document.head.appendChild(script);
35+
arguments[2]();
36+
JS
37+
STYLE_TAG = <<~JS
38+
const style = document.createElement("style");
39+
style.type = "text/css";
40+
style.appendChild(document.createTextNode(arguments[0]));
41+
document.head.appendChild(style);
42+
arguments[1]();
43+
JS
44+
LINK_TAG = <<~JS
45+
const link = document.createElement("link");
46+
link.rel = "stylesheet";
47+
link.href = arguments[0];
48+
link.onload = arguments[1];
49+
document.head.appendChild(link);
50+
JS
51+
2352
#
2453
# Returns current top window `location href`.
2554
#
@@ -180,6 +209,60 @@ def at_css(selector, within: nil)
180209

181210
evaluate_func(expr, selector, within)
182211
end
212+
213+
#
214+
# Adds a `<script>` tag to the document.
215+
#
216+
# @param [String, nil] url
217+
#
218+
# @param [String, nil] path
219+
#
220+
# @param [String, nil] content
221+
#
222+
# @param [String] type
223+
#
224+
# @example
225+
# browser.add_script_tag(url: "http://example.com/stylesheet.css") # => true
226+
#
227+
def add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript")
228+
expr, *args = if url
229+
[SCRIPT_SRC_TAG, url, type]
230+
elsif path || content
231+
if path
232+
content = File.read(path)
233+
content += "\n//# sourceURL=#{path}"
234+
end
235+
[SCRIPT_TEXT_TAG, content, type]
236+
end
237+
238+
evaluate_async(expr, @page.timeout, *args)
239+
end
240+
241+
#
242+
# Adds a `<style>` tag to the document.
243+
#
244+
# @param [String, nil] url
245+
#
246+
# @param [String, nil] path
247+
#
248+
# @param [String, nil] content
249+
#
250+
# @example
251+
# browser.add_style_tag(content: "h1 { font-size: 40px; }") # => true
252+
#
253+
def add_style_tag(url: nil, path: nil, content: nil)
254+
expr, *args = if url
255+
[LINK_TAG, url]
256+
elsif path || content
257+
if path
258+
content = File.read(path)
259+
content += "\n//# sourceURL=#{path}"
260+
end
261+
[STYLE_TAG, content]
262+
end
263+
264+
evaluate_async(expr, @page.timeout, *args)
265+
end
183266
end
184267
end
185268
end

lib/ferrum/frame/runtime.rb

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,6 @@ module Runtime
1616
INTERMITTENT_ATTEMPTS = ENV.fetch("FERRUM_INTERMITTENT_ATTEMPTS", 6).to_i
1717
INTERMITTENT_SLEEP = ENV.fetch("FERRUM_INTERMITTENT_SLEEP", 0.1).to_f
1818

19-
SCRIPT_SRC_TAG = <<~JS
20-
const script = document.createElement("script");
21-
script.src = arguments[0];
22-
script.type = arguments[1];
23-
script.onload = arguments[2];
24-
document.head.appendChild(script);
25-
JS
26-
SCRIPT_TEXT_TAG = <<~JS
27-
const script = document.createElement("script");
28-
script.text = arguments[0];
29-
script.type = arguments[1];
30-
document.head.appendChild(script);
31-
arguments[2]();
32-
JS
33-
STYLE_TAG = <<~JS
34-
const style = document.createElement("style");
35-
style.type = "text/css";
36-
style.appendChild(document.createTextNode(arguments[0]));
37-
document.head.appendChild(style);
38-
arguments[1]();
39-
JS
40-
LINK_TAG = <<~JS
41-
const link = document.createElement("link");
42-
link.rel = "stylesheet";
43-
link.href = arguments[0];
44-
link.onload = arguments[1];
45-
document.head.appendChild(link);
46-
JS
47-
4819
#
4920
# Evaluate and return result for given JS expression.
5021
#
@@ -126,60 +97,6 @@ def evaluate_on(node:, expression:, by_value: true, wait: 0)
12697
call(expression: expression, on: node, wait: wait, **options)
12798
end
12899

129-
#
130-
# Adds a `<script>` tag to the document.
131-
#
132-
# @param [String, nil] url
133-
#
134-
# @param [String, nil] path
135-
#
136-
# @param [String, nil] content
137-
#
138-
# @param [String] type
139-
#
140-
# @example
141-
# browser.add_script_tag(url: "http://example.com/stylesheet.css") # => true
142-
#
143-
def add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript")
144-
expr, *args = if url
145-
[SCRIPT_SRC_TAG, url, type]
146-
elsif path || content
147-
if path
148-
content = File.read(path)
149-
content += "\n//# sourceURL=#{path}"
150-
end
151-
[SCRIPT_TEXT_TAG, content, type]
152-
end
153-
154-
evaluate_async(expr, @page.timeout, *args)
155-
end
156-
157-
#
158-
# Adds a `<style>` tag to the document.
159-
#
160-
# @param [String, nil] url
161-
#
162-
# @param [String, nil] path
163-
#
164-
# @param [String, nil] content
165-
#
166-
# @example
167-
# browser.add_style_tag(content: "h1 { font-size: 40px; }") # => true
168-
#
169-
def add_style_tag(url: nil, path: nil, content: nil)
170-
expr, *args = if url
171-
[LINK_TAG, url]
172-
elsif path || content
173-
if path
174-
content = File.read(path)
175-
content += "\n//# sourceURL=#{path}"
176-
end
177-
[STYLE_TAG, content]
178-
end
179-
180-
evaluate_async(expr, @page.timeout, *args)
181-
end
182-
183100
private
184101

185102
def call(expression:, arguments: [], on: nil, wait: 0, handle: true, **options)

lib/ferrum/page/frames.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def frame_by(id: nil, name: nil, execution_id: nil)
6464

6565
def frames_subscribe
6666
subscribe_frame_attached
67+
subscribe_frame_detached
6768
subscribe_frame_started_loading
6869
subscribe_frame_navigated
6970
subscribe_frame_stopped_loading
@@ -86,6 +87,18 @@ def subscribe_frame_attached
8687
end
8788
end
8889

90+
def subscribe_frame_detached
91+
on("Page.frameDetached") do |params|
92+
frame = @frames[params["frameId"]]
93+
94+
if frame.main?
95+
frame.execution_id = nil
96+
else
97+
@frames.delete(frame.id)
98+
end
99+
end
100+
end
101+
89102
def subscribe_frame_started_loading
90103
on("Page.frameStartedLoading") do |params|
91104
frame = @frames[params["frameId"]]

0 commit comments

Comments
 (0)