Worker
Worker 类代表 WebWorker。worker
事件在页面对象上触发,以指示工作线程创建。当工作线程消失时,工作线程对象上会触发 close
事件。
¥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.on('worker', worker => {
console.log('Worker created: ' + worker.url());
worker.on('close', worker => console.log('Worker destroyed: ' + worker.url()));
});
console.log('Current workers:');
for (const worker of page.workers())
console.log(' ' + worker.url());
方法
¥Methods
evaluate
Added before v1.9返回 pageFunction 的返回值。
¥Returns the return value of pageFunction.
如果传递给 worker.evaluate() 的函数返回 Promise,那么 worker.evaluate() 将等待 Promise 解析并返回其值。
¥If the function passed to the worker.evaluate() returns a Promise, then worker.evaluate() would wait for the promise to resolve and return its value.
如果传递给 worker.evaluate() 的函数返回非 可串行化 值,则 worker.evaluate() 返回 undefined
。Playwright 还支持传输一些 JSON
无法序列化的附加值:-0
、NaN
、Infinity
、-Infinity
。
¥If the function passed to the worker.evaluate() returns a non-Serializable value, then worker.evaluate() returns undefined
. Playwright also supports transferring some additional values that are not serializable by JSON
: -0
, NaN
, Infinity
, -Infinity
.
用法
¥Usage
await worker.evaluate(pageFunction);
await worker.evaluate(pageFunction, arg);
参数
¥Arguments
在工作环境中评估的功能。
¥Function to be evaluated in the worker context.
arg
EvaluationArgument (optional)#
传递给 pageFunction 的可选参数。
¥Optional argument to pass to pageFunction.
返回
¥Returns
evaluateHandle
Added before v1.9将 pageFunction 的返回值作为 JSHandle 返回。
¥Returns the return value of pageFunction as a JSHandle.
worker.evaluate() 和 worker.evaluateHandle() 之间的唯一区别是 worker.evaluateHandle() 返回 JSHandle。
¥The only difference between worker.evaluate() and worker.evaluateHandle() is that worker.evaluateHandle() returns JSHandle.
如果传递给 worker.evaluateHandle() 的函数返回 Promise,那么 worker.evaluateHandle() 将等待 Promise 解析并返回其值。
¥If the function passed to the worker.evaluateHandle() returns a Promise, then worker.evaluateHandle() would wait for the promise to resolve and return its value.
用法
¥Usage
await worker.evaluateHandle(pageFunction);
await worker.evaluateHandle(pageFunction, arg);
参数
¥Arguments
在工作环境中评估的功能。
¥Function to be evaluated in the worker context.
arg
EvaluationArgument (optional)#
传递给 pageFunction 的可选参数。
¥Optional argument to pass to pageFunction.
返回
¥Returns
url
Added before v1.9用法
¥Usage
worker.url();
返回
¥Returns
事件
¥Events
on('close')
Added before v1.9当该专用 WebWorker 终止时触发。
¥Emitted when this dedicated WebWorker is terminated.
用法
¥Usage
worker.on('close', data => {});
事件数据
¥Event data