Class: Puppeteer::BrowserContext
- Inherits:
-
Object
- Object
- Puppeteer::BrowserContext
show all
- Includes:
- EventCallbackable
- Defined in:
- lib/puppeteer/browser_context.rb
Constant Summary
collapse
- WEB_PERMISSION_TO_PROTOCOL =
{
'geolocation' => 'geolocation',
'midi' => 'midi',
'notifications' => 'notifications',
'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',
'idle-detection' => 'idleDetection',
'midi-sysex' => 'midiSysex',
}.freeze
Instance Method Summary
collapse
#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener
Constructor Details
#initialize(connection, browser, context_id) ⇒ BrowserContext
Returns a new instance of BrowserContext.
8
9
10
11
12
|
# File 'lib/puppeteer/browser_context.rb', line 8
def initialize(connection, browser, context_id)
@connection = connection
@browser = browser
@id = context_id
end
|
Instance Method Details
#async_wait_for_target(predicate: , timeout: nil) ⇒ Object
49
|
# File 'lib/puppeteer/browser_context.rb', line 49
define_async_method :async_wait_for_target
|
109
110
111
|
# File 'lib/puppeteer/browser_context.rb', line 109
def browser
@browser
end
|
#clear_permission_overrides ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/puppeteer/browser_context.rb', line 95
def clear_permission_overrides
if @id
@connection.send_message('Browser.resetPermissions', browserContextId: @id)
else
@connection.send_message('Browser.resetPermissions')
end
end
|
#close ⇒ Object
113
114
115
116
117
118
|
# File 'lib/puppeteer/browser_context.rb', line 113
def close
unless @id
raise 'Non-incognito profiles cannot be closed!'
end
@browser.dispose_context(@id)
end
|
#incognito? ⇒ Boolean
56
57
58
|
# File 'lib/puppeteer/browser_context.rb', line 56
def incognito?
!!@id
end
|
104
105
106
|
# File 'lib/puppeteer/browser_context.rb', line 104
def new_page
@browser.create_page_in_context(@id)
end
|
#on(event_name, &block) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/puppeteer/browser_context.rb', line 15
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
24
25
26
27
28
29
30
|
# File 'lib/puppeteer/browser_context.rb', line 24
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
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/puppeteer/browser_context.rb', line 84
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 ⇒ !Promise<!Array<!Puppeteer.Page>>
52
53
54
|
# File 'lib/puppeteer/browser_context.rb', line 52
def pages
targets.select { |target| target.type == 'page' }.map(&:page).reject { |page| !page }
end
|
#targets ⇒ !Array<!Target>
33
34
35
|
# File 'lib/puppeteer/browser_context.rb', line 33
def targets
@browser.targets.select { |target| target.browser_context == self }
end
|
#wait_for_target(predicate:, timeout: nil) ⇒ Puppeteer::Target
39
40
41
42
43
44
|
# File 'lib/puppeteer/browser_context.rb', line 39
def wait_for_target(predicate:, timeout: nil)
@browser.wait_for_target(
predicate: ->(target) { target.browser_context == self && predicate.call(target) },
timeout: timeout,
)
end
|