Reputation: 551
I am building an electron app with the use of eel as well, although I am not sure that is relevant here. I recently came back to this project, and I'm sure it was working before. Everything works except for the minimise and close buttons which are custom ones.
Here is the HTML
<div id="title-bar">
<div id="title-bar-btns">
<button id="min-btn" onclick="window_minimise()">—</button>
<button id="close-btn" onclick="window_close()">✕</button>
</div>
</div>
And the JS
function window_minimise(){
const remote = require('electron').remote;
var window = remote.getCurrentWindow();
window.minimize();
}
function window_close(){
const remote = require('electron').remote;
eel.quit_app();
var window = remote.getCurrentWindow();
window.close();
}
And the Python code
@eel.expose
def quit_app():
sys.exit(0)
I tried running it in my browser via http://localhost:8000/html/index.html and when I click minimise I get this error
Uncaught ReferenceError: require is not defined at window_minimise (index.js:111) at HTMLButtonElement.onclick (index.html:18)
And close
Uncaught ReferenceError: require is not defined at window_close (index.js:117) at HTMLButtonElement.onclick (index.html:19)
Does anyone know what might be going wrong here and how I can fix it?
Thanks.
EDIT:
Here is my electron main.js file
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 1200, height: 748, icon:'web/images/favicon-32x32.png', resizable: false,
frame: false, show: false, backgroundColor: '#171717'}) // Needs to be changed based on theme
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})
mainWindow.setMenu(null);
// and load the index.html of the app.
mainWindow.loadURL('http://localhost:8000/html/index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active unti, l the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
There are some errors in it (missing semicolons and let mainWindow
), but those errors are also in the electron-quick-start main.js so I'm quite confused.
Upvotes: 0
Views: 1204
Reputation: 4093
As I explained in: https://stackoverflow.com/a/56289565/1907797
You have to set nodeIntegration
to true
in your BrowserWindow webPreferences since the version 5.0.0 the default values of nodeIntegration and webviewTag are false to improve security. Electron associated PR: 16235
const mainWindow = new electron.BrowserWindow({
webPreferences: {
nodeIntegration: true
}
});
Upvotes: 1
Reputation: 715
Please enable nodeIntegration in index.html, then only you can use require in renderer page
Upvotes: 0