Class: Puppeteer::JSHandle

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

Direct Known Subclasses

ElementHandle

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of JSHandle.

Parameters:



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

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.



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

def context
  @context
end

#remote_objectObject (readonly)

Returns the value of attribute remote_object.



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

def remote_object
  @remote_object
end

Class Method Details

.create(context:, remote_object:) ⇒ Object

Parameters:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppeteer/js_handle.rb', line 6

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

#as_elementObject



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

def as_element
  nil
end

#async_evaluateFuture<Object>

Parameters:

  • page_function (String)

Returns:

  • (Future<Object>)


51
52
53
# File 'lib/puppeteer/js_handle.rb', line 51

async def async_evaluate(page_function, *args)
  evaluate(page_function, *args)
end

#async_evaluate_handleFuture<Puppeteer::JSHandle>

Parameters:

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

Returns:



65
66
67
# File 'lib/puppeteer/js_handle.rb', line 65

async def async_evaluate_handle(page_function, *args)
  evaluate_handle(page_function, *args)
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)


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

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:



58
59
60
# File 'lib/puppeteer/js_handle.rb', line 58

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

#execution_contextPuppeteer::ExecutionContext



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

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) || @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