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:



30
31
32
33
34
35
# File 'lib/puppeteer/js_handle.rb', line 30

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.



37
38
39
# File 'lib/puppeteer/js_handle.rb', line 37

def context
  @context
end

#remote_objectObject (readonly)

Returns the value of attribute remote_object.



37
38
39
# File 'lib/puppeteer/js_handle.rb', line 37

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
23
24
25
# File 'lib/puppeteer/js_handle.rb', line 7

def self.create(context:, remote_object:)
  frame = context.frame
  if remote_object.sub_type == 'node' && frame
    frame_manager = frame.frame_manager
    Puppeteer::ElementHandle.new(
      context: context,
      client: context.client,
      remote_object: remote_object,
      page: frame_manager.page,
      frame_manager: frame_manager,
    )
  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:



81
82
83
# File 'lib/puppeteer/js_handle.rb', line 81

def [](name)
  property(name)
end

#as_elementObject



116
117
118
# File 'lib/puppeteer/js_handle.rb', line 116

def as_element
  nil
end

#disposeObject



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

def dispose
  return if @disposed

  @disposed = true
  @remote_object.release(@client)
end

#disposed?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/puppeteer/js_handle.rb', line 127

def disposed?
  @disposed
end

#evaluate(page_function, *args) ⇒ Object

Parameters:

  • page_function (String)

Returns:

  • (Object)


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

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:



55
56
57
# File 'lib/puppeteer/js_handle.rb', line 55

def evaluate_handle(page_function, *args)
  execution_context.evaluate_handle(page_function, self, *args)
end

#execution_contextPuppeteer::ExecutionContext



40
41
42
# File 'lib/puppeteer/js_handle.rb', line 40

def execution_context
  @context
end

#json_valueObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puppeteer/js_handle.rb', line 98

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:



87
88
89
90
91
92
93
94
95
96
# File 'lib/puppeteer/js_handle.rb', line 87

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:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puppeteer/js_handle.rb', line 64

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



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/puppeteer/js_handle.rb', line 131

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