Class: Puppeteer::Browser
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from IfPresent
#if_present
#add_event_listener, #emit_event, #on_event, #remove_event_listener
Methods included from DebugPrint
#debug_print, #debug_puts
Constructor Details
#initialize(connection:, context_ids:, ignore_https_errors:, default_viewport:, process:, close_callback:) ⇒ Browser
Returns a new instance of Browser.
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/puppeteer/browser.rb', line 35
def initialize(connection:, context_ids:, ignore_https_errors:, default_viewport:, process:, close_callback:)
@ignore_https_errors = ignore_https_errors
@default_viewport = default_viewport
@process = process
@connection = connection
@close_callback = close_callback
@default_context = Puppeteer::BrowserContext.new(@connection, self, nil)
@contexts = {}
context_ids.each do |context_id|
@contexts[context_id] = Puppeteer::BrowserContext.new(@connection, self. context_id)
end
@targets = {}
@connection.on_event 'Events.CDPSession.Disconnected' do
emit_event 'Events.Browser.Disconnected'
end
@connection.on_event 'Target.targetCreated', &method(:handle_target_created)
@connection.on_event 'Target.targetDestroyed', &method(:handle_target_destroyed)
@connection.on_event 'Target.targetInfoChanged', &method(:handle_target_info_changed)
end
|
Class Method Details
.create(connection:, context_ids:, ignore_https_errors:, default_viewport:, process:, close_callback:) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/puppeteer/browser.rb', line 16
def self.create(connection:, context_ids:, ignore_https_errors:, default_viewport:, process:, close_callback:)
browser = Puppeteer::Browser.new(
connection: connection,
context_ids: context_ids,
ignore_https_errors: ignore_https_errors,
default_viewport: default_viewport,
process: process,
close_callback: close_callback,
)
connection.send_message('Target.setDiscoverTargets', discover: true)
browser
end
|
Instance Method Details
#browser_contexts ⇒ Object
69
70
71
|
# File 'lib/puppeteer/browser.rb', line 69
def browser_contexts
[@default_context].concat(@contexts.values)
end
|
#close ⇒ Object
244
245
246
247
|
# File 'lib/puppeteer/browser.rb', line 244
def close
@close_callback.call
disconnect
end
|
#connected? ⇒ Boolean
253
254
255
|
# File 'lib/puppeteer/browser.rb', line 253
def connected?
!@connection.closed?
end
|
63
64
65
66
67
|
# File 'lib/puppeteer/browser.rb', line 63
def create_incognito_browser_context
result = @connection.send_message('Target.createBrowserContext')
browser_context_id = result['browserContextId']
@contexts[browser_context_id] = Puppeteer::BrowserContext.new(@connection, self, browser_context_id)
end
|
#create_page_in_context(context_id) ⇒ !Promise<!Puppeteer.Page>
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/puppeteer/browser.rb', line 172
def create_page_in_context(context_id)
create_target_params = { url: 'about:blank' }
if context_id
create_target_params[:browserContextId] = context_id
end
result = @connection.send_message('Target.createTarget', **create_target_params)
target_id = result['targetId']
target = @targets[target_id]
await target.initialized_promise
await target.page
end
|
74
75
76
|
# File 'lib/puppeteer/browser.rb', line 74
def default_browser_context
@default_context
end
|
#disconnect ⇒ Object
249
250
251
|
# File 'lib/puppeteer/browser.rb', line 249
def disconnect
@connection.dispose
end
|
#dispose_context(context_id) ⇒ Object
79
80
81
82
|
# File 'lib/puppeteer/browser.rb', line 79
def dispose_context(context_id)
@connection.send_message('Target.disposeBrowserContext', browserContextId: context_id)
@contexts.remove(context_id)
end
|
#handle_target_created(event) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/puppeteer/browser.rb', line 85
def handle_target_created(event)
target_info = Puppeteer::Target::TargetInfo.new(event['targetInfo'])
browser_context_id = target_info.browser_context_id
context =
if browser_context_id && @contexts.has_key?(browser_context_id)
@contexts[browser_context_id]
else
@default_context
end
target = Puppeteer::Target.new(
target_info: target_info,
browser_context: context,
session_factory: -> { @connection.create_session(target_info) },
ignore_https_errors: @ignore_https_errors,
default_viewport: @default_viewport,
screenshot_task_queue: @screenshot_task_queue,
)
@targets[target_info.target_id] = target
target.on_initialize_succeeded do
emit_event 'Events.Browser.TargetCreated', target
context.emit_event 'Events.BrowserContext.TargetCreated', target
end
if_present(pending_target_info_changed_event.delete(target_info.target_id)) do |pending_event|
handle_target_info_changed(pending_event)
end
end
|
#handle_target_destroyed(event) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/puppeteer/browser.rb', line 118
def handle_target_destroyed(event)
target_id = event['targetId']
target = @targets[target_id]
target.handle_initialized(false)
@targets.delete(target_id)
target.handle_closed
target.on_initialize_succeeded do
emit_event 'Events.Browser.TargetDestroyed', target
target.browser_context.emit_event 'Events.BrowserContext.TargetDestroyed', target
end
end
|
#handle_target_info_changed(event) ⇒ Object
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
|
# File 'lib/puppeteer/browser.rb', line 131
def handle_target_info_changed(event)
target_info = Puppeteer::Target::TargetInfo.new(event['targetInfo'])
target = @targets[target_info.target_id]
if !target
pending_target_info_changed_event[target_info.target_id] = event
return
end
previous_url = target.url
was_initialized = target.initialized?
target.handle_target_info_changed(target_info)
if was_initialized && previous_url != target.url
emit_event 'Events.Browser.TargetChanged', target
target.browser_context.emit_event 'Events.BrowserContext.TargetChanged', target
end
end
|
#new_page ⇒ Object
166
167
168
|
# File 'lib/puppeteer/browser.rb', line 166
def new_page
@default_context.new_page
end
|
#pages ⇒ !Promise<!Array<!Puppeteer.Page>>
230
231
232
|
# File 'lib/puppeteer/browser.rb', line 230
def pages
browser_contexts.flat_map(&:pages)
end
|
58
59
60
|
# File 'lib/puppeteer/browser.rb', line 58
def process
@process
end
|
191
192
193
|
# File 'lib/puppeteer/browser.rb', line 191
def target
targets.first { |target| target.type == 'browser' }
end
|
#targets ⇒ !Array<!Target>
185
186
187
|
# File 'lib/puppeteer/browser.rb', line 185
def targets
@targets.values.select { |target| target.initialized? }
end
|
#user_agent ⇒ String
240
241
242
|
# File 'lib/puppeteer/browser.rb', line 240
def user_agent
get_version.user_agent
end
|
#version ⇒ String
235
236
237
|
# File 'lib/puppeteer/browser.rb', line 235
def version
get_version.product
end
|
#wait_for_target(predicate:, timeout: nil) ⇒ !Promise<!Target>
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/puppeteer/browser.rb', line 198
def wait_for_target(predicate:, timeout: nil)
timeout_in_sec = (timeout || 30000).to_i / 1000.0
existing_target = targets.first { |target| predicate.call(target) }
return existing_target if existing_target
event_listening_ids = []
target_promise = resolvable_future
event_listening_ids << add_event_listener('Events.Browser.TargetCreated') do |target|
if predicate.call(target)
target_promise.fulfill(target)
end
end
event_listening_ids << add_event_listener('Events.Browser.TargetChanged') do |target|
if predicate.call(target)
target_promise.fulfill(target)
end
end
begin
if timeout_in_sec > 0
Timeout.timeout(timeout_in_sec) do
target_promise.value!
end
else
target_promise.value!
end
ensure
remove_event_listener(*event_listening_ids)
end
end
|
#websocket_endpoint ⇒ String
162
163
164
|
# File 'lib/puppeteer/browser.rb', line 162
def websocket_endpoint
@connection.url
end
|