Class: Puppeteer::BrowserContext

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:



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

#idObject (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

Parameters:



102
# File 'lib/puppeteer/browser_context.rb', line 102

define_async_method :async_wait_for_target

#browserBrowser

Returns:



222
223
224
# File 'lib/puppeteer/browser_context.rb', line 222

def browser
  @browser
end

#clear_permission_overridesObject



194
195
196
197
198
199
200
# File 'lib/puppeteer/browser_context.rb', line 194

def clear_permission_overrides
  if @id
    @connection.send_message('Browser.resetPermissions', browserContextId: @id)
  else
    @connection.send_message('Browser.resetPermissions')
  end
end

#closeObject



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

Returns:

  • (Boolean)


116
117
118
# File 'lib/puppeteer/browser_context.rb', line 116

def closed?
  @closed || !@browser.browser_contexts.include?(self)
end

#cookiesArray<Hash>

Returns:

  • (Array<Hash>)


235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/puppeteer/browser_context.rb', line 235

def cookies
  params = { browserContextId: @id }.compact
  response = @connection.send_message('Storage.getCookies', params)
  response.fetch('cookies', []).map do |cookie|
    normalized = cookie.dup
    partition_key = cookie['partitionKey']
    if partition_key
      normalized['partitionKey'] = convert_partition_key_from_cdp(partition_key)
    end
    normalized['sameParty'] = cookie['sameParty'] || false
    normalized
  end
end

Parameters:

  • cookies (Array<Hash>)


267
268
269
270
271
272
273
274
# File 'lib/puppeteer/browser_context.rb', line 267

def delete_cookie(*cookies)
  items = cookies.map do |cookie|
    normalized = normalize_cookie_hash(cookie)
    normalized['expires'] = 1
    normalized
  end
  set_cookie(*items)
end

#delete_matching_cookies(*filters) ⇒ Object

Parameters:

  • filters (Array<Hash>)


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 delete_matching_cookies(*filters)
  cookies_to_delete = cookies.select do |cookie|
    filters.any? do |filter|
      filter_name = hash_value(filter, 'name')
      next false unless filter_name == cookie['name']

      filter_domain = hash_value(filter, 'domain')
      next true if filter_domain && filter_domain == cookie['domain']

      filter_path = hash_value(filter, 'path')
      next true if filter_path && filter_path == cookie['path']

      filter_partition_key = hash_value(filter, 'partitionKey', 'partition_key')
      if filter_partition_key && cookie['partitionKey']
        if cookie['partitionKey'].is_a?(String)
          raise Puppeteer::Error.new('Unexpected string partition key')
        end

        cookie_partition_source_origin = hash_value(cookie['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 == cookie_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 == cookie['domain'] && url_path == cookie['path']
      end

      true
    end
  end

  delete_cookie(*cookies_to_delete)
end

#incognito?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/puppeteer/browser_context.rb', line 112

def incognito?
  !!@id
end

#new_pageFuture<Puppeteer::Page>

Returns:



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

Parameters:

  • event_name (Symbol)

    either of :disconnected, :targetcreated, :targetchanged, :targetdestroyed



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

Parameters:

  • event_name (Symbol)


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

Parameters:

  • origin (String)
  • permissions (Array<String>)


145
146
147
148
149
150
151
152
153
154
# File 'lib/puppeteer/browser_context.rb', line 145

def override_permissions(origin, permissions)
  protocol_permissions = permissions.map do |permission|
    WEB_PERMISSION_TO_PROTOCOL[permission] or raise ArgumentError.new("Unknown permission: #{permission}")
  end
  @connection.send_message('Browser.grantPermissions', {
    origin: origin,
    browserContextId: @id,
    permissions: protocol_permissions,
  }.compact)
end

#pages(include_all: false) ⇒ !Promise<!Array<!Puppeteer.Page>>

Returns:



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

Parameters:

  • cookies (Array<Hash>)


250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/puppeteer/browser_context.rb', line 250

def set_cookie(*cookies)
  items = cookies.map do |cookie|
    normalized = normalize_cookie_hash(cookie)
    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.send_message('Storage.setCookies', {
    browserContextId: @id,
    cookies: items,
  }.compact)
end

#set_download_behavior(download_behavior) ⇒ Object

Parameters:

  • download_behavior (Hash)


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.send_message('Browser.setDownloadBehavior', {
    behavior: behavior,
    downloadPath: download_path,
    browserContextId: @id,
  }.compact)
end

#set_permission(origin, *permissions) ⇒ Object

Parameters:

  • origin (String)

    Use ‘*’ to apply to all origins.

  • permissions (Array<Hash>)

    { permission: { name:, userVisibleOnly:, sysex:, panTiltZoom:, allowWithoutSanitization: }, state: ‘granted’|‘denied’|‘prompt’ }



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 set_permission(origin, *permissions)
  permissions.each do |permission|
    descriptor = hash_value(permission, 'permission')
    state = hash_value(permission, '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

    protocol_permission = { 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_permission[protocol_key] = value unless value.nil?
    end

    @connection.send_message('Browser.setPermission', {
      origin: origin == '*' ? nil : origin,
      browserContextId: @id,
      permission: protocol_permission,
      setting: state.to_s,
    }.compact)
  end
end

#start_screenshotObject



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.

Returns:

  • (!Array<!Target>)

    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_operationsObject



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

Parameters:

Returns:



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