Puppeteer 工人

2020-06-29 14:27 更新

class:worker

Worker 類表示一個(gè) WebWorker。在頁(yè)面對(duì)象上 workercreated 和 workerdestroyed 事件被觸發(fā),以標(biāo)識(shí) worker 的生命周期。

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());

Methods

  • worker.evaluate(pageFunction, ...args)v0.9.0
  • worker.evaluateHandle(pageFunction, ...args)v0.9.0
  • worker.executionContext()v0.9.0
  • worker.url()v0.9.0

Methods

worker.evaluate(pageFunction, ...args)v0.9.0

  • pageFunction <function|string> Function to be evaluated in the worker context
  • ...args <...Serializable|JSHandle> Arguments to pass to pageFunction
  • returns: <Promise<Serializable>> Promise which resolves to the return value of pageFunction

如果傳遞給 worker.evaluate 的函數(shù)返回一個(gè) Promise,那么 worker.evaluate 將等待解析并返回它的值。 如果傳遞給 worker.evaluate 的函數(shù)返回一個(gè) 非[序列化]的值,那么 worker.evaluate 解析為 undefined。 (await worker.executionContext()).evaluate(pageFunction, ...args) 的快捷鏈接。

worker.evaluateHandle(pageFunction, - ...args)v0.9.0

  • pageFunction <function|string> Function to be evaluated in the page context
  • ...args <...Serializable|JSHandle> Arguments to pass to pageFunction
  • returns: <Promise<JSHandle>> Promise which resolves to the return value of pageFunction as in-page object (JSHandle)

worker.evaluate 和 worker.evaluateHandle 之間的唯一區(qū)別是 worker.evaluateHandle 返回頁(yè)內(nèi)對(duì)象(JSHandle)。 如果傳遞給 worker.evaluateHandle 的函數(shù)返回一個(gè) Promise,那么 worker.evaluateHandle 將等待解析并返回它的值。 (await worker.executionContext()).evaluateHandle(pageFunction, ...args) 的快捷鏈接。

worker.executionContext()v0.9.0 returns: <Promise<ExecutionContext>>

worker.url()v0.9.0 returns: <string>

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)