Module: Ferrum::Frame::Runtime
- Included in:
- Ferrum::Frame
- Defined in:
- lib/ferrum/frame/runtime.rb
Constant Summary collapse
- INTERMITTENT_ATTEMPTS =
ENV.fetch("FERRUM_INTERMITTENT_ATTEMPTS", 6).to_i
- INTERMITTENT_SLEEP =
ENV.fetch("FERRUM_INTERMITTENT_SLEEP", 0.1).to_f
- EXECUTE_OPTIONS =
{ returnByValue: true, functionDeclaration: %(function() { %s }) }.freeze
- DEFAULT_OPTIONS =
{ functionDeclaration: %(function() { return %s }) }.freeze
- EVALUATE_ASYNC_OPTIONS =
{ awaitPromise: true, functionDeclaration: %( function() { return new Promise((__resolve, __reject) => { try { arguments[arguments.length] = r => __resolve(r); arguments.length = arguments.length + 1; setTimeout(() => __reject(new Error("timed out promise")), %s); %s } catch(error) { __reject(error); } }); } ) }.freeze
- SCRIPT_SRC_TAG =
<<~JS const script = document.createElement("script"); script.src = arguments[0]; script.type = arguments[1]; script.onload = arguments[2]; document.head.appendChild(script); JS
- SCRIPT_TEXT_TAG =
<<~JS const script = document.createElement("script"); script.text = arguments[0]; script.type = arguments[1]; document.head.appendChild(script); arguments[2](); JS
- STYLE_TAG =
<<~JS const style = document.createElement("style"); style.type = "text/css"; style.appendChild(document.createTextNode(arguments[0])); document.head.appendChild(style); arguments[1](); JS
- LINK_TAG =
<<~JS const link = document.createElement("link"); link.rel = "stylesheet"; link.href = arguments[0]; link.onload = arguments[1]; document.head.appendChild(link); JS
Instance Method Summary collapse
- #add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript") ⇒ Object
- #add_style_tag(url: nil, path: nil, content: nil) ⇒ Object
- #evaluate(expression, *args) ⇒ Object
- #evaluate_async(expression, wait_time, *args) ⇒ Object
- #evaluate_on(node:, expression:, by_value: true, wait: 0) ⇒ Object
- #execute(expression, *args) ⇒ Object
Instance Method Details
#add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript") ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ferrum/frame/runtime.rb', line 95 def add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript") expr, *args = if url [SCRIPT_SRC_TAG, url, type] elsif path || content if path content = File.read(path) content += "\n//# sourceURL=#{path}" end [SCRIPT_TEXT_TAG, content, type] end evaluate_async(expr, @page.timeout, *args) end |
#add_style_tag(url: nil, path: nil, content: nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ferrum/frame/runtime.rb', line 109 def add_style_tag(url: nil, path: nil, content: nil) expr, *args = if url [LINK_TAG, url] elsif path || content if path content = File.read(path) content += "\n//# sourceURL=#{path}" end [STYLE_TAG, content] end evaluate_async(expr, @page.timeout, *args) end |
#evaluate(expression, *args) ⇒ Object
63 64 65 |
# File 'lib/ferrum/frame/runtime.rb', line 63 def evaluate(expression, *args) call(*args, expression: expression) end |
#evaluate_async(expression, wait_time, *args) ⇒ Object
67 68 69 |
# File 'lib/ferrum/frame/runtime.rb', line 67 def evaluate_async(expression, wait_time, *args) call(*args, expression: expression, wait_time: wait_time * 1000, **EVALUATE_ASYNC_OPTIONS) end |
#evaluate_on(node:, expression:, by_value: true, wait: 0) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ferrum/frame/runtime.rb', line 76 def evaluate_on(node:, expression:, by_value: true, wait: 0) errors = [NodeNotFoundError, NoExecutionContextError] attempts, sleep = INTERMITTENT_ATTEMPTS, INTERMITTENT_SLEEP Ferrum.with_attempts(errors: errors, max: attempts, wait: sleep) do response = @page.command("DOM.resolveNode", nodeId: node.node_id) object_id = response.dig("object", "objectId") = DEFAULT_OPTIONS.merge(objectId: object_id) [:functionDeclaration] = [:functionDeclaration] % expression .merge!(returnByValue: by_value) response = @page.command("Runtime.callFunctionOn", wait: wait, **)["result"] .tap { |r| handle_error(r) } by_value ? response.dig("value") : handle_response(response) end end |
#execute(expression, *args) ⇒ Object
71 72 73 74 |
# File 'lib/ferrum/frame/runtime.rb', line 71 def execute(expression, *args) call(*args, expression: expression, handle: false, **EXECUTE_OPTIONS) true end |