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 =
<<~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
<<~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

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 = <<~JS
    function() {
      return new Promise((__f, __r) => {
        try {
          arguments[arguments.length] = r => __f(r);
          arguments.length = arguments.length + 1;
          setTimeout(() => __r(new Error("timed out promise")), %s);
          %s
        } catch(error) {
          __r(error);
        }
      });
    }
  JS

  expression = template % [wait * 1000, expression]
  call(expression: expression, arguments: args, awaitPromise: true)
end

#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