Skip to content Skip to sidebar Skip to footer

How To Access Browserwindow Javascript Global From Main Process In Electron?

I want a menu, defined in the main process to call JS code inside the current browser window in an atom/electron application. Getting main process globals form the browser window i

Solution 1:

Here is a reference to your comment about the webContents process in the api, in the "Note:" under remotes.

However, if you just want to trigger a function, you could also use the webContents.send() and ipc(main process) processes to trigger the appropriate code to run. Something like this...

// JS inside main processconstwindow = require('electron').BrowserWindow;

ipc.on('menuItem-selected', function(){
    let focusedWindow    = window.getFocusedWindow();
    focusedWindow.webContents.send('file-save');
});

// Inside the menu callbackrequire('ipc').on('file-save', function() {
  // File save function call here
});

Update:

For Electron version 0.35.0 and above, the ipc api changed to the following:

// In main process.const ipcMain = require('electron').ipcMain;

// In renderer process (web page).const ipcRenderer = require('electron').ipcRenderer;

Post a Comment for "How To Access Browserwindow Javascript Global From Main Process In Electron?"