Puppeteer 執(zhí)行上下文

2020-06-29 14:12 更新

class:executioncontext

class: ExecutionContext v0.9.0

該類表示一個 JavaScript 執(zhí)行的上下文。 Page 可能有許多執(zhí)行上下文: 每個 frame 都有 "默認(rèn)" 的執(zhí)行上下文,它始終在將幀附加到 DOM 后創(chuàng)建。該上下文由 frame.executionContext() 方法返回。 Extensions 的內(nèi)容腳本創(chuàng)建了其他執(zhí)行上下文。 除了頁面,執(zhí)行上下文可以在 workers 中找到。

Methods

  • executionContext.evaluate(pageFunction, ...args)v0.9.0
  • executionContext.evaluateHandle(pageFunction, ...args)v0.9.0
  • executionContext.frame()v0.9.0
  • executionContext.queryObjects(prototypeHandle)v0.9.0

Methods

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

  • pageFunction <function|string> Function to be evaluated in executionContext
  • ...args <...Serializable|JSHandle> Arguments to pass to pageFunction
  • returns: <Promise<Serializable>> Promise which resolves to the return value of pageFunction 如果傳遞給 executionContext.evaluate 的函數(shù)返回一個Promise,那么 executionContext.evaluate 將等待承諾解析并返回它的值。

const executionContext = await page.mainFrame().executionContext();
const result = await executionContext.evaluate(() = >Promise.resolve(8 * 7));
console.log(result); // 輸出 "56"

入?yún)⒖梢允且粋€字符串,但不能是函數(shù)。

console.log(await executionContext.evaluate('1 + 2')); // 輸出 "3"

JSHandle 實例可以作為參數(shù)傳遞給 executionContext.evaluate:

oneHandle = await executionContext.evaluateHandle(() = >1);
const twoHandle = await executionContext.evaluateHandle(() = >2);
const result = await executionContext.evaluate((a, b) = >a + b, oneHandle, twoHandle);
await oneHandle.dispose();
await twoHandle.dispose();
console.log(result); // 輸出 '3'

executionContext.evaluateHandle(pageFunction, ...args)v0.9.0

  • pageFunction <function|string> 函數(shù)在 executionContext 中被運行
  • ...args <...Serializable|JSHandle> 傳遞給 pageFunction 的參數(shù)
  • returns: <Promise<JSHandle>> Promise which resolves to the return value of pageFunction as in-page object (JSHandle)

executionContext.evaluate 和 executionContext.evaluateHandle 唯一的區(qū)別在于executionContext.evaluateHandle 會返回頁內(nèi)對象(JSHandle)。 如果傳遞給 executionContext.evaluateHandle 的函數(shù)返回一個 Promise,那么executionContext.evaluateHandle將等待承諾解析并返回它的值。

const context = await page.mainFrame().executionContext();
const aHandle = await context.evaluateHandle(() => Promise.resolve(self));aHandle; // 處理全局對象

入?yún)⒖梢允且粋€字符串,但不能是函數(shù)。

const aHandle = await context.evaluateHandle('1 + 2'); // 處理'3'對象

JSHandle 實例可以作為參數(shù)傳遞給

executionContext.evaluateHandle:const aHandle = await context.evaluateHandle(() = >document.body);
const resultHandle = await context.evaluateHandle(body = >body.innerHTML, aHandle);
console.log(await resultHandle.jsonValue()); // 輸出 body 的 innerHTMLawait aHandle.dispose();await resultHandle.dispose();

executionContext.frame()v0.9.0

returns: <?Frame> 與此執(zhí)行上下文相關(guān)的框架。

注意 并非每個執(zhí)行的上下文都與框架有關(guān)系。 例如,workers 和擴(kuò)展程序具有與框架無關(guān)的執(zhí)行上下文。

executionContext.queryObjects(prototypeHandle)v0.9.0

  • prototypeHandle <JSHandle> 對象原型的句柄
  • returns: <JSHandle> 這個原型的一個對象數(shù)組的句柄 該方法重復(fù)查找 JavaScript 堆,找到具有給定原型的所有對象。
    // 創(chuàng)建一個 Map 對象
    await page.evaluate(() = >window.map = new Map()); // 獲取 Map 對象原型的句柄
    const mapPrototype = await page.evaluateHandle(() = >Map.prototype); // 將所有映射實例查詢到一個數(shù)組中
    const mapInstances = await page.queryObjects(mapPrototype); // 計算堆中映射對象的數(shù)量
    const count = await page.evaluate(maps = >maps.length, mapInstances);
    await mapInstances.dispose();
    await mapPrototype.dispose();
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號