Skip to main content

Worker

The Worker class represents a WebWorker. worker event is emitted on the page object to signal a worker creation. close event is emitted on the worker object when the worker is gone.

page.Worker += (_, worker) =>
{
Console.WriteLine($"Worker created: {worker.Url}");
worker.Close += (_, _) => Console.WriteLine($"Worker closed {worker.Url}");
};

Console.WriteLine("Current Workers:");
foreach(var pageWorker in page.Workers)
{
Console.WriteLine($"\tWorker: {pageWorker.Url}");
}

方法 (Methods)

EvaluateAsync

Added before v1.9 worker.EvaluateAsync

Returns the return value of expression.

If the function passed to the Worker.EvaluateAsync() returns a Promise, then Worker.EvaluateAsync() would wait for the promise to resolve and return its value.

If the function passed to the Worker.EvaluateAsync() returns a non-Serializable value, then Worker.EvaluateAsync() returns undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.

使用方式

await Worker.EvaluateAsync(expression, arg);

參數

  • expression string#

    JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.

  • arg EvaluationArgument? (optional)#

    Optional argument to pass to expression.

傳回值

  • [object]#

EvaluateHandleAsync

Added before v1.9 worker.EvaluateHandleAsync

Returns the return value of expression as a JSHandle.

The only difference between Worker.EvaluateAsync() and Worker.EvaluateHandleAsync() is that Worker.EvaluateHandleAsync() returns JSHandle.

If the function passed to the Worker.EvaluateHandleAsync() returns a Promise, then Worker.EvaluateHandleAsync() would wait for the promise to resolve and return its value.

使用方式

await Worker.EvaluateHandleAsync(expression, arg);

參數

  • expression string#

    JavaScript expression to be evaluated in the browser context. If the expression evaluates to a function, the function is automatically invoked.

  • arg EvaluationArgument? (optional)#

    Optional argument to pass to expression.

傳回值


Url

Added before v1.9 worker.Url

使用方式

Worker.Url

傳回值


事件 (Events)

event Close

Added before v1.9 worker.event Close

Emitted when this dedicated WebWorker is terminated.

使用方式

Worker.Close += async (_, worker) => {};

事件資料