class: Worker
The Worker class represents a WebWorker.
The events workercreated
and workerdestroyed
are emitted on the page object to signal the worker lifecycle.
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());
worker.evaluate(pageFunction[, ...args])
pageFunction
<[function]|[string]> Function to be evaluated in the worker context...args
<...[Serializable]|[JSHandle]> Arguments to pass topageFunction
- returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of
pageFunction
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.
If the function passed to the worker.evaluate
returns a non-[Serializable] value, then worker.evaluate
resolves to undefined
. DevTools Protocol also supports transferring some additional values that are not serializable by JSON
: -0
, NaN
, Infinity
, -Infinity
, and bigint literals.
Shortcut for (await worker.executionContext()).evaluate(pageFunction, ...args).
worker.evaluateHandle(pageFunction[, ...args])
pageFunction
<[function]|[string]> Function to be evaluated in the page context...args
<...[Serializable]|[JSHandle]> Arguments to pass topageFunction
- returns: <[Promise]<[JSHandle]>> Promise which resolves to the return value of
pageFunction
as in-page object (JSHandle)
The only difference between worker.evaluate
and worker.evaluateHandle
is that worker.evaluateHandle
returns in-page object (JSHandle).
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.
Shortcut for (await worker.executionContext()).evaluateHandle(pageFunction, ...args).
worker.executionContext()
- returns: <[Promise]<[ExecutionContext]>>
worker.url()
- returns: <[string]>
The Worker class represents a WebWorker.
The events workercreated
and workerdestroyed
are emitted on the page object to signal the worker lifecycle.
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());
worker.evaluate(pageFunction[, ...args])
pageFunction
<[function]|[string]> Function to be evaluated in the worker context...args
<...[Serializable]|[JSHandle]> Arguments to pass topageFunction
- returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of
pageFunction
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.
If the function passed to the worker.evaluate
returns a non-[Serializable] value, then worker.evaluate
resolves to undefined
. DevTools Protocol also supports transferring some additional values that are not serializable by JSON
: -0
, NaN
, Infinity
, -Infinity
, and bigint literals.
Shortcut for (await worker.executionContext()).evaluate(pageFunction, ...args).
worker.evaluateHandle(pageFunction[, ...args])
pageFunction
<[function]|[string]> Function to be evaluated in the page context...args
<...[Serializable]|[JSHandle]> Arguments to pass topageFunction
- returns: <[Promise]<[JSHandle]>> Promise which resolves to the return value of
pageFunction
as in-page object (JSHandle)
The only difference between worker.evaluate
and worker.evaluateHandle
is that worker.evaluateHandle
returns in-page object (JSHandle).
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.
Shortcut for (await worker.executionContext()).evaluateHandle(pageFunction, ...args).
worker.executionContext()
- returns: <[Promise]<[ExecutionContext]>>
worker.url()
- returns: <[string]>
The Worker class represents a WebWorker.
The events workercreated
and workerdestroyed
are emitted on the page object to signal the worker lifecycle.
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());
worker.evaluate(pageFunction[, ...args])
pageFunction
<[function]|[string]> Function to be evaluated in the worker context...args
<...[Serializable]|[JSHandle]> Arguments to pass topageFunction
- returns: <[Promise]<[Serializable]>> Promise which resolves to the return value of
pageFunction
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.
If the function passed to the worker.evaluate
returns a non-[Serializable] value, then worker.evaluate
resolves to undefined
. DevTools Protocol also supports transferring some additional values that are not serializable by JSON
: -0
, NaN
, Infinity
, -Infinity
, and bigint literals.
Shortcut for (await worker.executionContext()).evaluate(pageFunction, ...args).
worker.evaluateHandle(pageFunction[, ...args])
pageFunction
<[function]|[string]> Function to be evaluated in the page context...args
<...[Serializable]|[JSHandle]> Arguments to pass topageFunction
- returns: <[Promise]<[JSHandle]>> Promise which resolves to the return value of
pageFunction
as in-page object (JSHandle)
The only difference between worker.evaluate
and worker.evaluateHandle
is that worker.evaluateHandle
returns in-page object (JSHandle).
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.
Shortcut for (await worker.executionContext()).evaluateHandle(pageFunction, ...args).
worker.executionContext()
- returns: <[Promise]<[ExecutionContext]>>
worker.url()
- returns: <[string]>
Worker 类代表 WebWorker。
在页面对象上发出事件workercreated
和workerdestroyed
,以指示工作程序生命周期。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。