Это работает, вам нужно включить nodeIntegration
в BrowserWindow и исправить импорт ipcRenderer
:
app.js (Основной процесс)
const {app,BrowserWindow} = require("electron")
const url = require("url")
const path = require("path")
let mainWindow
app.on("ready", function() {
mainWindow = new BrowserWindow({
width: 500,
height: 300,
webPreferences: {
nodeIntegration: true
}
})
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, "index.html"),
protocol: "file:",
slashes: true
}))
mainWindow.toggleDevTools()
setTimeout(() => {
console.log("sending message from main process")
mainWindow.webContents.send("submitted-form", "hello")
}, 3000)
})
index.html (процесс рендеринга)
<!DOCTYPE html>
<html>
<body>
Index with renderer javascript
</body>
<script type="text/javascript">
const { ipcRenderer } = require("electron")
ipcRenderer.on("submitted-form", function (event, data) {
console.log("received data", data)
alert("received data")
});
</script>
</html>