@joe_kerr/electron-context-bridge
v0.1.1
Published
Electron context bridge between Main World (renderer) and Preload Isolated World.
Downloads
4
Maintainers
Readme
Electron plugin for an isolated preload script
Run code in Electron's Main World (renderer) that is isolated, as much as possible, from the Preload Isolated World. This is achieved by using a context bridge. An Electron context bridge is required for security purposes.
The current version has some open points on the todo list. Interfaces may change.
Install
npm install @joe_kerr/electron-context-bridge
Usage
const {ContextBridgeMain} = require("@joe_kerr/electron-context-bridge");
const contextBridge = new ContextBridgeMain(BrowserWindow);
// (...)
const win = new BrowserWindow({
webPreferences: {
preload: contextBridge.getPreloadFilePath(),
nodeIntegration: false,
contextIsolation: true //*required!
}
});
Two types of communication are implemented:
- Renderer-Main request-response model
- Main broadcast
Request-response
//Main: Setup message channel
contextBridge.createIpcChannel("getPrinters", async (filterList)=>{
const printers = browserWindow.webContents.getPrinters();
return filter(printers, filterList);
});
//Renderer: send request to main and get response
async function doSth() {
const result = await window.$electron.request("getPrinters", optionalEgAFilterList);
}
Broadcast
//Renderer: listen for message event
window.$electron.on("broadcast-event", function(event) {
console.log(event.detail); //"broadcast-message"
document.getElementById("response").textContent = event.detail;
});
//Main: send message event
contextBridge.broadcastMessage("broadcast-event", {data: "broadcast-message"});
Versions
0.1.1
- Fix: Removed full exposure of ipcRenderer in Preload
0.1.0
- Chg: Code cleanup and refactoring
Breaking changes
- Chg: Removed todo code for parameter validation. Requirements too diverse, user responsibility
0.0.1
- Public beta release.
Copyright
MIT (c) Joe Kerr since 2020