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
SCRIPT_SRC_TAG =
"const script = document.createElement(\"script\");\nscript.src = arguments[0];\nscript.type = arguments[1];\nscript.onload = arguments[2];\ndocument.head.appendChild(script);\n"
SCRIPT_TEXT_TAG =
"const script = document.createElement(\"script\");\nscript.text = arguments[0];\nscript.type = arguments[1];\ndocument.head.appendChild(script);\narguments[2]();\n"
STYLE_TAG =
"const style = document.createElement(\"style\");\nstyle.type = \"text/css\";\nstyle.appendChild(document.createTextNode(arguments[0]));\ndocument.head.appendChild(style);\narguments[1]();\n"
"const link = document.createElement(\"link\");\nlink.rel = \"stylesheet\";\nlink.href = arguments[0];\nlink.onload = arguments[1];\ndocument.head.appendChild(link);\n"

Instance Method Summary collapse

Instance Method Details

#add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript") ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ferrum/frame/runtime.rb', line 90

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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ferrum/frame/runtime.rb', line 104

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



48
49
50
51
# File 'lib/ferrum/frame/runtime.rb', line 48

def evaluate(expression, *args)
  expression = "function() { return %s }" % expression
  call(expression: expression, arguments: args)
end

#evaluate_async(expression, wait, *args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ferrum/frame/runtime.rb', line 53

def evaluate_async(expression, wait, *args)
  template = "    function() {\n      return new Promise((__f, __r) => {\n        try {\n          arguments[arguments.length] = r => __f(r);\n          arguments.length = arguments.length + 1;\n          setTimeout(() => __r(new Error(\"timed out promise\")), %s);\n          %s\n        } catch(error) {\n          __r(error);\n        }\n      });\n    }\n  JS\n\n  expression = template % [wait * 1000, expression]\n  call(expression: expression, arguments: args, awaitPromise: true)\nend\n"

#evaluate_func(expression, *args, on: nil) ⇒ Object



79
80
81
# File 'lib/ferrum/frame/runtime.rb', line 79

def evaluate_func(expression, *args, on: nil)
  call(expression: expression, arguments: args, on: on)
end

#evaluate_on(node:, expression:, by_value: true, wait: 0) ⇒ Object



83
84
85
86
87
88
# File 'lib/ferrum/frame/runtime.rb', line 83

def evaluate_on(node:, expression:, by_value: true, wait: 0)
  options = { handle: true }
  expression = "function() { return %s }" % expression
  options = { handle: false, returnByValue: true } if by_value
  call(expression: expression, on: node, wait: wait, **options)
end

#execute(expression, *args) ⇒ Object



73
74
75
76
77
# File 'lib/ferrum/frame/runtime.rb', line 73

def execute(expression, *args)
  expression = "function() { %s }" % expression
  call(expression: expression, arguments: args, handle: false, returnByValue: true)
  true
end