import { createInterprocess } from '../interprocess';
export const { ipcMain: utilsIpcMain, ipcRenderer: utilsIpcRenderer } = createInterprocess({
main: {
/**
* Returns the electron application version
*/
async getElectronVersion() {
return '1.0.0';
},
},
renderer: {
/**
* Prints a log message in the frontend console.
* The first parameter is always an IPC event object.
*/
async logMessage(_, _msg: string) {
return;
},
},
});
If I want to implement the invoker directly to the context bridge:
import { contextBridge } from 'electron';
import { utilsIpcRenderer } from '../ipc/handlers/utils.ipc';
const api = {
utils: {
// typescript complains about the first parameter even though the handler is defined without any params
// so I have to satisfy it by using undefined
getElectronVersion: () => utilsIpcRenderer.invoke.getElectronVersion(undefined),
onLogMessage: (callback) => {
utilsIpcRenderer.handle.logMessage(async (_, { data: msg }) => {
callback(msg);
});
},
},
};
try {
contextBridge.exposeInMainWorld('native', api);
} catch (error) {
console.error(error);
}
I'm using Typescript 4.9.5
I'm not using the package installed as an npm module but copied the src files to my project to get around sandbox: true limitations
Given this example:
If I want to implement the invoker directly to the context bridge:
sandbox: true
limitations