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() 的函数返回一个非 Serializable 值,那么 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.
用法
await worker.evaluate(pageFunction);
await worker.evaluate(pageFunction, arg);
参数
-
pageFunctionfunction | string#在工作环境中评估的功能。
-
argEvaluationArgument (optional)#可选择的参数,可传递给 pageFunction。
返回
evaluateHandle
Added before v1.9以 JSHandle 的形式返回 pageFunction 的返回值。
🌐 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.
用法
await worker.evaluateHandle(pageFunction);
await worker.evaluateHandle(pageFunction, arg);
参数
-
pageFunctionfunction | string#在工作环境中评估的功能。
-
argEvaluationArgument (optional)#可选择的参数,可传递给 pageFunction。
返回
url
Added before v1.9用法
worker.url();
返回
waitForEvent
Added in: v1.57等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。如果在事件触发前页面被关闭,将抛出错误。返回事件数据的值。
🌐 Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the page is closed before the event is fired. Returns the event data value.
用法
// Start waiting for download before clicking. Note no await.
const consolePromise = worker.waitForEvent('console');
await worker.evaluate('console.log(42)');
const consoleMessage = await consolePromise;
参数
-
事件名称,通常传递到
*.on(event)的那个相同名称。 -
optionsOrPredicatefunction | Object (optional)#-
predicatefunction接收事件数据并在等待解决时解析为真值。
-
timeoutnumber (optional)等待的最长时间,以毫秒为单位。默认值为
0——无超时。默认值可以通过配置中的actionTimeout选项更改,或使用 browserContext.setDefaultTimeout() 或 page.setDefaultTimeout() 方法更改。
可以是接收事件的谓词,也可以是一个选项对象。可选。
-
-
optionsObject (optional)
返回
事件
🌐 Events
on('close')
Added before v1.9当这个专用的 WebWorker 被终止时触发。
🌐 Emitted when this dedicated WebWorker is terminated.
用法
worker.on('close', data => {});
事件数据
on('console')
Added in: v1.57当 worker 中的 JavaScript 调用某个 console API 方法时触发,例如 console.log 或 console.dir。
🌐 Emitted when JavaScript within the worker calls one of console API methods, e.g. console.log or console.dir.
用法
worker.on('console', data => {});
事件数据