Module: Obxcura::Frame::DOM

Included in:
Obxcura::Frame
Defined in:
lib/obxcura/frame/dom.rb

Overview

DOM: high-level reads of the rendered page, all built on Runtime.

Selector queries return live Nodes. Obscura serializes a DOM node over returnByValue as an internal stub carrying an _nid — useless on its own, but the _nid resolves (DOM.resolveNode) to a real remote object handle we wrap in a Node and drive with Runtime#call_on.

Instance Method Summary collapse

Instance Method Details

#at_css(selector) ⇒ Obxcura::Node?

First element matching a CSS selector.

Parameters:

  • selector (String)

    a CSS selector.

Returns:



36
37
38
# File 'lib/obxcura/frame/dom.rb', line 36

def at_css(selector)
  to_node(evaluate_func("function(s) { return document.querySelector(s); }", selector))
end

#bodyString Also known as: html

The live, post-JS HTML. Retrieved in chunks (see Runtime#read_string) because a full page's outerHTML routinely exceeds Obscura's message limit. Aliased as html.

Returns:

  • (String)

    the rendered document's outer HTML.



27
28
29
# File 'lib/obxcura/frame/dom.rb', line 27

def body
  read_string("document.documentElement.outerHTML")
end

#css(selector) ⇒ Array<Obxcura::Node>

All elements matching a CSS selector.

Parameters:

  • selector (String)

    a CSS selector.

Returns:



44
45
46
47
# File 'lib/obxcura/frame/dom.rb', line 44

def css(selector)
  Array(evaluate_func("function(s) { return Array.from(document.querySelectorAll(s)); }", selector))
    .filter_map { |stub| to_node(stub) }
end

#current_urlString

Returns the current top-window URL.

Returns:

  • (String)

    the current top-window URL.



13
14
15
# File 'lib/obxcura/frame/dom.rb', line 13

def current_url
  evaluate("window.location.href")
end

#titleString

Returns the current document title.

Returns:

  • (String)

    the current document title.



18
19
20
# File 'lib/obxcura/frame/dom.rb', line 18

def title
  evaluate("document.title")
end

#to_node(stub) ⇒ Obxcura::Node?

Resolve Obscura's serialized node stub ("_nid"=>N,...) into a Node. Public so Node#at_css can reuse the same resolution path.

Parameters:

  • stub (Hash, nil)

    the serialized node from a querySelector call.

Returns:

  • (Obxcura::Node, nil)

    a live node handle, or nil for a blank match.



54
55
56
57
58
59
60
# File 'lib/obxcura/frame/dom.rb', line 54

def to_node(stub)
  return nil unless stub.is_a?(Hash) && stub["_nid"]

  object_id = command("DOM.resolveNode", { nodeId: stub["_nid"] }).dig("object", "objectId")

  Node.new(self, object_id)
end