Module: Ferrum::Frame::DOM
- Included in:
- Ferrum::Frame
- Defined in:
- lib/ferrum/frame/dom.rb
Constant Summary collapse
- 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
- LINK_TAG =
<<~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
-
#add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript") ⇒ Object
Adds a ‘<script>` tag to the document.
-
#add_style_tag(url: nil, path: nil, content: nil) ⇒ Object
Adds a ‘<style>` tag to the document.
-
#at_css(selector, within: nil) ⇒ Node?
Finds a node by using a CSS path selector.
-
#at_xpath(selector, within: nil) ⇒ Node?
Finds a node by using a XPath selector.
-
#body ⇒ String
Returns current page’s html.
-
#css(selector, within: nil) ⇒ Array<Node>
Finds nodes by using a CSS path selector.
-
#current_title ⇒ String
Returns current top window title.
-
#current_url ⇒ String
Returns current top window ‘location href`.
- #doctype ⇒ Object
-
#frame_element ⇒ Node?
Returns the element in which the window is embedded.
-
#xpath(selector, within: nil) ⇒ Array<Node>
Finds nodes by using a XPath selector.
Instance Method Details
#add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript") ⇒ Object
Adds a ‘<script>` tag to the document.
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/ferrum/frame/dom.rb', line 243 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
Adds a ‘<style>` tag to the document.
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/ferrum/frame/dom.rb', line 269 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 |
#at_css(selector, within: nil) ⇒ Node?
Finds a node by using a CSS path selector.
218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ferrum/frame/dom.rb', line 218 def at_css(selector, within: nil) expr = <<~JS function(selector, within) { within ||= document return within.querySelector(selector); } JS evaluate_func(expr, selector, within) end |
#at_xpath(selector, within: nil) ⇒ Node?
Finds a node by using a XPath selector.
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ferrum/frame/dom.rb', line 164 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 |
#body ⇒ String
Returns current page’s html.
94 95 96 |
# File 'lib/ferrum/frame/dom.rb', line 94 def body evaluate("document.documentElement?.outerHTML") || "" end |
#css(selector, within: nil) ⇒ Array<Node>
Finds nodes by using a CSS path selector.
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/ferrum/frame/dom.rb', line 191 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 |
#current_title ⇒ String
Returns current top window title.
76 77 78 |
# File 'lib/ferrum/frame/dom.rb', line 76 def current_title evaluate("window.top.document.title") end |
#current_url ⇒ String
Returns current top window ‘location href`.
62 63 64 |
# File 'lib/ferrum/frame/dom.rb', line 62 def current_url evaluate("window.top.location.href") end |
#doctype ⇒ Object
80 81 82 |
# File 'lib/ferrum/frame/dom.rb', line 80 def doctype evaluate("document.doctype && new XMLSerializer().serializeToString(document.doctype)") end |
#frame_element ⇒ Node?
Returns the element in which the window is embedded.
110 111 112 |
# File 'lib/ferrum/frame/dom.rb', line 110 def frame_element evaluate("window.frameElement") end |
#xpath(selector, within: nil) ⇒ Array<Node>
Finds nodes by using a XPath selector.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ferrum/frame/dom.rb', line 130 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 |