Class: Obxcura::Browser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/obxcura/browser.rb

Overview

The entry point. Owns one Client (one connection) and the pages opened on it.

browser = Obxcura::Browser.new
page    = browser.create_page
browser.quit

Assumes a running obscura serve (default 127.0.0.1:9222). Pass host:/port: to point elsewhere.

Constant Summary collapse

DEFAULT_HOST =

Returns default host for obscura serve.

Returns:

  • (String)

    default host for obscura serve.

"127.0.0.1"
DEFAULT_PORT =

Returns default CDP port for obscura serve.

Returns:

  • (Integer)

    default CDP port for obscura serve.

9222

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, timeout: Client::DEFAULT_TIMEOUT) ⇒ Browser

Connect to a running obscura serve.

Parameters:

  • host (String) (defaults to: DEFAULT_HOST)

    host the browser listens on.

  • port (Integer) (defaults to: DEFAULT_PORT)

    CDP port the browser listens on.

  • timeout (Integer) (defaults to: Client::DEFAULT_TIMEOUT)

    default seconds to wait for CDP replies.

Raises:



36
37
38
39
40
41
42
# File 'lib/obxcura/browser.rb', line 36

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

#clientObxcura::Client, ... (readonly)

Returns:

  • (Obxcura::Client)

    the underlying CDP transport.

  • (Array<Obxcura::Page>)

    the pages currently open.

  • (String)

    the browser host.

  • (Integer)

    the browser port.



26
27
28
# File 'lib/obxcura/browser.rb', line 26

def client
  @client
end

#hostObxcura::Client, ... (readonly)

Returns:

  • (Obxcura::Client)

    the underlying CDP transport.

  • (Array<Obxcura::Page>)

    the pages currently open.

  • (String)

    the browser host.

  • (Integer)

    the browser port.



26
27
28
# File 'lib/obxcura/browser.rb', line 26

def host
  @host
end

#pagesObxcura::Client, ... (readonly)

Returns:

  • (Obxcura::Client)

    the underlying CDP transport.

  • (Array<Obxcura::Page>)

    the pages currently open.

  • (String)

    the browser host.

  • (Integer)

    the browser port.



26
27
28
# File 'lib/obxcura/browser.rb', line 26

def pages
  @pages
end

#portObxcura::Client, ... (readonly)

Returns:

  • (Obxcura::Client)

    the underlying CDP transport.

  • (Array<Obxcura::Page>)

    the pages currently open.

  • (String)

    the browser host.

  • (Integer)

    the browser port.



26
27
28
# File 'lib/obxcura/browser.rb', line 26

def port
  @port
end

Instance Method Details

#clear_cookiesvoid

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.



94
95
96
97
# File 'lib/obxcura/browser.rb', line 94

def clear_cookies
  command("Network.clearBrowserCookies")
  nil
end

#closevoid Also known as: quit

This method returns an undefined value.

Close every page, then drop the connection. Aliased as quit.



110
111
112
113
# File 'lib/obxcura/browser.rb', line 110

def close
  @pages.dup.each(&:close)
  client.close
end

#create_page(url = "about:blank") ⇒ Obxcura::Page

Open a fresh page (a CDP target) and attach to it.

Parameters:

  • url (String) (defaults to: "about:blank")

    URL to open the target at (defaults to a blank page).

Returns:



48
49
50
51
52
53
54
55
# File 'lib/obxcura/browser.rb', line 48

def create_page(url = "about:blank")
  target_id = command("Target.createTarget", { url: url })["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
end

#go_to(url) ⇒ Obxcura::Page Also known as: goto

Open a page and navigate to url in one call.

Navigates from a blank target rather than passing the URL straight to Target.createTarget: creating two URL-loaded targets and then evaluating crashes obscura serve (connection closed: end of file reached).

Parameters:

  • url (String)

    URL to navigate to.

Returns:



65
66
67
# File 'lib/obxcura/browser.rb', line 65

def go_to(url)
  create_page.goto(url)
end

#remove_page(page) ⇒ Obxcura::Page?

Stop tracking a page. Called by Page#close.

Parameters:

Returns:



103
104
105
# File 'lib/obxcura/browser.rb', line 103

def remove_page(page)
  @pages.delete(page)
end

#targetsArray<Hash>

Every target the browser knows about (pages, workers, ...).

Returns:

  • (Array<Hash>)

    raw CDP TargetInfo hashes.



73
74
75
# File 'lib/obxcura/browser.rb', line 73

def targets
  command("Target.getTargets")["targetInfos"]
end

#versionHash

The browser's /json/version metadata (product, protocol, ws endpoint).

Returns:

  • (Hash)

    the decoded JSON version document.



80
81
82
83
# File 'lib/obxcura/browser.rb', line 80

def version
  uri = URI("http://#{@host}:#{@port}/json/version")
  JSON.parse(Net::HTTP.get(uri))
end