smart url loading with a fallback strategy, fixes #4

This commit is contained in:
2024-05-29 22:05:48 +02:00
parent 76ff860c29
commit fb1ee829ee
+22 -10
View File
@@ -96,30 +96,41 @@ async function createWindow() {
}); });
// show black window // show black window
// win.setKiosk(true); win.setKiosk(true);
win.show(); win.show();
// load actual page from config file // Load page from config file
if (config != undefined && 'url' in config) { if (config != undefined && 'url' in config) {
testAndLoadUrl(config.url); testAndLoadUrl(config.url);
} else {
try {
// Load file from data folder if available
fs.accessSync('/assets/data/index.html');
win.loadFile('/assets/data/index.html');
} catch {
// Load fallback page
win.loadFile('index.html');
}
} }
// load fallback page
// win.loadFile('index.html');
} }
/** /**
* Validates the url of a navigatio event against the list of allowed domains in * Validates the url of a navigatio event against the list of allowed domains in
* the config file. See https://www.electronjs.org/docs/latest/api/web-contents#event-will-frame-navigate * the config file. See https://www.electronjs.org/docs/latest/api/web-contents#event-will-frame-navigate
* @param {Event} event * @param {Event} event
* @returns `true` if the url is allowed, `false` otherwise
*/ */
function validateDomain(event) { function validateDomain(event) {
console.log(event.type); if (config == undefined) return true;
if (config == undefined) return;
let url = new URL(event.url); let url = new URL(event.url);
// allow local urls
if (['file:', 'file'].includes(url.protocol)) return true;
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");
@@ -137,11 +148,11 @@ function validateDomain(event) {
function testAndLoadUrl(testurl) { function testAndLoadUrl(testurl) {
try { try {
let url = new URL(testurl); let url = new URL(testurl);
// If its an online url, test whether its reachable
// Its an online url, test if its reachable let delay = 1;
if (['https:', 'http:', 'https', 'http'].includes(url.protocol)) { if (['https:', 'http:', 'https', 'http'].includes(url.protocol)) {
(async function () { (async function () {
for await (const startTime of setInterval(1000)) { for await (const startTime of setInterval(delay)) {
let reachable = linkExists(url.hostname); let reachable = linkExists(url.hostname);
if (reachable) { if (reachable) {
log.info("Successfull access to " + url.hostname) log.info("Successfull access to " + url.hostname)
@@ -150,7 +161,8 @@ function testAndLoadUrl(testurl) {
// break the interval // break the interval
break; break;
} else { } else {
log.warn("Could not reach " + url.hostname) log.warn("Could not reach " + url.hostname);
delay = 1000;
} }
} }
})(); })();