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 fs = require('fs');
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
const log = require('electron-log/main');
|
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 config;
|
||||||
let win;
|
let win;
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Function
|
// Function
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@@ -25,23 +26,23 @@ let win;
|
|||||||
* 1. `/assets/presentation/config.json` (Tooloop OS)
|
* 1. `/assets/presentation/config.json` (Tooloop OS)
|
||||||
* 2. Path of the executable
|
* 2. Path of the executable
|
||||||
* - Linux: `app.getPath('exe')`
|
* - Linux: `app.getPath('exe')`
|
||||||
* - MacOS: `path.resolve(app.getPath('exe'), "../../../")`
|
* - MacOS: `path.resolve(app.getPath('exe'), "../../../../")`
|
||||||
* 3. `__dirname` (Development)
|
* 3. `__dirname` (Development)
|
||||||
*/
|
*/
|
||||||
function loadConfig() {
|
function loadConfig() {
|
||||||
const locations = [
|
const locations = [
|
||||||
'/assets/data',
|
'/assets/presentation',
|
||||||
os.platform() == 'darwin' ?
|
os.platform() == 'darwin' ?
|
||||||
path.resolve(app.getPath('exe'), "../../../") : app.getPath('exe'),
|
path.resolve(app.getPath('exe'), "../../../../") : app.getPath('exe'),
|
||||||
__dirname
|
__dirname
|
||||||
];
|
];
|
||||||
|
|
||||||
let filePath;
|
let filePath;
|
||||||
|
|
||||||
// Check all locations
|
// Check all locations
|
||||||
for (let i = 0; i < locations.length; i++) {
|
for (const location of locations) {
|
||||||
// Update the filepath
|
// Update the filepath
|
||||||
filePath = path.join(locations[i], 'config.json');
|
filePath = path.join(location, 'config.json');
|
||||||
try {
|
try {
|
||||||
// Try access
|
// Try access
|
||||||
fs.accessSync(filePath);
|
fs.accessSync(filePath);
|
||||||
@@ -51,8 +52,8 @@ function loadConfig() {
|
|||||||
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
|
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
|
||||||
config = JSON.parse(data);
|
config = JSON.parse(data);
|
||||||
|
|
||||||
// Break the loop if successful
|
// Break the loop
|
||||||
return false;
|
break;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('No config file found at ' + filePath);
|
console.warn('No config file found at ' + filePath);
|
||||||
}
|
}
|
||||||
@@ -90,31 +91,15 @@ async function createWindow() {
|
|||||||
win.show();
|
win.show();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// load fallback page
|
||||||
win.loadFile('index.html');
|
win.loadFile('index.html');
|
||||||
|
|
||||||
if (config != undefined) {
|
// load actual page from config file
|
||||||
try {
|
if (config != undefined && 'url' in config) {
|
||||||
let url = new URL(config.url);
|
testAndLoadUrl(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@@ -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
|
// Init electon app
|
||||||
|
|||||||
Reference in New Issue
Block a user