2024-05-28 16:43:25 +02:00
|
|
|
|
const { app, BrowserWindow } = require('electron/main');
|
|
|
|
|
|
const path = require('node:path');
|
|
|
|
|
|
const fs = require('fs');
|
2024-05-29 11:24:13 +02:00
|
|
|
|
const os = require('os');
|
2024-05-28 18:27:50 +02:00
|
|
|
|
const log = require('electron-log/main');
|
2024-05-29 12:36:42 +02:00
|
|
|
|
const { linkExists } = require('link-exists');
|
|
|
|
|
|
const { setInterval } = require('node:timers/promises');
|
2024-05-29 21:38:55 +02:00
|
|
|
|
const { type } = require('node:os');
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// Properties
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2024-05-28 14:25:32 +02:00
|
|
|
|
|
|
|
|
|
|
let config;
|
2024-05-28 16:43:25 +02:00
|
|
|
|
let win;
|
2024-05-28 14:25:32 +02:00
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// Function
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Loads a config file from disc.
|
2024-05-29 11:24:13 +02:00
|
|
|
|
* The file is expected to be named `config.json` and is searched for in these
|
|
|
|
|
|
* locations and in this order:
|
|
|
|
|
|
*
|
|
|
|
|
|
* 1. `/assets/presentation/config.json` (Tooloop OS)
|
|
|
|
|
|
* 2. Path of the executable
|
|
|
|
|
|
* - Linux: `app.getPath('exe')`
|
2024-05-29 12:36:42 +02:00
|
|
|
|
* - MacOS: `path.resolve(app.getPath('exe'), "../../../../")`
|
2024-05-29 11:24:13 +02:00
|
|
|
|
* 3. `__dirname` (Development)
|
|
|
|
|
|
*/
|
2024-05-28 14:25:32 +02:00
|
|
|
|
function loadConfig() {
|
2024-05-29 11:24:13 +02:00
|
|
|
|
const locations = [
|
2024-05-29 12:36:42 +02:00
|
|
|
|
'/assets/presentation',
|
2024-05-29 11:24:13 +02:00
|
|
|
|
os.platform() == 'darwin' ?
|
2024-05-29 12:36:42 +02:00
|
|
|
|
path.resolve(app.getPath('exe'), "../../../../") : app.getPath('exe'),
|
2024-05-29 11:24:13 +02:00
|
|
|
|
__dirname
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
let filePath;
|
|
|
|
|
|
|
|
|
|
|
|
// Check all locations
|
2024-05-29 12:36:42 +02:00
|
|
|
|
for (const location of locations) {
|
2024-05-29 11:24:13 +02:00
|
|
|
|
// Update the filepath
|
2024-05-29 12:36:42 +02:00
|
|
|
|
filePath = path.join(location, 'config.json');
|
2024-05-29 11:24:13 +02:00
|
|
|
|
try {
|
|
|
|
|
|
// Try access
|
|
|
|
|
|
fs.accessSync(filePath);
|
|
|
|
|
|
|
|
|
|
|
|
// Parse the file if found
|
|
|
|
|
|
console.info('Found config file at ' + filePath);
|
|
|
|
|
|
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
|
|
|
|
|
|
config = JSON.parse(data);
|
|
|
|
|
|
|
2024-05-29 12:36:42 +02:00
|
|
|
|
// Break the loop
|
|
|
|
|
|
break;
|
2024-05-29 11:24:13 +02:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.warn('No config file found at ' + filePath);
|
2024-05-28 18:50:15 +02:00
|
|
|
|
}
|
2024-05-28 16:43:25 +02:00
|
|
|
|
}
|
2024-05-29 11:24:13 +02:00
|
|
|
|
|
2024-05-28 14:25:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-29 11:24:13 +02:00
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Creates the browser window
|
|
|
|
|
|
*/
|
2024-05-28 16:43:25 +02:00
|
|
|
|
async function createWindow() {
|
|
|
|
|
|
win = new BrowserWindow({
|
2024-05-28 14:25:32 +02:00
|
|
|
|
width: 1920,
|
|
|
|
|
|
height: 1080,
|
2024-05-28 16:43:25 +02:00
|
|
|
|
backgroundColor: '#000000',
|
2024-05-29 10:09:11 +02:00
|
|
|
|
icon: 'images/icon-512.png',
|
2024-05-28 14:25:32 +02:00
|
|
|
|
webPreferences: {
|
|
|
|
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
|
|
|
|
nodeIntegration: true,
|
|
|
|
|
|
webSecurity: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
show: false,
|
|
|
|
|
|
autoHideMenuBar: true
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-05-29 21:38:55 +02:00
|
|
|
|
// 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' };
|
2024-05-28 14:25:32 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-05-29 21:38:55 +02:00
|
|
|
|
// show black window
|
|
|
|
|
|
// win.setKiosk(true);
|
|
|
|
|
|
win.show();
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
2024-05-28 16:43:25 +02:00
|
|
|
|
|
2024-05-29 12:36:42 +02:00
|
|
|
|
// load actual page from config file
|
|
|
|
|
|
if (config != undefined && 'url' in config) {
|
|
|
|
|
|
testAndLoadUrl(config.url);
|
2024-05-28 16:43:25 +02:00
|
|
|
|
}
|
2024-05-29 21:38:55 +02:00
|
|
|
|
|
|
|
|
|
|
// load fallback page
|
|
|
|
|
|
// win.loadFile('index.html');
|
2024-05-28 14:25:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* 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
|
|
|
|
|
|
* @param {Event} event
|
|
|
|
|
|
*/
|
|
|
|
|
|
function validateDomain(event) {
|
2024-05-29 21:38:55 +02:00
|
|
|
|
console.log(event.type);
|
|
|
|
|
|
if (config == undefined) return;
|
|
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
let url = new URL(event.url);
|
2024-05-29 21:38:55 +02:00
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
if ('allowedDomains' in config && !config.allowedDomains.includes(url.hostname)) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
log.info("Navigation to " + event.url + " prevented");
|
2024-05-29 21:38:55 +02:00
|
|
|
|
return false;
|
2024-05-28 18:27:50 +02:00
|
|
|
|
}
|
2024-05-29 21:38:55 +02:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2024-05-28 18:27:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-29 12:36:42 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* 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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
// Init electon app
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
2024-05-28 14:25:32 +02:00
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
|
|
loadConfig();
|
2024-05-28 18:27:50 +02:00
|
|
|
|
|
|
|
|
|
|
log.initialize();
|
2024-05-28 18:50:15 +02:00
|
|
|
|
if (config != undefined && 'logPath' in config) {
|
2024-05-28 18:27:50 +02:00
|
|
|
|
log.transports.file.resolvePathFn = () => config.logPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-28 14:25:32 +02:00
|
|
|
|
createWindow();
|
|
|
|
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
|
|
createWindow();
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
2024-05-28 16:43:25 +02:00
|
|
|
|
app.quit()
|
2024-05-28 18:27:50 +02:00
|
|
|
|
});
|