prevent new windows, fixes #5
This commit is contained in:
@@ -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`
|
||||||
|
// 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
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user