prevent new windows, fixes #5

This commit is contained in:
2024-05-29 21:38:55 +02:00
parent c99a7de98a
commit 76ff860c29
+25 -9
View File
@@ -5,6 +5,7 @@ const os = require('os');
const log = require('electron-log/main'); const log = require('electron-log/main');
const { linkExists } = require('link-exists'); const { linkExists } = require('link-exists');
const { setInterval } = require('node:timers/promises'); const { setInterval } = require('node:timers/promises');
const { type } = require('node:os');
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -80,24 +81,32 @@ async function createWindow() {
autoHideMenuBar: true autoHideMenuBar: true
}); });
win.on("closed", function () { // register event callbacks
win = null; win.on("closed", function () { win = null; });
win.webContents.on("will-frame-navigate", (event) => validateDomain(event));
win.webContents.setWindowOpenHandler(({ url }) => {
// we need to manually validate the url as `loadURL`
// doesnt trigger the `will-navigate` event
let event = new Event("DummyNavigation");
event.url = url;
if (validateDomain(event)) {
win.loadURL(url);
}
return { action: 'deny' };
}); });
win.webContents.addListener("will-navigate", validateDomain); // show black window
// win.setKiosk(true);
win.once("ready-to-show", () => {
win.setKiosk(true);
win.show(); win.show();
});
// load fallback page
win.loadFile('index.html');
// load actual page from config file // load actual page from config file
if (config != undefined && 'url' in config) { if (config != undefined && 'url' in config) {
testAndLoadUrl(config.url); testAndLoadUrl(config.url);
} }
// load fallback page
// win.loadFile('index.html');
} }
/** /**
@@ -106,11 +115,18 @@ async function createWindow() {
* @param {Event} event * @param {Event} event
*/ */
function validateDomain(event) { function validateDomain(event) {
console.log(event.type);
if (config == undefined) return;
let url = new URL(event.url); let url = new URL(event.url);
if ('allowedDomains' in config && !config.allowedDomains.includes(url.hostname)) { if ('allowedDomains' in config && !config.allowedDomains.includes(url.hostname)) {
event.preventDefault(); event.preventDefault();
log.info("Navigation to " + event.url + " prevented"); log.info("Navigation to " + event.url + " prevented");
return false;
} }
return true;
} }
/** /**