Class: Puppeteer::ExecutionContext

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

Defined Under Namespace

Classes: EvaluationError, JavaScriptExpression, JavaScriptFunction

Constant Summary collapse

EVALUATION_SCRIPT_URL =
'__puppeteer_evaluation_script__'
SOURCE_URL_REGEX =
/^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Constructor Details

#initialize(client, context_payload, world) ⇒ ExecutionContext

Returns a new instance of ExecutionContext.

Parameters:



11
12
13
14
15
16
# File 'lib/puppeteer/execution_context.rb', line 11

def initialize(client, context_payload, world)
  @client = client
  @world = world
  @context_id = context_payload['id']
  @context_name = context_payload['name']
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/puppeteer/execution_context.rb', line 18

def client
  @client
end

#worldObject (readonly)

Returns the value of attribute world.



18
19
20
# File 'lib/puppeteer/execution_context.rb', line 18

def world
  @world
end

Instance Method Details

#adopt_backend_node_id(backend_node_id) ⇒ Puppeteer::ElementHandle

Parameters:

  • backend_node_id (Integer)

Returns:



227
228
229
230
231
232
233
234
235
236
# File 'lib/puppeteer/execution_context.rb', line 227

def adopt_backend_node_id(backend_node_id)
  response = @client.send_message('DOM.resolveNode',
    backendNodeId: backend_node_id,
    executionContextId: @context_id,
  )
  Puppeteer::JSHandle.create(
    context: self,
    remote_object: Puppeteer::RemoteObject.new(response["object"]),
  )
end

#adopt_element_handle(element_handle) ⇒ Puppeteer::ElementHandle

Parameters:

Returns:



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/puppeteer/execution_context.rb', line 241

def adopt_element_handle(element_handle)
  if element_handle.execution_context == self
    raise ArgumentError.new('Cannot adopt handle that already belongs to this execution context')
  end

  unless @world
    raise 'Cannot adopt handle without DOMWorld'
  end

  node_info = element_handle.remote_object.node_info(@client)
  response = @client.send_message('DOM.resolveNode',
    backendNodeId: node_info["node"]["backendNodeId"],
    executionContextId: @context_id,
  )

  Puppeteer::JSHandle.create(
    context: self,
    remote_object: Puppeteer::RemoteObject.new(response["object"]),
  )
end

#evaluate(page_function, *args) ⇒ Object

Parameters:

  • page_function (String)

Returns:

  • (Object)


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

def evaluate(page_function, *args)
  evaluate_internal(true, page_function, *args)
end

#evaluate_handle(page_function, *args) ⇒ Puppeteer::JSHandle

Parameters:

  • page_function (String)

Returns:



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

def evaluate_handle(page_function, *args)
  evaluate_internal(false, page_function, *args)
end

#framePuppeteer::Frame

Returns:



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

def frame
  if_present(@world) do |world|
    world.frame
  end
end