Class: Puppeteer::Bidi::Core::Browser

Inherits:
EventEmitter show all
Includes:
Disposable::DisposableMixin
Defined in:
lib/puppeteer/bidi/core/browser.rb,
sig/puppeteer/bidi/core/browser.rbs

Overview

Browser represents the browser instance in the core layer It manages user contexts and provides browser-level operations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Disposable::DisposableMixin

#dispose

Methods inherited from EventEmitter

#dispose, #emit, #off, #on, #once, #remove_all_listeners

Constructor Details

#initialize(session) ⇒ Browser

Returns a new instance of Browser.

Parameters:

  • session (Object)


25
26
27
28
29
30
31
32
33
# File 'lib/puppeteer/bidi/core/browser.rb', line 25

def initialize(session)
  super()
  @session = session
  @closed = false
  @reason = nil
  @disposables = Disposable::DisposableStack.new
  @user_contexts = {}
  @shared_workers = {}
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.

Returns:

  • (Object)


23
24
25
# File 'lib/puppeteer/bidi/core/browser.rb', line 23

def session
  @session
end

Class Method Details

.from(session) ⇒ Async::Task[Browser]

Create a browser instance from a session

Parameters:

Returns:



15
16
17
18
19
20
21
# File 'lib/puppeteer/bidi/core/browser.rb', line 15

def self.from(session)
  browser = new(session)
  Async do
    browser.send(:initialize_browser).wait
    browser
  end
end

Instance Method Details

#add_preload_script(function_declaration, **options) ⇒ Async::Task[String]

Add a preload script to the browser

Parameters:

  • function_declaration (String)
  • options (Object)

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puppeteer/bidi/core/browser.rb', line 77

def add_preload_script(function_declaration, **options)
  raise BrowserDisconnectedError, @reason if disconnected?

  params = { functionDeclaration: function_declaration }
  if options[:contexts]
    params[:contexts] = options[:contexts].map(&:id)
  end
  if options.key?(:arguments) || options.key?("arguments")
    params[:arguments] = options[:arguments] || options["arguments"]
  end
  params[:sandbox] = options[:sandbox] if options[:sandbox]

  Async do
    result = @session.async_send_command('script.addPreloadScript', params).wait
    result['script']
  end
end

#closeAsync::Task[void]

Close the browser

Returns:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppeteer/bidi/core/browser.rb', line 61

def close
  Async do
    return if @closed

    begin
      @session.async_send_command('browser.close', {})
    ensure
      dispose_browser('Browser already closed.', closed: true)
    end
  end
end

#closed?Object

Check if the browser is closed

Returns:

  • (Object)


36
37
38
# File 'lib/puppeteer/bidi/core/browser.rb', line 36

def closed?
  @closed
end

#create_user_context(**options) ⇒ Async::Task[UserContext]

Create a new user context

Parameters:

  • options (Object)

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/puppeteer/bidi/core/browser.rb', line 106

def create_user_context(**options)
  raise BrowserDisconnectedError, @reason if disconnected?

  params = {}
  if options[:proxy_server]
    params[:proxy] = {
      proxyType: 'manual',
      httpProxy: options[:proxy_server],
      sslProxy: options[:proxy_server],
      noProxy: options[:proxy_bypass_list]
    }.compact
  end

  Async do
    result = @session.async_send_command('browser.createUserContext', params).wait
    user_context_id = result['userContext']

    create_user_context_object(user_context_id)
  end
end

#create_user_context_object(id) ⇒ Object

Parameters:

  • id (Object)

Returns:

  • (Object)


236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/puppeteer/bidi/core/browser.rb', line 236

def create_user_context_object(id)
  return @user_contexts[id] if @user_contexts[id]

  user_context = UserContext.create(self, id)
  @user_contexts[id] = user_context

  # Listen for user context closure
  user_context.once(:closed) do
    @user_contexts.delete(id)
  end

  user_context
end

#default_user_contextUserContext?

Get the default user context

Returns:



49
50
51
# File 'lib/puppeteer/bidi/core/browser.rb', line 49

def default_user_context
  @user_contexts[UserContext::DEFAULT]
end

#disconnected?Object Also known as: disposed?

Check if the browser is disconnected

Returns:

  • (Object)


41
42
43
# File 'lib/puppeteer/bidi/core/browser.rb', line 41

def disconnected?
  !@reason.nil?
end

#dispose_browser(reason, closed: false) ⇒ Object

Parameters:

  • reason (Object)
  • closed: (Object) (defaults to: false)

Returns:

  • (Object)


250
251
252
253
254
# File 'lib/puppeteer/bidi/core/browser.rb', line 250

def dispose_browser(reason, closed: false)
  @closed = closed
  @reason = reason
  dispose
end

#get_client_window_info(window_id) ⇒ Async::Task[Hash[String, untyped]]

Get browser client window info by window id

Parameters:

  • window_id (String)

Returns:



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/puppeteer/bidi/core/browser.rb', line 146

def get_client_window_info(window_id)
  raise BrowserDisconnectedError, @reason if disconnected?

  Async do
    result = @session.async_send_command('browser.getClientWindows', {}).wait
    windows = result['clientWindows'] || []
    window = windows.find { |item| item['clientWindow'] == window_id }
    raise Error, 'Window not found' unless window

    window
  end
end

#initialize_browserObject

Returns:

  • (Object)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/puppeteer/bidi/core/browser.rb', line 171

def initialize_browser
  Async do
    # Listen for session end
    @session.on(:ended) do |reason|
      dispose_browser(reason)
    end

    # Listen for shared worker creation
    @session.on('script.realmCreated') do |info|
      next unless info['type'] == 'shared-worker'
      # Create SharedWorkerRealm when implemented
      # @shared_workers[info['realm']] = SharedWorkerRealm.from(self, info['realm'], info['origin'])
    end

    # Sync existing user contexts and browsing contexts
    sync_user_contexts.wait
    sync_browsing_contexts.wait
  end
end

#perform_disposeObject

Returns:

  • (Object)


161
162
163
164
165
166
167
# File 'lib/puppeteer/bidi/core/browser.rb', line 161

def perform_dispose
  @reason ||= 'Browser was disconnected, probably because the session ended.'
  emit(:closed, @reason) if @closed
  emit(:disconnected, @reason)
  @disposables.dispose
  super
end

#process_contexts(contexts, context_ids) ⇒ Object

Parameters:

  • contexts (Object)
  • context_ids (Object)

Returns:

  • (Object)


224
225
226
227
228
229
230
231
232
233
234
# File 'lib/puppeteer/bidi/core/browser.rb', line 224

def process_contexts(contexts, context_ids)
  contexts.each do |info|
    # Emit context created event if not already tracked
    unless context_ids.include?(info['context'])
      @session.emit('browsingContext.contextCreated', info)
    end

    # Process children recursively
    process_contexts(info['children'], context_ids) if info['children']
  end
end

#remove_intercept(intercept) ⇒ Async::Task[untyped]

Remove a network intercept

Parameters:

  • intercept (String)

Returns:



130
131
132
133
# File 'lib/puppeteer/bidi/core/browser.rb', line 130

def remove_intercept(intercept)
  raise BrowserDisconnectedError, @reason if disconnected?
  @session.async_send_command('network.removeIntercept', { intercept: intercept })
end

#remove_preload_script(script) ⇒ Async::Task[untyped]

Remove a preload script

Parameters:

  • script (String)

Returns:



98
99
100
101
# File 'lib/puppeteer/bidi/core/browser.rb', line 98

def remove_preload_script(script)
  raise BrowserDisconnectedError, @reason if disconnected?
  @session.async_send_command('script.removePreloadScript', { script: script })
end

#set_client_window_state(params) ⇒ Async::Task[void]

Set browser window state/position/size

Parameters:

  • params (Hash[String | Symbol, untyped])

Returns:



138
139
140
141
# File 'lib/puppeteer/bidi/core/browser.rb', line 138

def set_client_window_state(params)
  raise BrowserDisconnectedError, @reason if disconnected?
  @session.async_send_command('browser.setClientWindowState', params)
end

#sync_browsing_contextsObject

Returns:

  • (Object)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/puppeteer/bidi/core/browser.rb', line 202

def sync_browsing_contexts
  Async do
    # Get all browsing contexts
    result = @session.async_send_command('browsingContext.getTree', {}).wait
    contexts = result['contexts']

    # Track context IDs for detecting created/destroyed contexts during sync
    context_ids = []

    # Setup temporary listener for context creation during sync
    temp_listener = @session.on('browsingContext.contextCreated') do |info|
      context_ids << info['context']
    end

    # Process all contexts (including nested ones)
    process_contexts(contexts, context_ids)

    # Remove temporary listener
    # @session.off('browsingContext.contextCreated', &temp_listener)
  end
end

#sync_user_contextsObject

Returns:

  • (Object)


191
192
193
194
195
196
197
198
199
200
# File 'lib/puppeteer/bidi/core/browser.rb', line 191

def sync_user_contexts
  Async do
    result = @session.async_send_command('browser.getUserContexts', {}).wait
    user_contexts = result['userContexts']

    user_contexts.each do |context_info|
      create_user_context_object(context_info['userContext'])
    end
  end
end

#user_contextsArray[UserContext]

Get all user contexts

Returns:



55
56
57
# File 'lib/puppeteer/bidi/core/browser.rb', line 55

def user_contexts
  @user_contexts.values
end