Class: Puppeteer::ExecutionContext::JavaScriptFunction

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

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.



101
102
103
104
105
106
# File 'lib/puppeteer/execution_context.rb', line 101

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:



111
112
113
114
115
116
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
# File 'lib/puppeteer/execution_context.rb', line 111

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 = client.send_message('Runtime.callFunctionOn',
    functionDeclaration: "#{@expression}\n#{suffix}\n",
    executionContextId: context_id,
    arguments: converted_args,
    returnByValue: @return_by_value,
    awaitPromise: true,
    userGesture: true,
  ) # .catch(rewriteError);

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

  if exception_details
    raise EvaluationError.new("Evaluation failed: #{exception_details}")
  end

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