Electron window will not minimise or close - javascript

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.

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
}
});

Please enable nodeIntegration in index.html, then only you can use require in renderer page

Related

My electron app is not starting without any logging

So I am trying to make an electron app with angular(Typescript version) and it will not start at all, I have integrated Electron-log which does not do anything (that is the reason i think it does not start at all).
const path = require('path');
const url = require('url');
const log = require('electron-log');
log.transports.file.level = 'debug';
log.transports.file.format = '{h}:{i}:{s}:{ms} {text}';
log.transports.file.file = __dirname + './log.txt';
log.transports.file.streamConfig = { flags: 'w'};
log.info('message');
// Keep a global reference of the window object, if we don't the window will be closed
// automagically when the JS object is Garbage Collected. (GC from here on out)
let win;
try {
const createWindow = () => {
// set timeout to render the window not until the Angular compiler is ready to show the project
//create the browser window
win = new BrowserWindow({
width: 800,
height: 600,
icon: path.join(__dirname, 'favicon.ico'),
});
//and load the app
win.loadURL(url.format({
pathname: path.join(__dirname, 'resources/app/angular-flask/index.html'),
protocol: 'file:',
slashes: true
}));
// Emitted when the window is closed
win.on('closed', () => {
// Dereference the window object
win = null;
});
};
// This method will be called when Electron has finished initialization and is ready to
// create browser windows . Some API's can only be used after this event happens.
app.on('ready', createWindow);
// Quit when all windows are closed
app.on('windows-all-closed', () => {
// On macOS applications might stay active untill user quits with cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS sometimes a window is recreated when the dock icon is clicked and there are no other
// windows open
if (win === null) {
createWindow();
}
});
} catch(e) {
log.error(e)
}
Above is my Electron Production code, also available on pastebin Can anyone explain why this might be happening and offer a solution?
EDIT: It did not work before I integrated electron-log. Also the angular front-end works when i start that through npx lite-server.
EDIT2: Here is my Package.json and my updated main.js code electron.prod.js Also I am using electron packager.
It looks like app is not defined.
Can you please try adding the following lines at the beginning of your file.
const electron = require('electron');
const app = electron.app;
So to fix my issue I went back to the tutorial I followed and checked the package.json that was used there and slowly added and updated everything I had into that package.json.
When it was still working I ported it to my own package.json and with that it worked.

Electron unable to fetch resources (.swf)

I am using electron (using pepper flash plugin) to load a swf file directly into my BrowserWindow.
The .swf file loads correctly but the code inside the .swf file is requesting a seperate resource (eg: something.labels).
For some reason the something.labels file cannot be requested, everytime it fails instantly (screenshot). I cannot figure out why the error occurs, no details provided... any ideas?
Network request
main.js
const { app, BrowserWindow } = require('electron');
const path = require('path');
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
// 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;
const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 900,
titleBarStyle: 'hidden',
resizable: true,
autoHideMenuBar: true,
webPreferences: {
plugins: true
}
});
// and load the index.html of the app.
mainWindow.loadURL('https://example.org/myapplication.swf');
// Open the DevTools.
mainWindow.webContents.openDevTools({ mode: 'detach' });
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// 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;
});
};
app.commandLine.appendSwitch('ppapi-flash-path', path.join(__dirname, 'pepflashplayer.dll'));
app.commandLine.appendSwitch('ppapi-flash-version', '32.0.0.270');
// 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', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// 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();
}
});
For everyone who is experiencing the same problem..
This is a bug in the elctron framework, introduced in version 7
Changing you're electron version to 6.1.2 will fix this

Electron index.html not loading after building the app

I have an electron app that works perfectly fine before bundling it with electron-builder. After bundling it and opening the app, I get the following error :
Not allowed to load local resource: file:///tmp/.mount_displa4VwuQh/resources/app.asar/file:/tmp/.mount_displa4VwuQh/resources/app.asar/build/index.html
In the build folder I have the electron.js file and index.html and since the app is starting electron.js and thus index.html got bundled correctly.
Here is my electron.js app entry point(basically boilerplate):
const { app, BrowserWindow } = require('electron')
const url = require('url')
const path = require('path')
// 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 win
function createWindow() {
// Create the browser window.
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, './index.html'),
protocol: 'file:',
slashes: true
}));
// Emitted when the window is closed.
win.on('closed', () => {
// 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.
win = 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', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS 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 (win === 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.
When trying with loadFile() instead of loadUrl() I get the same error, but with this path instead : file:///tmp/.mount_displa4VwuQh/resources/app.asar/index.html
Any idea what is wrong? Thanks in advance!
So it looks like my problem was caused from improper packaging after all. Adding this in package.json "build" configuration fixed it:
"directories": {
"buildResources": "build",
"app": "build"
}
check once your directory path for index.html
I think you follow any tutorial and do not verify your path just once check I hope this will resolve your issue.
pathname: path.join(__dirname, ./src/index.html),

Typescript Electron Renderer process not working

I'm using this boilerplate provided by electron team here. The main process is working but when I use renderer process to create a new window it gives this error
Uncaught TypeError: electron_1.BrowserWindow is not a constructor
at HTMLButtonElement
As I searched it is because of .remote is absent from the compiled typescript to javascript. Code is
main.ts:
import { app, BrowserWindow } from "electron";
import * as path from "path";
let mainWindow: Electron.BrowserWindow;
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
height: 600,
width: 800,
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, "../index.html"));
// Open the DevTools.
// mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on("closed", () => {
// 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", () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
// 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.
renderer.ts:
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
import { BrowserWindow } from "electron";
import * as path from "path";
let childWindow: Electron.BrowserWindow;
const newWindowBtn = document.getElementById('new-window');
newWindowBtn.addEventListener('click', (event) => {
const modalPath = path.join('file://', __dirname, '../modal.html');
childWindow = new BrowserWindow({ width: 400, height: 320 });
childWindow.on('close', () => { childWindow = null; });
childWindow.loadURL(modalPath);
childWindow.show();
});
When I tried to not compile the typescript code to javascript & directly run with .remote it works.
So how to do with typescript code?
I was experiencing this same issue.
When installing the quick start with npm and starting it, a folder called "Dist" was created. There is a renderer.js file in this folder. This is where the typescript renderer file is being compiled into JS.
In my index.html, I changed
<script>
require('./src/renderer.ts')
</script>
To <script>
require('./dist/renderer.js')
</script>
And voila! It worked.
I am not sure why, but none of the Electron Typescript Guides are working because of the renderer.ts not being compiled and required on the fly, and there is an open issue on Github (has been there for months) with no response.
Well, this is my workaround. I am not aware of any negative implications this would cause.

Electron Chromium Inspector

I have a very basic Electron application whith a simple package.json like this:
{
"name": "app",
"version": "1.0.0",
"main": "main.js"
}
and the main.js file like this:
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
// 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 win
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// 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.
win = 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', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS 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 (win === null) {
createWindow()
}
})
But when I start the app the chromium's inpector apear in the screen together with my index.html page. Why the inspector apear automtically?
It isn't technically called the "Inspector", but the "DevTools", short for "Developer Tools" on Chromium. The reason it's opening on app startup is because you are opening the DevTools with this line:
win.webContents.openDevTools()
BrowserWindow.webContents is documented here, with the description of openDevTools:
contents.openDevTools([options])
Opens the devtools.
Remove this to stop the DevTools from opening.

Categories