puppeteer/docs/api/puppeteer.webworker.md
2024-04-29 14:50:39 +02:00

3.3 KiB

sidebar_label
WebWorker

WebWorker class

This class represents a WebWorker.

Signature:

export declare abstract class WebWorker extends EventEmitter<Record<EventType, unknown>>

Extends: EventEmitter<Record<EventType, unknown>>

Remarks

The events workercreated and workerdestroyed are emitted on the page object to signal the worker lifecycle.

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the WebWorker class.

Example

page.on('workercreated', worker =>
  console.log('Worker created: ' + worker.url())
);
page.on('workerdestroyed', worker =>
  console.log('Worker destroyed: ' + worker.url())
);

console.log('Current workers:');
for (const worker of page.workers()) {
  console.log('  ' + worker.url());
}

Properties

Property

Modifiers

Type

Description

client

readonly

CDPSession

The CDP session client the WebWorker belongs to.

Methods

Method

Modifiers

Description

close()

evaluate(func, args)

Evaluates a given function in the worker.

Remarks:

If the given function returns a promise, evaluate will wait for the promise to resolve.

As a rule of thumb, if the return value of the given function is more complicated than a JSON object (e.g. most classes), then evaluate will _likely_ return some truncated value (or {}). This is because we are not returning the actual return value, but a deserialized version as a result of transferring the return value through a protocol to Puppeteer.

In general, you should use evaluateHandle if evaluate cannot serialize the return value properly or you need a mutable handle to the return object.

evaluateHandle(func, args)

Evaluates a given function in the worker.

Remarks:

If the given function returns a promise, evaluate will wait for the promise to resolve.

In general, you should use evaluateHandle if evaluate cannot serialize the return value properly or you need a mutable handle to the return object.

url()

The URL of this web worker.