module Ferrum::Frame::DOM

def add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript")

Parameters:
  • type (String) --
  • content (String, nil) --
  • path (String, nil) --
  • url (String, nil) --
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

def add_style_tag(url: nil, path: nil, content: nil)

Parameters:
  • content (String, nil) --
  • path (String, nil) --
  • url (String, nil) --
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

def at_css(selector, within: nil)

Returns:
  • (Node, nil) -

Parameters:
  • within (Node, nil) --
  • selector (String) --
def at_css(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      return within.querySelector(selector);
    }
  JS
  evaluate_func(expr, selector, within)
end

def at_xpath(selector, within: nil)

Returns:
  • (Node, nil) -

Parameters:
  • within (Node, nil) --
  • selector (String) --
def at_xpath(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      let xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      return xpath.snapshotItem(0);
    }
  JS
  evaluate_func(expr, selector, within)
end

def body

Returns:
  • (String) -
def body
  evaluate("document.documentElement.outerHTML")
end

def css(selector, within: nil)

Returns:
  • (Array) -

Parameters:
  • within (Node, nil) --
  • selector (String) --
def css(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      return Array.from(within.querySelectorAll(selector));
    }
  JS
  evaluate_func(expr, selector, within)
end

def current_title

Returns:
  • (String) -
def current_title
  evaluate("window.top.document.title")
end

def current_url

Returns:
  • (String) -
def current_url
  evaluate("window.top.location.href")
end

def doctype

def doctype
  evaluate("document.doctype && new XMLSerializer().serializeToString(document.doctype)")
end

def xpath(selector, within: nil)

Returns:
  • (Array) -

Parameters:
  • within (Node, nil) --
  • selector (String) --
def xpath(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      let results = [];
      within ||= document
      let xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      for (let i = 0; i < xpath.snapshotLength; i++) {
        results.push(xpath.snapshotItem(i));
      }
      return results;
    }
  JS
  evaluate_func(expr, selector, within)
end