Skip to content

WebWorker

Namespace: PuppeteerSharp

public abstract class WebWorker

The Worker class represents a WebWorker (<a href=“https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API&lt;/a>). The events WorkerCreated and WorkerDestroyed are emitted on the page object to signal the worker lifecycle.

Inheritance: object

Derived: CdpWebWorker

Example

page.WorkerCreated += (sender, e) => Console.WriteLine('Worker created: ' + e.Worker.Url);
page.WorkerDestroyed += (sender, e) => Console.WriteLine('Worker destroyed: ' + e.Worker.Url);
for (var worker of page.Workers)
{
Console.WriteLine(' ' + worker.Url);
}

View source

public abstract ICDPSession Client { get; }

The CDP session client the WebWorker belongs to.

Value: ICDPSession

View source

public string Url { get; }

Gets the Worker URL.

Value: string — Worker URL.

View source

public event EventHandler<ConsoleEventArgs> Console

Fired when the worker calls a console API method.

Value: EventHandler<ConsoleEventArgs>

View source

public abstract Task CloseAsync()

Closes the worker.

Returns: Task — A Task that completes when the worker is closed.

View source

public virtual Task<T> EvaluateExpressionAsync<T>(string script)

Executes a script in browser context.

Type parameters

Name Description
T The type to deserialize the result to.

Parameters

Name Type Description
script string Script to be evaluated in browser context.

Returns: Task<T> — Task which resolves to script return value.

If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.

View source

public virtual Task<IJSHandle> EvaluateExpressionHandleAsync(string script)

Executes a script in browser context.

Parameters

Name Type Description
script string Script to be evaluated in browser context.

Returns: Task<IJSHandle> — Task which resolves to script return value.

If the script, returns a Promise, then the method would wait for the promise to resolve and return its value.

View source

EvaluateFunctionAsync(string, params object[])

Section titled “EvaluateFunctionAsync(string, params object[])”
public virtual Task EvaluateFunctionAsync(string script, params object[] args)

Executes a function in browser context.

Parameters

Name Type Description
script string Script to be evaluated in browser context.
args object[] Arguments to pass to script.

Returns: Task — Task which resolves to script return value.

If the script, returns a Promise, then the method would wait for the promise to resolve and return its value. IJSHandle instances can be passed as arguments.

View source

EvaluateFunctionAsync<T>(string, params object[])

Section titled “EvaluateFunctionAsync<T>(string, params object[])”
public virtual Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)

Executes a function in the context.

Type parameters

Name Description
T The type to deserialize the result to.

Parameters

Name Type Description
script string Script to be evaluated in browser context.
args object[] Arguments to pass to script.

Returns: Task<T> — Task which resolves to script return value.

If the script, returns a Promise, then the method would wait for the promise to resolve and return its value. IJSHandle instances can be passed as arguments.

View source

WaitForFunctionAsync(string, WaitForFunctionOptions, params object[])

Section titled “WaitForFunctionAsync(string, WaitForFunctionOptions, params object[])”
public virtual Task<IJSHandle> WaitForFunctionAsync(string script, WaitForFunctionOptions options = null, params object[] args)

Waits for the provided function, <code class=“paramref”>script</code>, to return a truthy value when evaluated in the worker’s context.

Parameters

Name Type Description
script string Function to be evaluated in the worker context until it returns a truthy value.
options WaitForFunctionOptions Options for configuring waiting behavior.
args object[] Arguments to pass to <code class=“paramref”>script</code>.

Returns: Task<IJSHandle> — A Task that resolves to a IJSHandle of the truthy value returned by the function.

View source

Made with love by Dario Kondratiuk