# class:executioncontext
### class: ExecutionContext v0.9.0
該類表示一個 JavaScript 執行的上下文。 [Page](#?product=Puppeteer&version=v1.11.0&show=api-class-page "Page") 可能有許多執行上下文:
- 每個 [frame](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) 都有 "默認" 的執行上下文,它始終在將幀附加到 DOM 后創建。該上下文由 [`frame.executionContext()`](#?product=Puppeteer&version=v1.11.0&show=api-frameexecutioncontext) 方法返回。
- [Extensions](https://developer.chrome.com/extensions) 的內容腳本創建了其他執行上下文。
除了頁面,執行上下文可以在 [workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) 中找到。
#### Methods
- [executionContext.evaluate(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextevaluatepagefunction-args)v0.9.0
- [executionContext.evaluateHandle(pageFunction, ...args)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextevaluatehandlepagefunction-args)v0.9.0
- [executionContext.frame()](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextframe)v0.9.0
- [executionContext.queryObjects(prototypeHandle)](#?product=Puppeteer&version=v1.11.0&show=api-executioncontextqueryobjectsprototypehandle)v0.9.0
### Methods
#### executionContext.evaluate(pageFunction, ...args)v0.9.0
- `pageFunction` <[function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function")|[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> Function to be evaluated in `executionContext`
- `...args` <...[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")|[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> Arguments to pass to `pageFunction`
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")>> Promise which resolves to the return value of `pageFunction`
如果傳遞給 `executionContext.evaluate` 的函數返回一個[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"),那么 `executionContext.evaluate` 將等待承諾解析并返回它的值。
```
const executionContext = await page.mainFrame().executionContext();const result = await executionContext.evaluate(() => Promise.resolve(8 * 7));console.log(result); // 輸出 "56"
```
入參可以是一個字符串,但不能是函數。
```
console.log(await executionContext.evaluate('1 + 2')); // 輸出 "3"
```
[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle") 實例可以作為參數傳遞給 `executionContext.evaluate`:
```
const 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function")|[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "String")> 函數在 `executionContext` 中被運行
- `...args` <...[Serializable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable")|[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> 傳遞給 `pageFunction` 的參數
- returns: <[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise")<[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")>> Promise which resolves to the return value of `pageFunction` as in-page object (JSHandle)
`executionContext.evaluate` 和 `executionContext.evaluateHandle` 唯一的區別在于`executionContext.evaluateHandle` 會返回頁內對象(JSHandle)。
如果傳遞給 `executionContext.evaluateHandle` 的函數返回一個 [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"),那么`executionContext.evaluateHandle`將等待承諾解析并返回它的值。
```
const context = await page.mainFrame().executionContext();const aHandle = await context.evaluateHandle(() => Promise.resolve(self));aHandle; // 處理全局對象
```
入參可以是一個字符串,但不能是函數。
```
const aHandle = await context.evaluateHandle('1 + 2'); // 處理'3'對象
```
[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle") 實例可以作為參數傳遞給 `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](#?product=Puppeteer&version=v1.11.0&show=api-class-frame "Frame")> 與此執行上下文相關的框架。
> **注意** 并非每個執行的上下文都與框架有關系。 例如,workers 和擴展程序具有與框架無關的執行上下文。
#### executionContext.queryObjects(prototypeHandle)v0.9.0
- `prototypeHandle` <[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> 對象原型的句柄
- returns: <[JSHandle](#?product=Puppeteer&version=v1.11.0&show=api-class-jshandle "JSHandle")> 這個原型的一個對象數組的句柄
該方法重復查找 JavaScript 堆,找到具有給定原型的所有對象。
```
// 創建一個 Map 對象await page.evaluate(() => window.map = new Map());// 獲取 Map 對象原型的句柄const mapPrototype = await page.evaluateHandle(() => Map.prototype);// 將所有映射實例查詢到一個數組中const mapInstances = await page.queryObjects(mapPrototype);// 計算堆中映射對象的數量const count = await page.evaluate(maps => maps.length, mapInstances);await mapInstances.dispose();await mapPrototype.dispose();
```

puppeteer.js中文網|class:executioncontext
puppeteer.js中文文檔, puppeteer chrome, puppeteer firefox, puppeteer api 中文文檔
puppeteer.js中文網包含了Puppeteer中文文檔,最新資訊,應用案例等。Puppeteer 是一個 Node 庫,它提供了一個高級 API 來通過 DevTools 協議控制 Chromium 或 Chrome。
- Introduction
- 版本記錄
- 概要
- puppeteer-vs-puppeteer-core
- 環境變量
- error-handling
- working-with-chrome-extensions
- class:puppeteer
- class:browserfetcher
- class:browser
- class:page
- class:worker
- class:accessibility
- class:keyboard
- class:mouse
- class:touchscreen
- class:tracing
- class:dialog
- class:consolemessage
- class:frame
- class:executioncontext
- class:jshandle
- class:elementhandle
- class:request
- class:response
- class:securitydetails
- class:target
- class:cdpsession
- class:coverage
- class:timeouterror