I can't communicate between Main and Index.html with ipcMain
yceballost opened this issue · 1 comments
yceballost commented
Hi guys
I can't send my package version to index.html.
in main.js
const main = require('electron').ipcMain;
const { menubar } = require('menubar');
const electron = require('electron');
const { app, BrowserWindow, ipcMain } = require ('electron');
app.commandLine.appendSwitch ('disable-http-cache');
const { autoUpdater } = require('electron-updater');
let mainWindow;
// Cretate dropdown that will be open when click on system tray
const mb = menubar({
browserWindow: {
height: 666,
width: 400,
preloadWindow: true,
showDockIcon: false,
resizable: false,
webPreferences: {
nodeIntegration: true,
},
},
});
ipcMain.on('app_version', (event) => {
event.sender.send('app_version', { version: app.getVersion() });
});
in index.html
<body>
...
...
<script>
const { ipcRenderer } = require('electron');
const version = document.getElementById('version');
ipcRenderer.send('app_version');
ipcRenderer.on('app_version', (event, arg) => {
ipcRenderer.removeAllListeners('app_version');
version.innerText = 'Version' + arg.version;
});
</script
</body>
Specifications
- Menubar version: 8.0.2
- Platform: MacOS
- Electron version: v10.1.5
typologist commented
Just leaving this in case someone runs into it in the future.
You should wait for the window
to be created. Then you'll be able to communicate with it.
mb.on('after-create-window', () => {
mainWindow = mb.window;
// Now you can talk with your mainWindow
mainWindow.webContents.send('my-event', 'something');
});