Class: Puppeteer::Bidi::FrameRealm

Inherits:
Realm
  • Object
show all
Defined in:
lib/puppeteer/bidi/realm.rb,
sig/puppeteer/bidi/realm.rbs

Overview

Concrete realm that wraps the default window realm for a Frame. https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/bidi/Realm.ts

Instance Attribute Summary collapse

Attributes inherited from Realm

#task_manager

Instance Method Summary collapse

Methods inherited from Realm

#adopt_handle, #default_timeout, #disposed?, #ensure_environment_active!, #transfer_handle

Constructor Details

#initialize(frame, core_realm) ⇒ FrameRealm

Returns a new instance of FrameRealm.

Parameters:



128
129
130
131
132
133
134
135
136
# File 'lib/puppeteer/bidi/realm.rb', line 128

def initialize(frame, core_realm)
  @frame = frame
  @core_realm = core_realm
  @puppeteer_util_handle = nil
  @puppeteer_util_lazy_arg = nil
  super(frame.page.timeout_settings)

  setup_core_realm_callbacks
end

Instance Attribute Details

#core_realmCore::WindowRealm (readonly)

: Core::WindowRealm

Returns:



123
124
125
# File 'lib/puppeteer/bidi/realm.rb', line 123

def core_realm
  @core_realm
end

Instance Method Details

#call_function(function_declaration, await_promise, **options) ⇒ Hash[String, untyped]

Call a function in the realm

Parameters:

  • function_declaration (String)
  • await_promise (Boolean)
  • options (Object)

Returns:

  • (Hash[String, untyped])


182
183
184
185
186
187
188
189
# File 'lib/puppeteer/bidi/realm.rb', line 182

def call_function(function_declaration, await_promise, **options)
  ensure_environment_active!

  result = @core_realm.call_function(function_declaration, await_promise, **options).wait
  handle_evaluation_exception(result) if result['type'] == 'exception'

  result
end

#disposevoid

This method returns an undefined value.

Dispose this realm



235
236
237
238
# File 'lib/puppeteer/bidi/realm.rb', line 235

def dispose
  reset_puppeteer_util_handle!
  super
end

#environmentFrame

Get the frame environment

Returns:



140
141
142
# File 'lib/puppeteer/bidi/realm.rb', line 140

def environment
  @frame
end

#evaluate(script, *args) ⇒ Object

Evaluate JavaScript in this realm

Parameters:

  • script (String)
  • args (Object)

Returns:

  • (Object)


154
155
156
157
158
159
160
161
162
# File 'lib/puppeteer/bidi/realm.rb', line 154

def evaluate(script, *args)
  ensure_environment_active!

  result = execute_with_core(script, args).wait
  handle_evaluation_exception(result) if result['type'] == 'exception'

  actual_result = result['result'] || result
  Deserializer.deserialize(actual_result)
end

#evaluate_handle(script, *args) ⇒ JSHandle

Evaluate JavaScript and return a handle to the result

Parameters:

  • script (String)
  • args (Object)

Returns:



168
169
170
171
172
173
174
175
# File 'lib/puppeteer/bidi/realm.rb', line 168

def evaluate_handle(script, *args)
  ensure_environment_active!

  result = execute_with_core(script, args).wait
  handle_evaluation_exception(result) if result['type'] == 'exception'

  JSHandle.from(result['result'], @core_realm)
end

#execute_with_core(script, args) ⇒ Async::Task[Hash[String, untyped]]

Execute script with core realm

Parameters:

  • script (String)
  • args (Array[untyped])

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/puppeteer/bidi/realm.rb', line 246

def execute_with_core(script, args)
  script_trimmed = script.strip

  is_iife = script_trimmed.match?(/\)\s*\(\s*\)\s*\z/)
  is_function = !is_iife && (
    script_trimmed.match?(/\A\s*(?:async\s+)?(?:\(.*?\)|[a-zA-Z_$][\w$]*)\s*=>/) ||
    script_trimmed.match?(/\A\s*(?:async\s+)?function\s*\w*\s*\(/)
  )

  if is_function
    serialized_args = args.map { |arg| Serializer.serialize(arg) }
    options = {}
    options[:arguments] = serialized_args unless serialized_args.empty?
    @core_realm.call_function(script_trimmed, true, **options)
  else
    @core_realm.evaluate(script_trimmed, true)
  end
end

#handle_evaluation_exception(result) ⇒ void

This method returns an undefined value.

Handle evaluation exception from result

Parameters:

  • result (Hash[String, untyped])


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/puppeteer/bidi/realm.rb', line 268

def handle_evaluation_exception(result)
  exception_details = result['exceptionDetails']
  return unless exception_details

  text = exception_details['text'] || 'Evaluation failed'
  exception = exception_details['exception']

  error_message = text

  if exception && exception['type'] == 'object' && !exception.key?('value')
    error_message = text
  elsif exception && exception['type'] != 'error'
    thrown_value = Deserializer.deserialize(exception)
    error_message = "Evaluation failed: #{thrown_value}"
  end

  raise error_message
end

#pagePage

Get the page

Returns:



146
147
148
# File 'lib/puppeteer/bidi/realm.rb', line 146

def page
  @frame.page
end

#puppeteer_utilJSHandle

Get the Puppeteer utility handle

Returns:



193
194
195
196
197
198
# File 'lib/puppeteer/bidi/realm.rb', line 193

def puppeteer_util
  return @puppeteer_util_handle if @puppeteer_util_handle

  script = "(function() { const module = { exports: {} }; #{PUPPETEER_INJECTED_SOURCE}; return module.exports.default; })()"
  @puppeteer_util_handle = evaluate_handle(script)
end

#puppeteer_util_lazy_argLazyArg

Get or create lazy arg for Puppeteer utility

Returns:



202
203
204
# File 'lib/puppeteer/bidi/realm.rb', line 202

def puppeteer_util_lazy_arg
  @puppeteer_util_lazy_arg ||= LazyArg.create { puppeteer_util }
end

#reset_puppeteer_util_handle!void

This method returns an undefined value.

Reset the Puppeteer utility handle



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/puppeteer/bidi/realm.rb', line 208

def reset_puppeteer_util_handle!
  handle = @puppeteer_util_handle
  @puppeteer_util_handle = nil
  @puppeteer_util_lazy_arg = nil
  return unless handle

  begin
    handle.dispose
  rescue StandardError
    # The realm might already be gone; ignore cleanup failures.
  end
end

#setup_core_realm_callbacksvoid

This method returns an undefined value.

Set up core realm event callbacks



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/puppeteer/bidi/realm.rb', line 289

def setup_core_realm_callbacks
  @core_realm.environment = @frame if @core_realm.respond_to?(:environment=)

  destroyed_listener = proc do |reason|
    task_manager.terminate_all(Error.new(reason || 'Realm destroyed'))
    dispose
  end

  updated_listener = proc do
    reset_puppeteer_util_handle!
    task_manager.rerun_all
  end

  @core_realm.on(:destroyed, &destroyed_listener)
  @core_realm.on(:updated, &updated_listener)
end

#wait_for_function(page_function, options = {}, *args, &block) ⇒ void

This method returns an undefined value.

Wait for a function to return a truthy value

Parameters:

  • page_function (String)
  • options (Hash[Symbol, untyped]) (defaults to: {})
  • args (Object)


227
228
229
230
231
# File 'lib/puppeteer/bidi/realm.rb', line 227

def wait_for_function(page_function, options = {}, *args, &block)
  raise FrameDetachedError if @frame.detached?

  super
end