Class: Puppeteer::Bidi::Core::Browser
- Inherits:
-
EventEmitter
- Object
- EventEmitter
- Puppeteer::Bidi::Core::Browser
- 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
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Class Method Summary collapse
-
.from(session) ⇒ Async::Task[Browser]
Create a browser instance from a session.
Instance Method Summary collapse
-
#add_preload_script(function_declaration, **options) ⇒ Async::Task[String]
Add a preload script to the browser.
-
#close ⇒ Async::Task[void]
Close the browser.
-
#closed? ⇒ Object
Check if the browser is closed.
-
#create_user_context(**options) ⇒ Async::Task[UserContext]
Create a new user context.
- #create_user_context_object(id) ⇒ Object
-
#default_user_context ⇒ UserContext?
Get the default user context.
-
#disconnected? ⇒ Object
(also: #disposed?)
Check if the browser is disconnected.
- #dispose_browser(reason, closed: false) ⇒ Object
-
#get_client_window_info(window_id) ⇒ Async::Task[Hash[String, untyped]]
Get browser client window info by window id.
-
#initialize(session) ⇒ Browser
constructor
A new instance of Browser.
- #initialize_browser ⇒ Object
- #perform_dispose ⇒ Object
- #process_contexts(contexts, context_ids) ⇒ Object
-
#remove_intercept(intercept) ⇒ Async::Task[untyped]
Remove a network intercept.
-
#remove_preload_script(script) ⇒ Async::Task[untyped]
Remove a preload script.
-
#set_client_window_state(params) ⇒ Async::Task[void]
Set browser window state/position/size.
- #sync_browsing_contexts ⇒ Object
- #sync_user_contexts ⇒ Object
-
#user_contexts ⇒ Array[UserContext]
Get all user contexts.
Methods included from Disposable::DisposableMixin
Methods inherited from EventEmitter
#dispose, #emit, #off, #on, #once, #remove_all_listeners
Constructor Details
#initialize(session) ⇒ Browser
Returns a new instance of Browser.
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
#session ⇒ Object (readonly)
Returns the value of attribute session.
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
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
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, **) raise BrowserDisconnectedError, @reason if disconnected? params = { functionDeclaration: function_declaration } if [:contexts] params[:contexts] = [:contexts].map(&:id) end if .key?(:arguments) || .key?("arguments") params[:arguments] = [:arguments] || ["arguments"] end params[:sandbox] = [:sandbox] if [:sandbox] Async do result = @session.async_send_command('script.addPreloadScript', params).wait result['script'] end end |
#close ⇒ Async::Task[void]
Close the browser
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
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
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(**) raise BrowserDisconnectedError, @reason if disconnected? params = {} if [:proxy_server] params[:proxy] = { proxyType: 'manual', httpProxy: [:proxy_server], sslProxy: [:proxy_server], noProxy: [: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
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_context ⇒ UserContext?
Get the default user context
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
41 42 43 |
# File 'lib/puppeteer/bidi/core/browser.rb', line 41 def disconnected? !@reason.nil? end |
#dispose_browser(reason, closed: false) ⇒ 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
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_browser ⇒ 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_dispose ⇒ 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
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
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
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
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_contexts ⇒ 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_contexts ⇒ 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_contexts ⇒ Array[UserContext]
Get all user contexts
55 56 57 |
# File 'lib/puppeteer/bidi/core/browser.rb', line 55 def user_contexts @user_contexts.values end |