-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
22 lines (19 loc) · 942 Bytes
/
preload.js
File metadata and controls
22 lines (19 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// see https://www.electronjs.org/docs/latest/tutorial/context-isolation
const { contextBridge, ipcRenderer } = require("electron");
// We need to wait until the renderer window is ready to receive the message before
// sending the port. We create this promise in the preload so it's guaranteed
// to register the onload listener before the load event is fired.
const windowLoaded = new Promise((resolve) => {
window.onload = resolve;
});
ipcRenderer.on("port", async (event) => {
await windowLoaded;
// We use regular window.postMessage to transfer the port from the isolated
// world to the renderer world.
window.postMessage("port", "*", event.ports);
});
contextBridge.exposeInMainWorld("api", {
invoke: (channel, data) => ipcRenderer.invoke(channel, data),
addListener: (channel, fn) => ipcRenderer.addListener(channel, fn),
rmListeners: (channel) => ipcRenderer.removeAllListeners(channel),
});