Puppeteer 執(zhí)行上下文

2020-06-29 14:12 更新

class:executioncontext

class: ExecutionContext v0.9.0

該類表示一個 JavaScript 執(zhí)行的上下文。 Page 可能有許多執(zhí)行上下文: 每個 frame 都有 "默認" 的執(zhí)行上下文,它始終在將幀附加到 DOM 后創(chuàng)建。該上下文由 frame.executionContext() 方法返回。 Extensions 的內容腳本創(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 的函數返回一個Promise,那么 executionContext.evaluate 將等待承諾解析并返回它的值。

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

入參可以是一個字符串,但不能是函數。

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

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

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

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

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

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

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

入參可以是一個字符串,但不能是函數。

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

JSHandle 實例可以作為參數傳遞給

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

executionContext.frame()v0.9.0

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

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

executionContext.queryObjects(prototypeHandle)v0.9.0

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號