#the collapse and close buttons do not work

1 messages · Page 1 of 1 (latest)

opaque snow
#

render.js

const remote = require('electron').remote;
const wnd = remote.getCurrentWindow();
document.addEventListener('DOMContentLoaded', () => {
    console.log('cmn.js loaded')
    let btn_min = document.querySelector('.btn-min');
    let btn_close = document.querySelector('.btn-close');

    document.querySelector('.btn-min').addEventListener('click', () => {
        btn_min.addEventListener('click', () => {
            wnd.minimize();
        });
    });

    btn_close.addEventListener('click', () => {
        wnd.close();
    });
});```

main.js

```py
const path = require('path');
const url = require('url');
const { app, BrowserWindow, ipcMain, webContents } = require('electron');

let win;

function createWindow() {
    win = new BrowserWindow({
        width: 1450,
        height: 678,
        minHeight: 678,
        minWidth: 1450,
        maxHeight: 678,
        maxWidth: 1450,
        icon: __dirname + "/img/icon.png",
        frame: false,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true,
        }
    });

    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
    }));

    //win,webContents.openDevtools();

};

app.on('window-all-closed', () => {
    app.quit();
});

app.on('ready', createWindow);```
#

html

<script>
        require('./render.js');
    </script>```
winter sparrow
#

your html contains no html called btn-min or btn-close

opaque snow
#

they are

opaque snow
winter sparrow
#

i never really worked with querySelectors, but i think you need to remove the leading "."

opaque snow
#

does not work

winter sparrow
opaque snow
# winter sparrow 2. https://www.electronjs.org/docs/latest/tutorial/tutorial-preload

**main.js **

const path = require('path');
const url = require('url');
const { app, BrowserWindow, ipcMain, webContents } = require('electron');

let win;

function createWindow() {
    win = new BrowserWindow({
        width: 1450,
        height: 678,
        minHeight: 678,
        minWidth: 1450,
        maxHeight: 678,
        maxWidth: 1450,
        icon: __dirname + "/img/icon.png",
        frame: false,
        webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true,
        }
    });

    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
    }));

    //win, webContents.openDevtools();

};

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

ipcMain.on('close', () => {
    app.quit()
})

app.on('ready', createWindow);```

**render.js**

```py
const remote = require('electron').remote;
const wnd = remote.getCurrentWindow();
document.addEventListener('DOMContentLoaded', () => {
    console.log('cmn.js loaded')
    let btn_min = document.querySelector('.btn-min');
    let btn_close = document.querySelector('.btn-close');

    function closeApp(e) {
        e.preventDefault()
        ipc.send('close')
    }

    document.getElementById("btn-close1").addEventListener("click", closeApp);
});```
#

as I understood it should work like this but it doesn't work

winter sparrow
#

depending on your electron version, this may not be enough, because there is "sandbox" and "contextisolation" too which would block you from using ipc directly in the renderer.js. Anyway. it is not called ipc.send("close") but would be rather ipcRenderer.send("close") :

webPreferences: {
            nodeIntegration: true,
            enableRemoteModule: true,
        }```
opaque snow
#

still not working:0

#

function closeApp(e) {
e.preventDefault()
ipcRenderer.send("close")
}

#

webPreferences: {}

#

I found out that common is not loading for common.js how else can I connect this

winter sparrow
#

try adding this to browserwindow webpreferances:
nodeIntegration: true,
enableRemoteModule: true,
sandbox: false
contextIsolation : false

#

it is really crap to do it, but may work for you

#

if this doesnt work. say what electron version you are using and try to follow the links i gave you

#

just read the whole getting started + tutorial + "processes in electron"

#

you need that info to understand what you are doing and what to do anyway