Class: Puppeteer::Bidi::FrameRealm
- 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
-
#core_realm ⇒ Core::WindowRealm
readonly
: Core::WindowRealm.
Attributes inherited from Realm
Instance Method Summary collapse
-
#call_function(function_declaration, await_promise, **options) ⇒ Hash[String, untyped]
Call a function in the realm.
-
#dispose ⇒ void
Dispose this realm.
-
#environment ⇒ Frame
Get the frame environment.
-
#evaluate(script, *args) ⇒ Object
Evaluate JavaScript in this realm.
-
#evaluate_handle(script, *args) ⇒ JSHandle
Evaluate JavaScript and return a handle to the result.
-
#execute_with_core(script, args) ⇒ Async::Task[Hash[String, untyped]]
Execute script with core realm.
-
#handle_evaluation_exception(result) ⇒ void
Handle evaluation exception from result.
-
#initialize(frame, core_realm) ⇒ FrameRealm
constructor
A new instance of FrameRealm.
-
#page ⇒ Page
Get the page.
-
#puppeteer_util ⇒ JSHandle
Get the Puppeteer utility handle.
-
#puppeteer_util_lazy_arg ⇒ LazyArg
Get or create lazy arg for Puppeteer utility.
-
#reset_puppeteer_util_handle! ⇒ void
Reset the Puppeteer utility handle.
-
#setup_core_realm_callbacks ⇒ void
Set up core realm event callbacks.
-
#wait_for_function(page_function, options = {}, *args, &block) ⇒ void
Wait for a function to return a truthy value.
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.
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_realm ⇒ Core::WindowRealm (readonly)
: Core::WindowRealm
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
182 183 184 185 186 187 188 189 |
# File 'lib/puppeteer/bidi/realm.rb', line 182 def call_function(function_declaration, await_promise, **) ensure_environment_active! result = @core_realm.call_function(function_declaration, await_promise, **).wait handle_evaluation_exception(result) if result['type'] == 'exception' result end |
#dispose ⇒ void
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 |
#environment ⇒ Frame
Get the frame environment
140 141 142 |
# File 'lib/puppeteer/bidi/realm.rb', line 140 def environment @frame end |
#evaluate(script, *args) ⇒ Object
Evaluate JavaScript in this realm
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
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
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) } = {} [:arguments] = serialized_args unless serialized_args.empty? @core_realm.call_function(script_trimmed, true, **) 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
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'] = text if exception && exception['type'] == 'object' && !exception.key?('value') = text elsif exception && exception['type'] != 'error' thrown_value = Deserializer.deserialize(exception) = "Evaluation failed: #{thrown_value}" end raise end |
#page ⇒ Page
Get the page
146 147 148 |
# File 'lib/puppeteer/bidi/realm.rb', line 146 def page @frame.page end |
#puppeteer_util ⇒ JSHandle
Get the Puppeteer utility handle
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_arg ⇒ LazyArg
Get or create lazy arg for Puppeteer utility
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_callbacks ⇒ void
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
227 228 229 230 231 |
# File 'lib/puppeteer/bidi/realm.rb', line 227 def wait_for_function(page_function, = {}, *args, &block) raise FrameDetachedError if @frame.detached? super end |