Class: Puppeteer::ExecutionContext::JavaScriptFunction

Inherits:
Object
  • Object
show all
Includes:
IfPresent
Defined in:
lib/puppeteer/execution_context.rb,
sig/puppeteer/execution_context.rbs

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Constructor Details

#initialize(execution_context, expression, args, return_by_value) ⇒ JavaScriptFunction

Returns a new instance of JavaScriptFunction.

Parameters:

  • execution_context (Object)
  • expression (Object)
  • args (Object)
  • return_by_value (Object)


107
108
109
110
111
112
# File 'lib/puppeteer/execution_context.rb', line 107

def initialize(execution_context, expression, args, return_by_value)
  @execution_context = execution_context
  @expression = expression
  @return_by_value = return_by_value
  @args = args
end

Instance Method Details

#evaluate_with(client:, context_id:) ⇒ Object|JSHandle

Parameters:

Returns:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/puppeteer/execution_context.rb', line 117

def evaluate_with(client:, context_id:)
  # `function` can be omitted in JS after ES2015.
  # https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Object_initializer
  #
  # Original puppeteer implementation take it into consideration.
  # But we don't support the syntax here.

  result =
    begin
      client.send_message('Runtime.callFunctionOn',
        functionDeclaration: function_declaration,
        executionContextId: context_id,
        arguments: converted_args,
        returnByValue: @return_by_value,
        awaitPromise: true,
        userGesture: true,
      )
    rescue Puppeteer::Connection::ProtocolError => err
      raise @execution_context.send(:rewrite_evaluation_error, err)
    end # .catch(rewriteError);

  exception_details = result['exceptionDetails']
  if exception_details
    raise EvaluationError.new("Evaluation failed: #{exception_details}")
  end

  remote_object = Puppeteer::RemoteObject.new(result['result'])

  if @return_by_value
    remote_object.value
  else
    Puppeteer::JSHandle.create(
      context: @execution_context,
      remote_object: remote_object,
    )
  end
end