Class: Puppeteer::JSHandle

Inherits:
Object
  • Object
show all
Includes:
IfPresent
Defined in:
lib/puppeteer/js_handle.rb

Direct Known Subclasses

ElementHandle

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Constructor Details

#initialize(context:, client:, remote_object:) ⇒ JSHandle

Returns a new instance of JSHandle.

Parameters:



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

#contextObject (readonly)

Returns the value of attribute context.



34
35
36
# File 'lib/puppeteer/js_handle.rb', line 34

def context
  @context
end

#remote_objectObject (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

Parameters:



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

Parameters:

  • name (String)

Returns:



86
87
88
# File 'lib/puppeteer/js_handle.rb', line 86

def [](name)
  property(name)
end

#as_elementObject



121
122
123
# File 'lib/puppeteer/js_handle.rb', line 121

def as_element
  nil
end

#disposeObject



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

Returns:

  • (Boolean)


132
133
134
# File 'lib/puppeteer/js_handle.rb', line 132

def disposed?
  @disposed
end

#evaluate(page_function, *args) ⇒ Object

Parameters:

  • page_function (String)

Returns:

  • (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

Parameters:

  • page_function (String)
  • args (Array<*>)

Returns:



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_contextPuppeteer::ExecutionContext



45
46
47
# File 'lib/puppeteer/js_handle.rb', line 45

def execution_context
  @context
end

#inspectObject



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_valueObject



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

#propertiesHash<String, JSHandle>

getProperties in JavaScript.

Returns:



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

Parameters:

  • name (String)

Returns:



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_sObject



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