Class: Obxcura::Browser
- Inherits:
-
Object
- Object
- Obxcura::Browser
- Extended by:
- Forwardable
- Defined in:
- lib/obxcura/browser.rb
Overview
Constant Summary collapse
- DEFAULT_HOST =
Returns default host for
obscura serve. "127.0.0.1"- DEFAULT_PORT =
Returns default CDP port for
obscura serve. 9222- BLANK_PAGE =
The only URL a target may safely be created at; see #create_page.
"about:blank"
Instance Attribute Summary collapse
- #client ⇒ Obxcura::Client, ... readonly
- #host ⇒ Obxcura::Client, ... readonly
- #pages ⇒ Obxcura::Client, ... readonly
- #port ⇒ Obxcura::Client, ... readonly
Instance Method Summary collapse
-
#clear_cookies ⇒ void
Drop every cookie held on this connection.
-
#close ⇒ void
(also: #quit)
Close every page, then drop the connection.
-
#create_page(url = BLANK_PAGE) ⇒ Obxcura::Page
Open a fresh page (a CDP target) and attach to it.
-
#go_to(url) ⇒ Obxcura::Page
(also: #goto)
Open a page and navigate to
urlin one call — the expressive name for #create_page with a URL. -
#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, timeout: Client::DEFAULT_TIMEOUT) ⇒ Browser
constructor
Connect to a running
obscura serve. -
#remove_page(page) ⇒ Obxcura::Page?
Stop tracking a page.
-
#targets ⇒ Array<Hash>
Every target the browser knows about (pages, workers, ...).
-
#version ⇒ Hash
The browser's
/json/versionmetadata (product, protocol, ws endpoint).
Constructor Details
#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, timeout: Client::DEFAULT_TIMEOUT) ⇒ Browser
Connect to a running obscura serve.
40 41 42 43 44 45 46 |
# File 'lib/obxcura/browser.rb', line 40 def initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, timeout: Client::DEFAULT_TIMEOUT) @host = host @port = port @timeout = timeout @pages = [] @client = Client.new(browser_ws_url, timeout: @timeout) end |
Instance Attribute Details
#client ⇒ Obxcura::Client, ... (readonly)
30 31 32 |
# File 'lib/obxcura/browser.rb', line 30 def client @client end |
#host ⇒ Obxcura::Client, ... (readonly)
30 31 32 |
# File 'lib/obxcura/browser.rb', line 30 def host @host end |
#pages ⇒ Obxcura::Client, ... (readonly)
30 31 32 |
# File 'lib/obxcura/browser.rb', line 30 def pages @pages end |
#port ⇒ Obxcura::Client, ... (readonly)
30 31 32 |
# File 'lib/obxcura/browser.rb', line 30 def port @port end |
Instance Method Details
#clear_cookies ⇒ void
This method returns an undefined value.
Drop every cookie held on this connection.
Since Obscura 0.1.11 each connection owns its own browser context, so cookies
no longer leak between Browser instances and this is only about resetting
state within one connection — between logical sessions on the same socket,
say. obscura serve is still long-lived and #quit only drops the socket, so
a fresh Browser is the other way to get a clean jar.
107 108 109 110 |
# File 'lib/obxcura/browser.rb', line 107 def command("Network.clearBrowserCookies") nil end |
#close ⇒ void Also known as: quit
This method returns an undefined value.
Close every page, then drop the connection. Aliased as quit.
123 124 125 126 |
# File 'lib/obxcura/browser.rb', line 123 def close @pages.dup.each(&:close) client.close end |
#create_page(url = BLANK_PAGE) ⇒ Obxcura::Page
Open a fresh page (a CDP target) and attach to it. With a url, the page
is navigated there and the call blocks until its load event fires.
The target is always created blank and then navigated, never opened at
url directly. Handing a URL to Target.createTarget works exactly once:
the second such call on a connection kills obscura serve outright —
every command after it fails with connection closed: end of file reached, and the process is gone, not just the socket. Measured on 0.2.0
and deterministic, whatever the URLs are; two blank targets are fine, and
so is any amount of navigation. Since the parameter cannot be honoured
literally without handing callers a way to kill the browser, it is honoured
by navigating.
63 64 65 66 67 68 69 70 71 |
# File 'lib/obxcura/browser.rb', line 63 def create_page(url = BLANK_PAGE) target_id = command("Target.createTarget", { url: BLANK_PAGE })["targetId"] session_id = command("Target.attachToTarget", { targetId: target_id, flatten: true })["sessionId"] page = Page.new(self, target_id: target_id, session_id: session_id) @pages << page page.goto(url) unless url == BLANK_PAGE page end |
#go_to(url) ⇒ Obxcura::Page Also known as: goto
Open a page and navigate to url in one call — the expressive name for
#create_page with a URL.
78 79 80 |
# File 'lib/obxcura/browser.rb', line 78 def go_to(url) create_page(url) end |
#remove_page(page) ⇒ Obxcura::Page?
Stop tracking a page. Called by Page#close.
116 117 118 |
# File 'lib/obxcura/browser.rb', line 116 def remove_page(page) @pages.delete(page) end |
#targets ⇒ Array<Hash>
Every target the browser knows about (pages, workers, ...).
86 87 88 |
# File 'lib/obxcura/browser.rb', line 86 def targets command("Target.getTargets")["targetInfos"] end |
#version ⇒ Hash
The browser's /json/version metadata (product, protocol, ws endpoint).
93 94 95 96 |
# File 'lib/obxcura/browser.rb', line 93 def version uri = URI("http://#{@host}:#{@port}/json/version") JSON.parse(Net::HTTP.get(uri)) end |