Class: Charai::Browser

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

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web_socket, debug_protocol: false) ⇒ Browser

Returns a new instance of Browser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/charai/browser.rb', line 7

def initialize(web_socket, debug_protocol: false)
  @web_socket = web_socket
  @debug_protocol = debug_protocol
  @browsing_contexts = {}

  web_socket.on_message do |message|
    handle_received_message_from_websocket(JSON.parse(message))
  end

  bidi_call_async('session.new', {
    capabilities: {
      alwaysMatch: {
        acceptInsecureCerts: false,
        webSocketUrl: true,
      },
    },
  }).value!
  sync_browsing_contexts
  bidi_call_async('session.subscribe', {
    events: %w[browsingContext],
  }) # do no await
end

Class Method Details

.launchObject



3
4
5
# File 'lib/charai/browser.rb', line 3

def self.launch(...)
  BrowserLauncher.new.launch(...)
end

Instance Method Details

#bidi_call_async(method_, params = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/charai/browser.rb', line 41

def bidi_call_async(method_, params = {})
  with_message_id do |message_id|
    @message_results[message_id] = Concurrent::Promises.resolvable_future

    send_message_to_websocket({
      id: message_id,
      method: method_,
      params: params,
    })

    @message_results[message_id]
  end
end

#closeObject



36
37
38
39
# File 'lib/charai/browser.rb', line 36

def close
  bidi_call_async('browser.close').value!
  @web_socket.close
end

#create_browsing_contextObject



30
31
32
33
34
# File 'lib/charai/browser.rb', line 30

def create_browsing_context
  result = bidi_call_async('browsingContext.create', { type: :tab, userContext: :default }).value!
  browsing_context_id = result['context']
  @browsing_contexts[browsing_context_id] ||= BrowsingContext.new(self, browsing_context_id)
end