diff --git a/main.js b/main.js index 91f2530..837bc7b 100644 --- a/main.js +++ b/main.js @@ -5,6 +5,7 @@ const os = require('os'); const log = require('electron-log/main'); const { linkExists } = require('link-exists'); const { setInterval } = require('node:timers/promises'); +const { type } = require('node:os'); //------------------------------------------------------------------------------ @@ -80,24 +81,32 @@ async function createWindow() { autoHideMenuBar: true }); - win.on("closed", function () { - win = null; + // register event callbacks + 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` + // doesn’t 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.show(); - win.once("ready-to-show", () => { - win.setKiosk(true); - win.show(); - }); - - // load fallback page - win.loadFile('index.html'); // load actual page from config file if (config != undefined && 'url' in config) { testAndLoadUrl(config.url); } + + // load fallback page + // win.loadFile('index.html'); } /** @@ -106,11 +115,18 @@ async function createWindow() { * @param {Event} event */ function validateDomain(event) { + console.log(event.type); + if (config == undefined) return; + let url = new URL(event.url); + if ('allowedDomains' in config && !config.allowedDomains.includes(url.hostname)) { event.preventDefault(); log.info("Navigation to " + event.url + " prevented"); + return false; } + + return true; } /**