showing results for - "ipcrenderer preload js"
Ahmed
28 Sep 2017
1const {
2    contextBridge,
3    ipcRenderer
4} = require("electron");
5
6// Expose protected methods that allow the renderer process to use
7// the ipcRenderer without exposing the entire object
8contextBridge.exposeInMainWorld(
9    "api", {
10        //send: (channel, data) => {
11        request: (channel, data) => {
12            // whitelist channels
13            let validChannels = ["toMain"];
14            if (validChannels.includes(channel)) {
15                ipcRenderer.send(channel, data);
16            }
17        },
18        //receive: (channel, func) => {
19        response: (channel, func) => {
20            let validChannels = ["fromMain"];
21            if (validChannels.includes(channel)) {
22                // Deliberately strip event as it includes `sender` 
23                ipcRenderer.on(channel, (event, ...args) => func(...args));
24            }
25        }
26    }
27);
28