My app doesn’t close on button, electron.js

Tags: , ,



I have this electron.js app and I would like the application to close when I click on the text with class closer. Here is my code:

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title>Hello World!</title>
    <link rel="stylesheet" href="../style.css">
  </head>
  <body>

  
    <div class="content">
      <h1>Hello</h1>
      <h1 class="closer">UUUU</h1>
    </div>
 
    <script src="./renderer.js"></script>
    <script src="./closer.js"></script>
  </body>
</html>

main.js initialization electron

const {app, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 600,
    backgroundColor: "red",
    frame: false,
    webPreferences: {
      nodeIntegration: true,
     
      preload: path.join(__dirname, 'preload.js')
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.html')


  // mainWindow.webContents.openDevTools()
}


app.whenReady().then(() => {
  createWindow()
  
  app.on('activate', function () {
    
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})


app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

closer.js

const { app } = require('electron');
const babi = function(){
    const bubu = document.querySelector('.closer');
    function bubub(){
        bubu.innerHTML = "aganim";
        app.quit();
    }
    bubu.addEventListener('click', bubub);
}
babi();

My problem is that clicking the button, doesn’t work. What should I do?

Answer

[...bubu].forEach(bubu => 
  bubu.addEventListener("click", bubuClose()) 
);

 function bubuClose() {
   bubu.innerHTML = "aganim";
   window.close();
 };

I think you can do it with a loop like this.



Source: stackoverflow