0
votes

I need to access renderer scripts from electron main menu via onclick. I've added the following to the electron main menu:

{
       label: 'Checking for Update',
       click: function () {
          require('./update')
       }
}

.........................

{
       label: `Preferences...`,
       click: function () { 
          require('./preferences')
       }
}

I'm trying to call preferences.js or update.js above. Both scripts working fine when calling directly from index.html, or from app window via onclick, but not from app menu. When clicked in main app menu its just erroring with

Uncaught Exception:
TypeError: Cannot read property 'app' of undefined
    at IncomingMessage.res.on (update.js:20:36)
    at emitOne (events.js:96:13)
    at IncomingMessage.emit (events.js:188:7)
    at IncomingMessage.Readable.read (_stream_readable.js:381:10)
    at flow (_stream_readable.js:761:34)
    at resume_ (_stream_readable.js:743:3)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

update.js:20 looks like the following:

   const appname = electron.remote.app.getName()

There is no remote in the main menu process. Remote module belongs only to renderer so this seems to be reasonable error for me.

But I'm not quite sure how to easily fix this and make scripts work once called from app menu.

2

2 Answers

0
votes

Something like this could work:

const {app} = (process.type === 'renderer' ? require('electron').remote : require('electron'))

I think that should help to make your code main and renderer proof. Of course you have to change the rest of your code accordingly. const appname = electron.remote.app.getName() would just become const appname = app.getName()

0
votes

I was able to workaround it as following:

const electron = require('electron')
const remote = electron.remote       

if (remote) {    // remote will be 'undefined' when calling from the main process              
   const app = remote.app
   const dialog = remote.dialog 
   const window = remote.getCurrentWindow()
} else {
   const app = electron.app
   const dialog = electron.dialog 
   const window = electron.getCurrentWindow()
}