Class: WebkitRemote::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/webkit_remote/browser.rb

Overview

The master connection to the remote debugging server in a Webkit process.

Defined Under Namespace

Classes: Tab

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Browser

Sets up a debugging connection to a Webkit process.

Parameters:

  • opts (Hash) (defaults to: {})

    info on the browser to connect to

Options Hash (opts):

  • host (String)

    the hostname / IP address of the Webkit remote debugging server

  • port (Integer)

    the port that the Webkit remote debugging server listens to

  • process (WebkitRemote::Process)

    a process on the local machine to connect to; the process will automatically be stopped when the debugging connection is closed; the host and port will be configured automatically

  • stop_process (Boolean)

    if true, the WebkitRemote::Process passed to the constructor will be automatically stopped

Raises:

  • (SystemCallError)

    if the connection could not be established; this most likely means that there is no remote debugging server at the given host / port



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/webkit_remote/browser.rb', line 24

def initialize(opts = {})
  if opts[:process]
    @process = opts[:process]
    @stop_process = opts.fetch :stop_process, false
    @host = 'localhost'
    @port = process.port
  else
    @process = nil
    @stop_process = false
    @host = opts[:host] || 'localhost'
    @port = opts[:port] || 9292
  end
  @closed = false

  @http = Net::HTTP.start @host, @port
end

Instance Attribute Details

#closedBoolean (readonly) Also known as: closed?

Returns if true, the connection to the remote debugging server has been closed, and this instance is mostly useless.

Returns:

  • (Boolean)

    if true, the connection to the remote debugging server has been closed, and this instance is mostly useless



113
114
115
# File 'lib/webkit_remote/browser.rb', line 113

def closed
  @closed
end

#hostString (readonly)

Returns hostname or IP of the Webkit remote debugging server.

Returns:

  • (String)

    hostname or IP of the Webkit remote debugging server



106
107
108
# File 'lib/webkit_remote/browser.rb', line 106

def host
  @host
end

#portInteger (readonly)

Returns port that the Webkit remote debugging server listens on.

Returns:

  • (Integer)

    port that the Webkit remote debugging server listens on



109
110
111
# File 'lib/webkit_remote/browser.rb', line 109

def port
  @port
end

#processWebkitRemote::Process? (readonly)

Returns Process instance passed to this connection’s constructor.

Returns:



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

def process
  @process
end

#stop_processBoolean Also known as: stop_process?

Returns if true, a WebkitRemote::Process will be stopped when this browser connection is closed.

Returns:

  • (Boolean)

    if true, a WebkitRemote::Process will be stopped when this browser connection is closed



77
78
79
# File 'lib/webkit_remote/browser.rb', line 77

def stop_process
  @stop_process
end

Instance Method Details

#closeWebkitRemote::Browser

Closes the connection the browser.

If the Browser instance was given a WebkitRemote::Process, the process will also be stopped. This instance becomes useless after closing.

Returns:



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

def close
  return self if @closed
  @closed = true
  @http.finish
  @http = nil
  @process.stop if @stop_process
  self
end

#finalizeObject

Clean up when garbage collected.



117
118
119
# File 'lib/webkit_remote/browser.rb', line 117

def finalize
  close unless @closed
end

#tabsArray<WebkitRemote::Browser::Tab>

Retrieves the tabs that are currently open in the browser.

These tabs can be used to start debugging.

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/webkit_remote/browser.rb', line 61

def tabs
  http_response = @http.request Net::HTTP::Get.new('/json')
  tabs = JSON.parse(http_response.body).map do |json_tab|
    title = json_tab['title']
    url = json_tab['url']
    debug_url = json_tab['webSocketDebuggerUrl']
    Tab.new self, debug_url, title: title, url: url
  end
  # HACK(pwnall): work around the nasty Google Hangouts integration
  tabs.select do |tab|
    tab.url != 'chrome-extension://nkeimhogjdpnpccoofpliimaahmaaome/background.html'
  end
end