跳转至

contextBridge

<!-- ``YAML history changes: - pr-url: https://github.com/electron/electron/pull/40330 description: "ipcRenderercan no longer be sent over thecontextBridge`" breaking-changes-header: behavior-changed-ipcrenderer-can-no-longer-be-sent-over-the-contextbridge

-->

> 在隔离上下文之间创建安全、双向、同步的桥接

进程:[渲染器](../glossary.md#renderer-process)

下面是一个在隔离的预加载脚本中向渲染器暴露 API 的示例:

```js
// 预加载(隔离世界)
const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('electron', {
  doThing: () => ipcRenderer.send('do-a-thing')
})

```js @ts-nocheck // 渲染器(主世界)

window.electron.doThing()

## 术语表 {#glossary}

### 主世界 {#main-world}

"主世界"是您的主要渲染器代码运行所在的 JavaScript 上下文。默认情况下,您在渲染器中加载的页面会在此世界中执行代码。

### 隔离世界 {#isolated-world}

当您在 `webPreferences` 中启用了 `contextIsolation`(自 Electron 12.0.0 起这是默认行为)时,您的 `preload` 脚本会在"隔离世界"中运行。您可以在[安全](https://atomgit.com/GitHub_Trending/el/electron/blob/main/docs/tutorial/security.md#3-enable-context-isolation)文档中阅读更多有关上下文隔离及其影响的内容。

## 方法 {#methods}

`contextBridge` 模块有以下方法:

### `contextBridge.exposeInMainWorld(apiKey, api)` {#contextbridgeexposeinmainworldapikey-api}

* `apiKey` string - 用于将 API 注入到 `window` 上的键。API 将可通过 `window[apiKey]` 访问。
* `api` any - 您的 API,有关此 API 可以是什么以及其工作方式的更多信息如下。

### `contextBridge.exposeInIsolatedWorld(worldId, apiKey, api)` {#contextbridgeexposeinisolatedworldworldid-apikey-api}

* `worldId` Integer - 要将 API 注入到的世界的 ID。`0` 是默认世界,`999` 是 Electron 的 `contextIsolation` 功能使用的世界。使用 999 会为 preload 上下文暴露对象。我们建议在创建隔离世界时使用 1000 及以上。
* `apiKey` string - 用于将 API 注入到 `window` 上的键。API 将可通过 `window[apiKey]` 访问。
* `api` any - 您的 API,有关此 API 可以是什么以及其工作方式的更多信息如下。

### `contextBridge.executeInMainWorld(executionScript)` _实验性_ {#contextbridgeexecuteinmainworldexecutionscript-experimental}

<!-- TODO(samuelmaddock): add generics to map the `args` types to the `func` params  -->

* `executionScript` Object
    * `func` (...args: any[]) => any - 要执行的 JavaScript 函数。此函数会被序列化,这意味着任何绑定的参数和执行上下文都将丢失。
    * `args` any[] (可选) - 要传递给所提供函数的参数数组。这些参数将根据[支持的类型表](#parameter--error--return-type-support)在世界之间复制。

返回 `any` - 在主世界中执行函数所得结果的副本。有关值如何在世界之间复制,请[参阅该表](#parameter--error--return-type-support)。

## 使用方法 {#usage}

### API {#api}

提供给 [`exposeInMainWorld`](#contextbridgeexposeinmainworldapikey-api) 的 `api` 必须是一个 `Function`、`string`、`number`、`Array`、`boolean`,或一个键为字符串、值为 `Function`、`string`、`number`、`Array`、`boolean` 或另一个满足相同条件的嵌套对象的对象。

`Function` 值会被代理到另一个上下文,所有其他值都会被**复制**并**冻结**。API 中发送的任何数据/原始值都将变为不可变,并且桥接任一侧的更新不会导致另一侧的更新。

下面是一个复杂 API 的示例:

```js
const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInMainWorld('electron', {
  doThing: () => ipcRenderer.send('do-a-thing'),
  myPromises: [Promise.resolve(), Promise.reject(new Error('whoops'))],
  anAsyncFunction: async () => 123,
  data: {
    myFlags: ['a', 'b', 'c'],
    bootTime: 1234
  },
  nestedAPI: {
    evenDeeper: {
      youCanDoThisAsMuchAsYouWant: {
        fn: () => ({
          returnData: 123
        })
      }
    }
  }
})

下面是一个 exposeInIsolatedWorld 的示例:

const { contextBridge, ipcRenderer } = require('electron')

contextBridge.exposeInIsolatedWorld(1004, 'electron', {
  doThing: () => ipcRenderer.send('do-a-thing')
})

```js @ts-nocheck // 渲染器(隔离世界 id1004)

window.electron.doThing()

### API 函数 {#api-functions}

通过 `contextBridge` 绑定的 `Function` 值会经由 Electron 进行代理,以确保上下文保持隔离。这带来了我们在下面概述的一些关键限制。

#### 参数 / 错误 / 返回值支持 {#parameter--error--return-type-support}

由于参数、错误和返回值在通过桥接发送时会被**复制**,因此只能使用某些类型。总的来说,如果您想使用的类型可以被序列化和反序列化为同一个对象,那么它就能正常工作。为完整起见,下面附上类型支持表:

| 类型 | 复杂度 | 参数支持 | 返回值支持 | 限制 |
| ---- | ---------- | ----------------- | -------------------- | ----------- |
| `string` | 简单 | ✅ | ✅ | 无 |
| `number` | 简单 | ✅ | ✅ | 无 |
| `boolean` | 简单 | ✅ | ✅ | 无 |
| `Object` | 复杂 | ✅ | ✅ | 键必须仅使用此表中的"简单"类型。值必须受此表支持。原型修改会被丢弃。发送自定义类会复制值,但不会复制原型。 |
| `Array` | 复杂 | ✅ | ✅ | 与 `Object` 类型相同的限制 |
| `Error` | 复杂 | ✅ | ✅ | 抛出的错误也会被复制,这可能导致错误的消息和堆栈跟踪因在不同上下文中抛出而发生轻微变化,并且 Error 对象上的任何自定义属性[都将丢失](https://github.com/electron/electron/issues/25596) |
| `Promise` | 复杂 | ✅ | ✅ | 无 |
| `Function` | 复杂 | ✅ | ✅ | 原型修改会被丢弃。发送类或构造函数将无法工作。 |
| [可克隆类型](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) | 简单 | ✅ | ✅ | 请参阅有关可克隆类型的链接文档 |
| `Element` | 复杂 | ✅ | ✅ | 原型修改会被丢弃。发送自定义元素将无法工作。 |
| `Blob` | 复杂 | ✅ | ✅ | 不适用 |
| `VideoFrame` | 复杂 | ✅ | ✅ | 不适用 |
| `Symbol` | 不适用 | ❌ | ❌ | 符号无法在不同上下文之间复制,因此会被丢弃 |

如果你关心的类型不在上表中,那么它很可能不受支持。

### 暴露 ipcRenderer {#exposing-ipcrenderer}

尝试将整个 `ipcRenderer` 模块作为对象通过 `contextBridge` 发送时,桥接的接收方会得到一个空对象。完整发送 `ipcRenderer` 会让任意代码发送任意消息,这是一个安全隐患。要通过 `ipcRenderer` 进行交互,请像下面这样提供一个安全包装器:

```js
// 预加载(隔离世界)
contextBridge.exposeInMainWorld('electron', {
  onMyEventName: (callback) => ipcRenderer.on('MyEventName', (e, ...args) => callback(args))
})

```js @ts-nocheck // 渲染进程(主世界) window.electron.onMyEventName((data) => { /* ... */ })

### 暴露 Node 全局符号 {#exposing-node-global-symbols}

预加载脚本可以使用 `contextBridge` 让你的渲染进程访问 Node API。上文所述的支持类型表同样适用于你通过 `contextBridge` 暴露的 Node API。请注意,许多 Node API 会授予对本地系统资源的访问权限。对于向不受信任的远程内容暴露哪些全局变量和 API,请务必非常谨慎。

```js
const { contextBridge } = require('electron')

const crypto = require('node:crypto')

contextBridge.exposeInMainWorld('nodeCrypto', {
  sha256sum(data) {
    const hash = crypto.createHash('sha256')
    hash.update(data)
    return hash.digest('hex')
  }
})

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron