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
: Browser.
-
#user_context ⇒ Core::UserContext
readonly
: Core::UserContext.
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).
-
#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 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 41 def initialize(browser, user_context) @browser = browser @user_context = user_context @pages = {} @frame_targets = {} @overrides = [] end |
Instance Attribute Details
#browser ⇒ Browser (readonly)
: Browser
36 37 38 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 36 def browser @browser end |
#user_context ⇒ Core::UserContext (readonly)
: Core::UserContext
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.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 269 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
286 287 288 289 290 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 286 def close return if closed? @user_context.remove.wait end |
#closed? ⇒ Boolean
Check if context is closed
294 295 296 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 294 def closed? @user_context.disposed? end |
#cookie_matches_filter?(cookie, raw_filter) ⇒ Boolean
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 313 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.
108 109 110 111 112 113 114 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 108 def return [] if closed? @user_context..wait.map do || CookieUtils.(, return_composite_partition_key: true) end end |
#debug_error(error) ⇒ Object
345 346 347 348 349 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 345 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.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 166 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.
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 182 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)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 54 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 |
#override_permissions(origin, permissions) ⇒ void
This method returns an undefined value.
Override permissions for an origin
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 212 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
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 196 def page_for(browsing_context) @pages[browsing_context.id] ||= begin page = Page.new(self, browsing_context) browsing_context.once(:closed) do @pages.delete(browsing_context.id) end page end end |
#pages ⇒ Array[Page]
Get all pages in this context
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 72 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.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 120 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.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 238 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
302 303 304 305 306 307 308 309 310 311 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 302 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.
86 87 88 89 90 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 86 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.
96 97 98 99 100 101 102 103 104 |
# File 'lib/puppeteer/bidi/browser_context.rb', line 96 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 |