Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

electron how to setup preload.js

// Preload.js setup example
const { contextBridge, ipcRenderer } = require("electron")

// Expose valid channels for ipc.render methods inside array
const ipc = {
    render: {
        send: [],
        receive: [],
        sendRecieve: [],
    }
};

contextBridge.exposeInMainWorld(
    "ipcRender", {
        // From render to main.
        send: (channel, args) => {
            let validChannels = ipc.render.send;
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, args);
            }
        },
        // From main to render.
        receive: (channel, listener) => {
            let validChannels = ipc.render.receive;
            if (validChannels.includes(channel)) {
                ipcRenderer.on(channel, (event, args) => listener(args));
            }
        },
        // From render to main and back again.
        invoke: (channel, args) => {
            let validChannels = ipc.render.sendReceive;
            if (validChannels.includes(channel)) {
                return ipcRenderer.invoke(channel, args);
            }
        }
    }
);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #electron #setup
ADD COMMENT
Topic
Name
2+4 =