sirzento
sirzento

Reputation: 687

Can't close window in electron

I have this code in my renderer.js:

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

function sendForm(event) {
    event.preventDefault() // stop the form from submitting
    let code = document.getElementById("code").value;
    ipcRenderer.send('2FacLogin', {twoFactorCode: code});

    // TODO Close Window
    var window = remote.getCurrentWindow();
    window.close();
}

this is how the window is opened:

 const win = new BrowserWindow({
    width: 300,
    height: 300,
    webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
    }
  })

Why does the window not close?

Upvotes: 0

Views: 315

Answers (1)

Bad Window
Bad Window

Reputation: 26

Since Electron 10.0.0, the remote module is disabled by default, it must be explicitly enabled when creating a new browser window by adding enableRemoteModule: true to the webPreferences object.

const win = new BrowserWindow({
    width: 300,
    height: 300,
    webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        enableRemoteModule: true
    }
})

See: Default Changed: enableRemoteModule defaults to false

BTW, the following statement:

var window = remote.getCurrentWindow();

most certainly triggers an error like:

Uncaught TypeError: Cannot read property 'getCurrentWindow' of undefined

that you can catch yourself by checking the DevTools' console...

Upvotes: 1

Related Questions