test if url is reachable and only then load it
This commit is contained in:
@@ -3,6 +3,8 @@ const path = require('node:path');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const log = require('electron-log/main');
|
||||
const { linkExists } = require('link-exists');
|
||||
const { setInterval } = require('node:timers/promises');
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -12,7 +14,6 @@ const log = require('electron-log/main');
|
||||
let config;
|
||||
let win;
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Function
|
||||
//------------------------------------------------------------------------------
|
||||
@@ -25,23 +26,23 @@ let win;
|
||||
* 1. `/assets/presentation/config.json` (Tooloop OS)
|
||||
* 2. Path of the executable
|
||||
* - Linux: `app.getPath('exe')`
|
||||
* - MacOS: `path.resolve(app.getPath('exe'), "../../../")`
|
||||
* - MacOS: `path.resolve(app.getPath('exe'), "../../../../")`
|
||||
* 3. `__dirname` (Development)
|
||||
*/
|
||||
function loadConfig() {
|
||||
const locations = [
|
||||
'/assets/data',
|
||||
'/assets/presentation',
|
||||
os.platform() == 'darwin' ?
|
||||
path.resolve(app.getPath('exe'), "../../../") : app.getPath('exe'),
|
||||
path.resolve(app.getPath('exe'), "../../../../") : app.getPath('exe'),
|
||||
__dirname
|
||||
];
|
||||
|
||||
let filePath;
|
||||
|
||||
// Check all locations
|
||||
for (let i = 0; i < locations.length; i++) {
|
||||
for (const location of locations) {
|
||||
// Update the filepath
|
||||
filePath = path.join(locations[i], 'config.json');
|
||||
filePath = path.join(location, 'config.json');
|
||||
try {
|
||||
// Try access
|
||||
fs.accessSync(filePath);
|
||||
@@ -51,8 +52,8 @@ function loadConfig() {
|
||||
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
|
||||
config = JSON.parse(data);
|
||||
|
||||
// Break the loop if successful
|
||||
return false;
|
||||
// Break the loop
|
||||
break;
|
||||
} catch (err) {
|
||||
console.warn('No config file found at ' + filePath);
|
||||
}
|
||||
@@ -90,31 +91,15 @@ async function createWindow() {
|
||||
win.show();
|
||||
});
|
||||
|
||||
// load fallback page
|
||||
win.loadFile('index.html');
|
||||
|
||||
if (config != undefined) {
|
||||
try {
|
||||
let url = new URL(config.url);
|
||||
// let options = {
|
||||
// method: 'HEAD',
|
||||
// host: url.hostname,
|
||||
// port: url.port ? url.port : 80,
|
||||
// path: '/'
|
||||
// };
|
||||
// let req = http.request(options, function (r) {
|
||||
// log.info(r.headers);
|
||||
// });
|
||||
// req.end();
|
||||
|
||||
win.loadURL(config.url);
|
||||
|
||||
} catch (err) {
|
||||
log.error(err);
|
||||
}
|
||||
// load actual page from config file
|
||||
if (config != undefined && 'url' in config) {
|
||||
testAndLoadUrl(config.url);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -128,6 +113,42 @@ function validateDomain(event) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the host of the url is reachable.
|
||||
* Access is tried in a 1 second interval and the url is loaded if successfull.
|
||||
* @param {string} testurl
|
||||
*/
|
||||
function testAndLoadUrl(testurl) {
|
||||
try {
|
||||
let url = new URL(testurl);
|
||||
|
||||
// It’s an online url, test if it’s reachable
|
||||
if (['https:', 'http:', 'https', 'http'].includes(url.protocol)) {
|
||||
(async function () {
|
||||
for await (const startTime of setInterval(1000)) {
|
||||
let reachable = linkExists(url.hostname);
|
||||
if (reachable) {
|
||||
log.info("Successfull access to " + url.hostname)
|
||||
log.info("Loading " + config.url)
|
||||
win.loadURL(config.url);
|
||||
// break the interval
|
||||
break;
|
||||
} else {
|
||||
log.warn("Could not reach " + url.hostname)
|
||||
}
|
||||
}
|
||||
})();
|
||||
}
|
||||
// Load other protocols (i. e. offline) immedately
|
||||
else {
|
||||
win.loadURL(config.url);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
log.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Init electon app
|
||||
|
||||
Reference in New Issue
Block a user