Class: Puppeteer::Bidi::BrowserContext
- Inherits:
-
Object
- Object
- Puppeteer::Bidi::BrowserContext
- Defined in:
- lib/puppeteer/bidi/browser_context.rb,
sig/puppeteer/bidi/browser_context.rbs
Overview
BrowserContext represents an isolated browsing session This is a high-level wrapper around Core::UserContext
Constant Summary collapse
- WEB_PERMISSION_TO_PROTOCOL_PERMISSION =
Maps web permission names to protocol permission names Based on Puppeteer's WEB_PERMISSION_TO_PROTOCOL_PERMISSION
{ 'accelerometer' => 'sensors', 'ambient-light-sensor' => 'sensors', 'background-sync' => 'backgroundSync', 'camera' => 'videoCapture', 'clipboard-read' => 'clipboardReadWrite', 'clipboard-sanitized-write' => 'clipboardSanitizedWrite', 'clipboard-write' => 'clipboardReadWrite', 'geolocation' => 'geolocation', 'gyroscope' => 'sensors', 'idle-detection' => 'idleDetection', 'keyboard-lock' => 'keyboardLock', 'magnetometer' => 'sensors', 'microphone' => 'audioCapture', 'midi' => 'midi', 'midi-sysex' => 'midiSysex', 'notifications' => 'notifications', 'payment-handler' => 'paymentHandler', 'persistent-storage' => 'durableStorage', 'pointer-lock' => 'pointerLock' }.freeze
Instance Attribute Summary collapse
- #browser ⇒ Browser readonly
- #user_context ⇒ Core::UserContext readonly
Instance Method Summary collapse
-
#clear_permission_overrides ⇒ void
Clear permissions set through #override_permissions.
-
#close ⇒ void
Close the browser context.
-
#closed? ⇒ Boolean
Check if context is closed.
- #cookie_matches_filter?(cookie, raw_filter) ⇒ Boolean
-
#cookies ⇒ Array[Hash[String, untyped]]
Get all cookies in this context.
- #debug_error(error) ⇒ Object
-
#delete_cookie(*cookies, **cookie) ⇒ void
Delete cookies in this context.
-
#delete_matching_cookies(*filters, **filter) ⇒ void
Delete cookies matching the provided filters.
-
#initialize(browser, user_context) ⇒ BrowserContext
constructor
A new instance of BrowserContext.
-
#new_page(background: nil, type: nil, window_bounds: nil) ⇒ Page
Create a new page (tab/window).
-
#off(event, &block) ⇒ void
Remove an event listener.
-
#on(event) {|arg0| ... } ⇒ void
Register an event listener.
-
#once(event) {|arg0| ... } ⇒ void
Register a one-time event listener.
-
#override_permissions(origin, permissions) ⇒ void
Override permissions for an origin.
-
#page_for(browsing_context) ⇒ Page
Get or create a Page for the given browsing context.
-
#pages ⇒ Array[Page]
Get all pages in this context.
-
#set_cookie(*cookies, **cookie) ⇒ void
Set cookies in this context.
-
#set_permission(origin, *permissions) ⇒ void
Set permission states for one or more permission descriptors.
- #target_for_frame(frame) ⇒ FrameTarget
-
#targets ⇒ Array[PageTarget | FrameTarget]
Get all known targets in this browser context.
-
#wait_for_target(timeout: nil) {|arg0| ... } ⇒ PageTarget, FrameTarget
Wait until a target in this context satisfies the predicate.
Constructor Details
#initialize(browser, user_context) ⇒ BrowserContext
Returns a new instance of BrowserContext.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 41 def initialize(browser, user_context) @browser = browser @user_context = user_context @pages = {} @frame_targets = {} @overrides = [] @emitter = Core::EventEmitter.new @user_context.browsing_contexts.each { |browsing_context| page_for(browsing_context) } @user_context.on(:browsingcontext) { |browsing_context| page_for(browsing_context) } @user_context.once(:closed) { @emitter.dispose } end |
Instance Attribute Details
#browser ⇒ Browser (readonly)
36 37 38 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 36 def browser @browser end |
#user_context ⇒ Core::UserContext (readonly)
35 36 37 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 35 def user_context @user_context end |
Instance Method Details
#clear_permission_overrides ⇒ void
This method returns an undefined value.
Clear permissions set through #override_permissions.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 317 def tasks = @overrides.map do |override| lambda do begin @user_context.(override[:origin], { name: override[:permission] }, 'prompt').wait rescue StandardError => error debug_error(error) end end end @overrides = [] AsyncUtils.await_promise_all(*tasks) unless tasks.empty? end |
#close ⇒ void
This method returns an undefined value.
Close the browser context
334 335 336 337 338 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 334 def close return if closed? @user_context.remove.wait end |
#closed? ⇒ Boolean
Check if context is closed
342 343 344 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 342 def closed? @user_context.disposed? end |
#cookie_matches_filter?(cookie, raw_filter) ⇒ Boolean
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 361 def (, raw_filter) filter = CookieUtils.(raw_filter) return false unless filter["name"] == ["name"] return true if filter.key?("domain") && filter["domain"] == ["domain"] return true if filter.key?("path") && filter["path"] == ["path"] if filter.key?("partitionKey") && .key?("partitionKey") = ["partitionKey"] unless .is_a?(Hash) raise Error, "Unexpected string partition key" end filter_partition = filter["partitionKey"] if filter_partition.is_a?(String) return true if filter_partition == ["sourceOrigin"] elsif filter_partition.is_a?(Hash) normalized_partition = CookieUtils.(filter_partition) return true if normalized_partition["sourceOrigin"] == ["sourceOrigin"] end end if filter.key?("url") url = URI.parse(filter["url"]) url_path = url.path url_path = "/" if url_path.nil? || url_path.empty? return true if url.host == ["domain"] && url_path == ["path"] end true end |
#cookies ⇒ Array[Hash[String, untyped]]
Get all cookies in this context.
137 138 139 140 141 142 143 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 137 def return [] if closed? @user_context..wait.map do || CookieUtils.(, return_composite_partition_key: true) end end |
#debug_error(error) ⇒ Object
393 394 395 396 397 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 393 def debug_error(error) return unless ENV['DEBUG_BIDI_COMMAND'] warn(error.) end |
#delete_cookie(*cookies, **cookie) ⇒ void
This method returns an undefined value.
Delete cookies in this context.
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 195 def (*, **) = .dup << unless .empty? delete_candidates = .map do || = CookieUtils.() .merge("expires" => 1) end (*delete_candidates) end |
#delete_matching_cookies(*filters, **filter) ⇒ void
This method returns an undefined value.
Delete cookies matching the provided filters.
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 211 def (*filters, **filter) filters = filters.dup filters << filter unless filter.empty? = .select do || filters.any? { |filter_entry| (, filter_entry) } end (*) end |
#new_page(background: nil, type: nil, window_bounds: nil) ⇒ Page
Create a new page (tab/window)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 83 def new_page(background: nil, type: nil, window_bounds: nil) create_type = type.to_s == 'window' ? 'window' : 'tab' browsing_context = @user_context.create_browsing_context(create_type, background: background) page = page_for(browsing_context) if create_type == 'window' && window_bounds begin @browser.set_window_bounds(browsing_context.window_id, window_bounds) rescue StandardError => error debug_error(error) end end page end |
#off(event, &block) ⇒ void
This method returns an undefined value.
Remove an event listener
74 75 76 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 74 def off(event, &block) @emitter.off(event, &block) end |
#on(event) {|arg0| ... } ⇒ void
This method returns an undefined value.
Register an event listener
58 59 60 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 58 def on(event, &block) @emitter.on(event, &block) end |
#once(event) {|arg0| ... } ⇒ void
This method returns an undefined value.
Register a one-time event listener
66 67 68 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 66 def once(event, &block) @emitter.once(event, &block) end |
#override_permissions(origin, permissions) ⇒ void
This method returns an undefined value.
Override permissions for an origin
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 260 def (origin, ) # Validate all permissions are known = .map do || = WEB_PERMISSION_TO_PROTOCOL_PERMISSION[.to_s] raise ArgumentError, "Unknown permission: #{}" unless .to_s end.to_set # Set each permission WEB_PERMISSION_TO_PROTOCOL_PERMISSION.each_key do || state = .include?() ? 'granted' : 'denied' begin @user_context.(origin, { name: }, state).wait @overrides << { origin: origin, permission: } rescue StandardError # Ignore errors for denied permissions (some may not be supported) raise if .include?() end end end |
#page_for(browsing_context) ⇒ Page
Get or create a Page for the given browsing context
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 225 def page_for(browsing_context) @pages[browsing_context.id] ||= begin page = Page.new(self, browsing_context) page_target = page.target page.on(:frameattached) do |frame| @emitter.emit(:targetcreated, target_for_frame(frame)) end page.on(:framenavigated) do |frame| target = frame.parent_frame ? target_for_frame(frame) : page_target @emitter.emit(:targetchanged, target) end page.on(:framedetached) do |frame| next unless frame.parent_frame target = @frame_targets.delete(frame.browsing_context.id) @emitter.emit(:targetdestroyed, target) if target end browsing_context.once(:closed) do @pages.delete(browsing_context.id) @emitter.emit(:targetdestroyed, page_target) end @emitter.emit(:targetcreated, page_target) page end end |
#pages ⇒ Array[Page]
Get all pages in this context
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 101 def pages return [] if closed? # Return pages for all currently-known top-level browsing contexts. # Browsing contexts are synchronized from `browsingContext.getTree` during browser/session # initialization, so this allows `Puppeteer::Bidi.connect` to expose existing pages without # requiring an explicit enumeration via `wait_for_target`. @user_context.browsing_contexts .reject(&:disposed?) .map { |browsing_context| page_for(browsing_context) } end |
#set_cookie(*cookies, **cookie) ⇒ void
This method returns an undefined value.
Set cookies in this context.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 149 def (*, **) = .dup << unless .empty? tasks = .map do || = CookieUtils.() domain = ["domain"] if domain.nil? raise ArgumentError, "At least one of the url and domain needs to be specified" end = { "domain" => domain, "name" => ["name"], "value" => { "type" => "string", "value" => ["value"] }, } ["path"] = ["path"] if .key?("path") ["httpOnly"] = ["httpOnly"] if .key?("httpOnly") ["secure"] = ["secure"] if .key?("secure") if .key?("sameSite") && !["sameSite"].nil? ["sameSite"] = CookieUtils.( ["sameSite"] ) end expiry = CookieUtils.(["expires"]) ["expiry"] = expiry unless expiry.nil? .merge!(CookieUtils.( , "sourceScheme", "priority", "url" )) partition_key = CookieUtils.( ["partitionKey"] ) -> { @user_context.(, source_origin: partition_key).wait } end AsyncUtils.await_promise_all(*tasks) unless tasks.empty? end |
#set_permission(origin, *permissions) ⇒ void
This method returns an undefined value.
Set permission states for one or more permission descriptors.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 286 def (origin, *) if origin.to_s == '*' raise UnsupportedOperationError, 'Origin (*) is not supported by WebDriver BiDi' end tasks = .map do || descriptor = [:permission] || ['permission'] state = [:state] || ['state'] raise ArgumentError, 'permission descriptor is required' unless descriptor.is_a?(Hash) raise ArgumentError, 'permission state is required' if state.nil? descriptor = descriptor.transform_keys(&:to_sym) if descriptor[:allowWithoutSanitization] || descriptor[:allow_without_sanitization] raise UnsupportedOperationError, 'allowWithoutSanitization is not supported by WebDriver BiDi' end if descriptor[:panTiltZoom] || descriptor[:pan_tilt_zoom] raise UnsupportedOperationError, 'panTiltZoom is not supported by WebDriver BiDi' end if descriptor[:userVisibleOnly] || descriptor[:user_visible_only] raise UnsupportedOperationError, 'userVisibleOnly is not supported by WebDriver BiDi' end raise ArgumentError, 'permission name is required' if descriptor[:name].nil? -> { @user_context.(origin.to_s, { name: descriptor[:name] }, state.to_s).wait } end AsyncUtils.await_promise_all(*tasks) unless tasks.empty? end |
#target_for_frame(frame) ⇒ FrameTarget
350 351 352 353 354 355 356 357 358 359 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 350 def target_for_frame(frame) context_id = frame.browsing_context.id @frame_targets[context_id] ||= begin target = FrameTarget.new(frame) frame.browsing_context.once(:closed) do @frame_targets.delete(context_id) end target end end |
#targets ⇒ Array[PageTarget | FrameTarget]
Get all known targets in this browser context.
115 116 117 118 119 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 115 def targets pages.flat_map do |page| [page.target] + page.frames.drop(1).map { |frame| target_for_frame(frame) } end end |
#wait_for_target(timeout: nil) {|arg0| ... } ⇒ PageTarget, FrameTarget
Wait until a target in this context satisfies the predicate.
125 126 127 128 129 130 131 132 133 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 125 def wait_for_target(timeout: nil, &predicate) predicate ||= ->(_target) { true } browser.wait_for_target(timeout: timeout) do |target| (target.is_a?(PageTarget) || target.is_a?(FrameTarget)) && target.browser_context == self && predicate.call(target) end #: PageTarget | FrameTarget end |