feat: add exposeInIsolatedWorld(worldId, key, api) to contextBridge (#34974)

* feat: add exposeInIsolatedWorld(worldId, key, api) to contextBridge

* Updates exposeInIslatedWorld worldId documentation
This commit is contained in:
Akshay Deo 2022-09-21 23:47:10 +05:30 committed by GitHub
parent 8c3c0f0b50
commit dfc134de42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 21 deletions

View file

@ -46,6 +46,12 @@ The `contextBridge` module has the following methods:
* `apiKey` string - The key to inject the API onto `window` with. The API will be accessible on `window[apiKey]`.
* `api` any - Your API, more information on what this API can be and how it works is available below.
### `contextBridge.exposeInIsolatedWorld(worldId, apiKey, api)`
* `worldId` Integer - The ID of the world to inject the API into. `0` is the default world, `999` is the world used by Electron's `contextIsolation` feature. Using 999 would expose the object for preload context. We recommend using 1000+ while creating isolated world.
* `apiKey` string - The key to inject the API onto `window` with. The API will be accessible on `window[apiKey]`.
* `api` any - Your API, more information on what this API can be and how it works is available below.
## Usage
### API
@ -84,6 +90,26 @@ contextBridge.exposeInMainWorld(
)
```
An example of `exposeInIsolatedWorld` is shown below:
```javascript
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInIsolatedWorld(
1004,
'electron',
{
doThing: () => ipcRenderer.send('do-a-thing')
}
)
```
```javascript
// Renderer (In isolated world id1004)
window.electron.doThing()
```
### API Functions
`Function` values that you bind through the `contextBridge` are proxied through Electron to ensure that contexts remain isolated. This