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
-
#at_css(selector) ⇒ Obxcura::Node?
First element matching a CSS selector.
-
#body ⇒ String
(also: #html)
The live, post-JS HTML.
-
#css(selector) ⇒ Array<Obxcura::Node>
All elements matching a CSS selector.
-
#current_url ⇒ String
The current top-window URL.
-
#title ⇒ String
The current document title.
-
#to_node(stub) ⇒ Obxcura::Node?
Resolve Obscura's serialized node stub ("_nid"=>N,...) into a Node.
Instance Method Details
#at_css(selector) ⇒ Obxcura::Node?
First element matching a CSS selector.
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 |
#body ⇒ String 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.
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.
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_url ⇒ String
Returns the current top-window URL.
13 14 15 |
# File 'lib/obxcura/frame/dom.rb', line 13 def current_url evaluate("window.location.href") end |
#title ⇒ String
Returns 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.
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 |