Class: Puppeteer::JSHandle
- Inherits:
-
Object
- Object
- Puppeteer::JSHandle
- Includes:
- IfPresent
- Defined in:
- lib/puppeteer/js_handle.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#remote_object ⇒ Object
readonly
Returns the value of attribute remote_object.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Puppeteer::JSHandle
- #as_element ⇒ Object
- #dispose ⇒ Object
- #disposed? ⇒ Boolean
- #evaluate(page_function, *args) ⇒ Object
- #evaluate_handle(page_function, *args) ⇒ Puppeteer::JSHandle
- #execution_context ⇒ Puppeteer::ExecutionContext
-
#initialize(context:, client:, remote_object:) ⇒ JSHandle
constructor
A new instance of JSHandle.
- #inspect ⇒ Object
- #json_value ⇒ Object
-
#properties ⇒ Hash<String, JSHandle>
getProperties in JavaScript.
-
#property(name) ⇒ Puppeteer::JSHandle
getProperty(propertyName) in JavaScript.
- #to_s ⇒ Object
Methods included from IfPresent
Constructor Details
#initialize(context:, client:, remote_object:) ⇒ JSHandle
Returns a new instance of JSHandle.
27 28 29 30 31 32 |
# File 'lib/puppeteer/js_handle.rb', line 27 def initialize(context:, client:, remote_object:) @context = context @client = client @remote_object = remote_object @disposed = false end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
34 35 36 |
# File 'lib/puppeteer/js_handle.rb', line 34 def context @context end |
#remote_object ⇒ Object (readonly)
Returns the value of attribute remote_object.
34 35 36 |
# File 'lib/puppeteer/js_handle.rb', line 34 def remote_object @remote_object end |
Class Method Details
.create(context:, remote_object:) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/puppeteer/js_handle.rb', line 7 def self.create(context:, remote_object:) if remote_object.sub_type == 'node' && context.world Puppeteer::ElementHandle.new( context: context, client: context.client, remote_object: remote_object, frame: context.world.frame, ) else Puppeteer::JSHandle.new( context: context, client: context.client, remote_object: remote_object, ) end end |
Instance Method Details
#[](name) ⇒ Puppeteer::JSHandle
86 87 88 |
# File 'lib/puppeteer/js_handle.rb', line 86 def [](name) property(name) end |
#as_element ⇒ Object
121 122 123 |
# File 'lib/puppeteer/js_handle.rb', line 121 def as_element nil end |
#dispose ⇒ Object
125 126 127 128 129 130 |
# File 'lib/puppeteer/js_handle.rb', line 125 def dispose return if @disposed @disposed = true @remote_object.release(@client) end |
#disposed? ⇒ Boolean
132 133 134 |
# File 'lib/puppeteer/js_handle.rb', line 132 def disposed? @disposed end |
#evaluate(page_function, *args) ⇒ Object
51 52 53 |
# File 'lib/puppeteer/js_handle.rb', line 51 def evaluate(page_function, *args) execution_context.evaluate(page_function, self, *args) end |
#evaluate_handle(page_function, *args) ⇒ Puppeteer::JSHandle
60 61 62 |
# File 'lib/puppeteer/js_handle.rb', line 60 def evaluate_handle(page_function, *args) execution_context.evaluate_handle(page_function, self, *args) end |
#execution_context ⇒ Puppeteer::ExecutionContext
45 46 47 |
# File 'lib/puppeteer/js_handle.rb', line 45 def execution_context @context end |
#inspect ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/puppeteer/js_handle.rb', line 36 def inspect values = %i[context remote_object disposed].map do |sym| value = instance_variable_get(:"@#{sym}") "@#{sym}=#{value}" end "#<Puppeteer::JSHandle #{values.join(' ')}>" end |
#json_value ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/puppeteer/js_handle.rb', line 103 def json_value # original logic was: # if (this._remoteObject.objectId) { # const response = await this._client.send('Runtime.callFunctionOn', { # functionDeclaration: 'function() { return this; }', # objectId: this._remoteObject.objectId, # returnByValue: true, # awaitPromise: true, # }); # return helper.valueFromRemoteObject(response.result); # } # return helper.valueFromRemoteObject(this._remoteObject); # # However it would be better that RemoteObject is responsible for # the logic `if (this._remoteObject.objectId) { ... }`. @remote_object.evaluate_self(@client)&.value || @remote_object.value end |
#properties ⇒ Hash<String, JSHandle>
getProperties in JavaScript.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/puppeteer/js_handle.rb', line 92 def properties response = @remote_object.properties(@client) response['result'].each_with_object({}) do |prop, h| next unless prop['enumerable'] h[prop['name']] = Puppeteer::JSHandle.create( context: @context, remote_object: Puppeteer::RemoteObject.new(prop['value']), ) end end |
#property(name) ⇒ Puppeteer::JSHandle
getProperty(propertyName) in JavaScript
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/puppeteer/js_handle.rb', line 69 def property(name) js = <<~JAVASCRIPT (object, propertyName) => { const result = {__proto__: null}; result[propertyName] = object[propertyName]; return result; } JAVASCRIPT object_handle = evaluate_handle(js, name) properties = object_handle.properties result = properties[name] object_handle.dispose result end |
#to_s ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/puppeteer/js_handle.rb', line 136 def to_s # original logic was: # if (this._remoteObject.objectId) { # const type = this._remoteObject.subtype || this._remoteObject.type; # return 'JSHandle@' + type; # } # return 'JSHandle:' + helper.valueFromRemoteObject(this._remoteObject); # # However it would be better that RemoteObject is responsible for # the logic `if (this._remoteObject.objectId) { ... }`. if_present(@remote_object.type_str) { |type_str| "JSHandle@#{type_str}" } || "JSHandle:#{@remote_object.value || 'undefined'}" end |