contextBridge communication via preload is not working
umerjaved178 opened this issue · 3 comments
umerjaved178 commented
Unable to get window.electronAPI on the page
const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("electronAPI", {
setTitle: (title) => ipcRenderer.send("send-message", title),
});
node integration is on
umerjaved178 commented
@caoxiemeihao the core functionality of electronjs is broken, can you please look into this issue
caoxiemeihao commented
win = new BrowserWindow({
title: 'Main window',
icon: join(process.env.VITE_PUBLIC, 'favicon.ico'),
webPreferences: {
preload,
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
// Consider using contextBridge.exposeInMainWorld
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
- nodeIntegration: true,
- contextIsolation: false,
},
})
If this doesn't solve your problem, you can use the project created by create electron-vite
, that is different.
amirrezapanahi commented
@umerjaved178 Seems that there is currently no typescript support or declared types for the exposed API, probably requires some form of polyfilling.
However I found a workaround to the issue:
await (window as any).electronAPI.setTitle(title)