Home > Software engineering >  Electron Uncaught Exception: TypeError: Cannot read properties of null (reading 'on')
Electron Uncaught Exception: TypeError: Cannot read properties of null (reading 'on')

Time:07-04

The error appears only if i open a new window when the inspector is opened once. It does'nt appear for mainWindow.

enter image description here

// Detect maximize-unmaximize to change the maximize button's icon
app.on('browser-window-focus', (event, win) => {
  console.log('Focus: '   win.webContents.id);
  BrowserWindow.fromId(win.webContents.id).on('ready-to-show', () => {
    BrowserWindow.fromId(win.webContents.id).on('maximize', () => {
      console.log("maximize detected", win.webContents.id);
      BrowserWindow.fromId(win.webContents.id).send('window-events', 'maximize');
    })
    BrowserWindow.fromId(win.webContents.id).on('unmaximize', () => {
      console.log("unmaximize detected", win.webContents.id);
      BrowserWindow.fromId(win.webContents.id).send('window-events', 'unmaximize');
    })
  })
})

CodePudding user response:

This happens because you are using the same webview instance of your browser windows. You can fix this by creating a separate webview in each window and then use that instead. For example, try changing it as follows:

var myBrowser = require('electron').remote.getGlobal('Browser'); var myBrowser2 = require('electron').remote.getGlobal('Browser2');

myBrowser.webContents.on('dom-ready', function(){ myBrowser2.webContents.send('window-events', 'maximize') }); myBrowser2.webContents.on('dom-ready', function(){ myBrowser.webContents.send('window-events', 'unmaximize') });

You will need to make sure that both windows have their own unique id so that they do not conflict with one another. Also note that I am assuming you want to send these events from within the renderer process itself. If you would like to receive them on the main process side, you should be able to listen for "window-events" on the remote object returned by getGlobal.

CodePudding user response:

I solved the problem with using .getFocusedWindow() instead of .fromId. Thank you.

  • Related