|
1 | 1 | import { ipcRenderer, contextBridge } from 'electron' |
2 | 2 |
|
3 | 3 | // --------- Expose some API to the Renderer process --------- |
4 | | -contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer)) |
| 4 | +contextBridge.exposeInMainWorld('app', { |
| 5 | + onEvent(cb) { |
| 6 | + const channel = 'main-process-message' |
| 7 | + const channel2 = 'other-ipc-channel' |
5 | 8 |
|
6 | | -// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it. |
7 | | -function withPrototype(obj: Record<string, any>) { |
8 | | - const protos = Object.getPrototypeOf(obj) |
9 | | - |
10 | | - for (const [key, value] of Object.entries(protos)) { |
11 | | - if (Object.prototype.hasOwnProperty.call(obj, key)) continue |
12 | | - |
13 | | - if (typeof value === 'function') { |
14 | | - // Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function. |
15 | | - obj[key] = function (...args: any) { |
16 | | - return value.call(obj, ...args) |
17 | | - } |
18 | | - } else { |
19 | | - obj[key] = value |
20 | | - } |
21 | | - } |
22 | | - return obj |
23 | | -} |
| 9 | + ipcRenderer.on(channel, (_e, ...args) => cb(channel, ...args)) |
| 10 | + ipcRenderer.on(channel2, (_e, ...args) => cb(channel2, ...args)) |
| 11 | + }, |
| 12 | +}) |
24 | 13 |
|
25 | 14 | // --------- Preload scripts loading --------- |
26 | 15 | function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) { |
|
0 commit comments