Class: Puppeteer::BrowserContext
- Inherits:
-
Object
- Object
- Puppeteer::BrowserContext
- Includes:
- EventCallbackable
- Defined in:
- lib/puppeteer/browser_context.rb
Defined Under Namespace
Classes: ScreenshotGuard
Constant Summary collapse
- WEB_PERMISSION_TO_PROTOCOL =
{ 'geolocation' => 'geolocation', 'midi' => 'midi', 'notifications' => 'notifications', # TODO: push isn't a valid type? # 'push' => 'push', 'camera' => 'videoCapture', 'microphone' => 'audioCapture', 'background-sync' => 'backgroundSync', 'ambient-light-sensor' => 'sensors', 'accelerometer' => 'sensors', 'gyroscope' => 'sensors', 'magnetometer' => 'sensors', 'accessibility-events' => 'accessibilityEvents', 'clipboard-read' => 'clipboardReadWrite', 'clipboard-write' => 'clipboardReadWrite', 'payment-handler' => 'paymentHandler', 'persistent-storage' => 'durableStorage', 'idle-detection' => 'idleDetection', # chrome-specific permissions we have. 'midi-sysex' => 'midiSysex', }.freeze
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #async_wait_for_target(predicate:, timeout: nil) ⇒ Object
- #browser ⇒ Browser
- #clear_permission_overrides ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #cookies ⇒ Array<Hash>
- #delete_cookie(*cookies) ⇒ Object
- #delete_matching_cookies(*filters) ⇒ Object
- #incognito? ⇒ Boolean
-
#initialize(connection, browser, context_id) ⇒ BrowserContext
constructor
A new instance of BrowserContext.
- #new_page ⇒ Future<Puppeteer::Page>
- #on(event_name, &block) ⇒ Object
- #once(event_name, &block) ⇒ Object
- #override_permissions(origin, permissions) ⇒ Object
- #pages(include_all: false) ⇒ !Promise<!Array<!Puppeteer.Page>>
- #set_cookie(*cookies) ⇒ Object
- #set_download_behavior(download_behavior) ⇒ Object
- #set_permission(origin, *permissions) ⇒ Object
- #start_screenshot ⇒ Object
-
#targets ⇒ !Array<!Target>
Target.
- #wait_for_screenshot_operations ⇒ Object
- #wait_for_target(predicate:, timeout: nil) ⇒ Puppeteer::Target
Methods included from EventCallbackable
#add_event_listener, #emit_event, #observe_first, #off, #on_event, #remove_event_listener
Constructor Details
#initialize(connection, browser, context_id) ⇒ BrowserContext
Returns a new instance of BrowserContext.
11 12 13 14 15 16 17 18 |
# File 'lib/puppeteer/browser_context.rb', line 11 def initialize(connection, browser, context_id) @connection = connection @browser = browser @id = context_id @closed = false @screenshot_semaphore = nil @screenshot_operations_count = 0 end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
20 21 22 |
# File 'lib/puppeteer/browser_context.rb', line 20 def id @id end |
Instance Method Details
#==(other) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/puppeteer/browser_context.rb', line 39 def ==(other) other = other.__getobj__ if other.is_a?(Puppeteer::ReactorRunner::Proxy) return true if equal?(other) return false unless other.is_a?(Puppeteer::BrowserContext) return false if @id.nil? || other.id.nil? @id == other.id end |
#async_wait_for_target(predicate:, timeout: nil) ⇒ Object
102 |
# File 'lib/puppeteer/browser_context.rb', line 102 define_async_method :async_wait_for_target |
#browser ⇒ Browser
222 223 224 |
# File 'lib/puppeteer/browser_context.rb', line 222 def browser @browser end |
#clear_permission_overrides ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/puppeteer/browser_context.rb', line 194 def if @id @connection.('Browser.resetPermissions', browserContextId: @id) else @connection.('Browser.resetPermissions') end end |
#close ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/puppeteer/browser_context.rb', line 226 def close unless @id raise 'Non-incognito profiles cannot be closed!' end @browser.dispose_context(@id) @closed = true end |
#closed? ⇒ Boolean
116 117 118 |
# File 'lib/puppeteer/browser_context.rb', line 116 def closed? @closed || !@browser.browser_contexts.include?(self) end |
#cookies ⇒ Array<Hash>
235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/puppeteer/browser_context.rb', line 235 def params = { browserContextId: @id }.compact response = @connection.('Storage.getCookies', params) response.fetch('cookies', []).map do || normalized = .dup partition_key = ['partitionKey'] if partition_key normalized['partitionKey'] = convert_partition_key_from_cdp(partition_key) end normalized['sameParty'] = ['sameParty'] || false normalized end end |
#delete_cookie(*cookies) ⇒ Object
267 268 269 270 271 272 273 274 |
# File 'lib/puppeteer/browser_context.rb', line 267 def (*) items = .map do || normalized = () normalized['expires'] = 1 normalized end (*items) end |
#delete_matching_cookies(*filters) ⇒ Object
277 278 279 280 281 282 283 284 285 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 314 315 316 317 318 |
# File 'lib/puppeteer/browser_context.rb', line 277 def (*filters) = .select do || filters.any? do |filter| filter_name = hash_value(filter, 'name') next false unless filter_name == ['name'] filter_domain = hash_value(filter, 'domain') next true if filter_domain && filter_domain == ['domain'] filter_path = hash_value(filter, 'path') next true if filter_path && filter_path == ['path'] filter_partition_key = hash_value(filter, 'partitionKey', 'partition_key') if filter_partition_key && ['partitionKey'] if ['partitionKey'].is_a?(String) raise Puppeteer::Error.new('Unexpected string partition key') end = hash_value(['partitionKey'], 'sourceOrigin', 'source_origin') filter_partition_source_origin = if filter_partition_key.is_a?(String) filter_partition_key else hash_value(filter_partition_key, 'sourceOrigin', 'source_origin') end next true if filter_partition_source_origin == end filter_url = hash_value(filter, 'url') if filter_url url = URI.parse(filter_url) url_path = url.path.to_s.empty? ? '/' : url.path next true if url.hostname == ['domain'] && url_path == ['path'] end true end end (*) end |
#incognito? ⇒ Boolean
112 113 114 |
# File 'lib/puppeteer/browser_context.rb', line 112 def incognito? !!@id end |
#new_page ⇒ Future<Puppeteer::Page>
214 215 216 217 218 219 |
# File 'lib/puppeteer/browser_context.rb', line 214 def new_page guard = wait_for_screenshot_operations @browser.create_page_in_context(@id) ensure guard&.release end |
#on(event_name, &block) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/puppeteer/browser_context.rb', line 49 def on(event_name, &block) unless BrowserContextEmittedEvents.values.include?(event_name.to_s) raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserContextEmittedEvents.values.to_a.join(", ")}") end super(event_name.to_s, &block) end |
#once(event_name, &block) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/puppeteer/browser_context.rb', line 58 def once(event_name, &block) unless BrowserContextEmittedEvents.values.include?(event_name.to_s) raise ArgumentError.new("Unknown event name: #{event_name}. Known events are #{BrowserContextEmittedEvents.values.to_a.join(", ")}") end super(event_name.to_s, &block) end |
#override_permissions(origin, permissions) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/puppeteer/browser_context.rb', line 145 def (origin, ) = .map do || WEB_PERMISSION_TO_PROTOCOL[] or raise ArgumentError.new("Unknown permission: #{}") end @connection.('Browser.grantPermissions', { origin: origin, browserContextId: @id, permissions: , }.compact) end |
#pages(include_all: false) ⇒ !Promise<!Array<!Puppeteer.Page>>
105 106 107 108 109 110 |
# File 'lib/puppeteer/browser_context.rb', line 105 def pages(include_all: false) targets.select { |target| target.type == 'page' || ((target.type == 'other' || include_all) && @browser.is_page_target_callback&.call(target.target_info)) }.map(&:page).compact end |
#set_cookie(*cookies) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/puppeteer/browser_context.rb', line 250 def (*) items = .map do || normalized = () partition_key = normalized.delete('partitionKey') || normalized.delete('partition_key') normalized['partitionKey'] = convert_partition_key_for_cdp(partition_key) if partition_key same_site = normalized.delete('sameSite') || normalized.delete('same_site') converted_same_site = convert_same_site_for_cdp(same_site) normalized['sameSite'] = converted_same_site if converted_same_site normalized end @connection.('Storage.setCookies', { browserContextId: @id, cookies: items, }.compact) end |
#set_download_behavior(download_behavior) ⇒ Object
203 204 205 206 207 208 209 210 211 |
# File 'lib/puppeteer/browser_context.rb', line 203 def set_download_behavior(download_behavior) behavior = hash_value(download_behavior, 'policy') download_path = hash_value(download_behavior, 'downloadPath', 'download_path') @connection.('Browser.setDownloadBehavior', { behavior: behavior, downloadPath: download_path, browserContextId: @id, }.compact) end |
#set_permission(origin, *permissions) ⇒ Object
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 190 191 192 |
# File 'lib/puppeteer/browser_context.rb', line 158 def (origin, *) .each do || descriptor = hash_value(, 'permission') state = hash_value(, 'state') unless descriptor.is_a?(Hash) raise ArgumentError.new('Each permission entry must include a permission descriptor') end unless state raise ArgumentError.new('Each permission entry must include state') end name = hash_value(descriptor, 'name') unless name raise ArgumentError.new('Permission descriptor must include name') end = { name: name.to_s } { userVisibleOnly: %w[userVisibleOnly user_visible_only], sysex: %w[sysex], panTiltZoom: %w[panTiltZoom pan_tilt_zoom], allowWithoutSanitization: %w[allowWithoutSanitization allow_without_sanitization], }.each do |protocol_key, keys| value = hash_value(descriptor, *keys) [protocol_key] = value unless value.nil? end @connection.('Browser.setPermission', { origin: origin == '*' ? nil : origin, browserContextId: @id, permission: , setting: state.to_s, }.compact) end end |
#start_screenshot ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/puppeteer/browser_context.rb', line 71 def start_screenshot semaphore = @screenshot_semaphore || Async::Semaphore.new(1) @screenshot_semaphore = semaphore @screenshot_operations_count += 1 semaphore.acquire ScreenshotGuard.new(semaphore, on_release: lambda { @screenshot_operations_count -= 1 @screenshot_semaphore = nil if @screenshot_operations_count.zero? }) end |
#targets ⇒ !Array<!Target>
Returns target.
67 68 69 |
# File 'lib/puppeteer/browser_context.rb', line 67 def targets @browser.targets.select { |target| target.browser_context == self } end |
#wait_for_screenshot_operations ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/puppeteer/browser_context.rb', line 82 def wait_for_screenshot_operations semaphore = @screenshot_semaphore return nil unless semaphore semaphore.acquire ScreenshotGuard.new(semaphore) end |
#wait_for_target(predicate:, timeout: nil) ⇒ Puppeteer::Target
92 93 94 95 96 97 |
# File 'lib/puppeteer/browser_context.rb', line 92 def wait_for_target(predicate:, timeout: nil) @browser.wait_for_target( predicate: ->(target) { target.browser_context == self && predicate.call(target) }, timeout: timeout, ) end |