Files
Tooloop-Kiosk-Browser/main.js
T

354 lines
9.9 KiB
JavaScript
Raw Normal View History

2024-06-03 16:32:05 +02:00
const { app, BrowserWindow, ipcMain, nativeTheme, Menu, MenuItem } = require('electron/main');
2024-05-28 16:43:25 +02:00
const path = require('node:path');
const fs = require('fs');
const os = require('os');
2024-05-28 18:27:50 +02:00
const log = require('electron-log/main');
2024-06-03 12:16:11 +02:00
const isReachable = require('is-reachable');
const { setInterval } = require('node:timers/promises');
2024-06-03 12:16:11 +02:00
const configModule = require('./js/config.js');
2024-05-28 18:27:50 +02:00
2024-05-31 10:57:15 +02:00
const isMac = process.platform === 'darwin';
2024-05-28 18:27:50 +02:00
//------------------------------------------------------------------------------
// Properties
//------------------------------------------------------------------------------
2024-05-28 14:25:32 +02:00
let config;
2024-06-03 16:32:05 +02:00
let configPath;
2024-06-03 12:16:11 +02:00
let configWindow;
2024-05-28 16:43:25 +02:00
let win;
2024-05-31 10:57:15 +02:00
let menu;
2024-05-28 14:25:32 +02:00
2024-05-28 18:27:50 +02:00
//------------------------------------------------------------------------------
// Function
//------------------------------------------------------------------------------
/**
* Loads a config file from disc.
* 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. appData directory
* 3. Path of the executable
* - Linux: `app.getPath('exe')`
* - MacOS: `path.resolve(app.getPath('exe'), "../../../../")`
* 4. `__dirname` (Development)
2024-05-29 22:50:18 +02:00
*/
2024-05-28 14:25:32 +02:00
function loadConfig() {
const locations = [
'/assets/presentation',
path.join(app.getPath('appData'), app.name),
2024-05-31 10:57:15 +02:00
isMac ? path.resolve(app.getPath('exe'), "../../../../") : app.getPath('exe'),
__dirname
];
// Check all locations
for (const location of locations) {
// Update the filepath
configPath = location;
let configFile = path.join(configPath, 'config.json');
try {
// Try access
fs.accessSync(configFile);
// Parse the file if found
console.info('Found config file at ' + configFile);
const data = fs.readFileSync(configFile, { encoding: 'utf8' });
config = JSON.parse(data);
// Break the loop
break;
} catch (err) {
2024-06-03 16:32:05 +02:00
console.warn('No config file found at ' + configPath);
}
2024-05-28 16:43:25 +02:00
}
2024-06-03 16:32:05 +02:00
}
function saveConfig(newConfig) {
// If there was no config file before create a new object
// and store a default save path
if (!config) {
config = {
url: "",
whitelist: [],
logPath: ""
};
2024-06-03 16:32:05 +02:00
try {
configPath = fs.accessSync('/assets/presentation');
} catch (error) {
configPath = path.join(app.getPath('appData'), app.name);
}
}
2024-06-03 16:32:05 +02:00
// Store changes
if (config.url != newConfig.url) {
2024-06-03 16:32:05 +02:00
config.url = newConfig.url;
loadUrlAsync(config.url);
}
2024-06-03 16:32:05 +02:00
config.whitelist = [];
let whitelist = newConfig.whitelist.split(";");
whitelist = whitelist.filter(function (entry) { return entry.trim() != ''; });
whitelist.forEach(
token => {
config.whitelist.push(token.replace(/\r?\n|\r/g, ""));
}
);
config.logPath = newConfig.logPath;
// Write file
let configFile = path.join(configPath, 'config.json');
fs.writeFile(
configFile,
JSON.stringify(config, null, " "),
(error) => {
if (error) log.warn('Error writing to ' + configPath, error);
log.info("Saved config to " + configFile);
}
);
// Update UI with new values
2024-06-03 16:32:05 +02:00
configWindow.webContents.send('update-config', config);
}
/**
* Creates a modal config view and attaches as to the main window
*/
function showConfigWindow() {
// create lazily
if (configWindow == undefined) {
configWindow = new BrowserWindow({
parent: win,
width: 640,
height: 460,
minimizable: false,
maximizable: false,
fullscreenable: false,
backgroundColor: '#1f1f1f',
autoHideMenuBar: true,
excludedFromShownWindowsMenu: true,
webPreferences: {
preload: path.join(__dirname, 'js/configPreload.js')
}
});
configWindow.on('close', (event) => {
event.preventDefault();
configWindow.hide();
});
ipcMain.on('save-config', (event, configData) => {
configWindow.hide();
saveConfig(configData);
});
ipcMain.on('cancel-config', (event) => {
configWindow.hide();
configWindow.webContents.send('update-config', config);
});
configWindow.loadFile('./html/config.html');
}
// update text field values
configWindow.webContents.send('update-config', config);
// show window
configWindow.show();
2024-05-28 14:25:32 +02:00
}
2024-05-28 18:27:50 +02:00
/**
* Creates the browser window
*/
2024-06-03 12:16:11 +02:00
function createMainWindow() {
2024-05-28 16:43:25 +02:00
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-29 22:29:17 +02:00
autoHideMenuBar: true,
2024-06-04 08:09:59 +02:00
kiosk: true,
2024-05-28 14:25:32 +02:00
webPreferences: {
2024-06-03 16:32:05 +02:00
preload: path.join(__dirname, 'js/preload.js'),
2024-05-28 14:25:32 +02:00
webSecurity: false,
2024-05-29 22:29:17 +02:00
disableDialogs: true
2024-05-28 14:25:32 +02:00
},
});
2024-05-29 22:29:17 +02:00
nativeTheme.themeSource = 'dark';
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`
// doesnt 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-06-03 12:16:11 +02:00
win.on("page-title-updated", (event) => event.preventDefault());
2024-05-28 14:25:32 +02:00
// Load page from config file
if (config != undefined && 'url' in config) {
2024-05-31 17:28:43 +02:00
loadUrlAsync(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
2024-06-03 12:16:11 +02:00
win.loadFile('./html/onboarding.html');
}
2024-05-28 16:43:25 +02:00
}
2024-05-31 10:57:15 +02:00
}
2024-05-29 21:38:55 +02:00
2024-05-31 10:57:15 +02:00
/**
* Creates all available keyboard shortcuts
*/
function createMenu() {
menu = new Menu();
menu.append(new MenuItem({
label: app.name,
submenu: [
{
label: 'Toggle Fullscreen',
accelerator: isMac ? 'Cmd+F' : 'Ctrl+F',
click: () => { win.setKiosk(!win.kiosk); }
},
2024-06-03 12:16:11 +02:00
{ role: 'reload' },
{ role: 'quit' },
{
label: 'Config',
accelerator: isMac ? 'Cmd+,' : 'Ctrl+,',
2024-06-03 16:32:05 +02:00
click: () => { showConfigWindow(); }
2024-06-03 12:16:11 +02:00
}
2024-05-31 10:57:15 +02:00
]
}));
Menu.setApplicationMenu(menu);
2024-05-28 14:25:32 +02:00
}
2024-05-31 10:57:15 +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
* @returns `true` if the url is allowed, `false` otherwise
2024-05-28 18:27:50 +02:00
*/
function validateDomain(event) {
if (config == undefined) return true;
2024-05-29 21:38:55 +02:00
2024-05-28 18:27:50 +02:00
let url = new URL(event.url);
2024-05-29 21:38:55 +02:00
// allow local urls
if (['file:', 'file'].includes(url.protocol)) return true;
2024-06-03 16:32:05 +02:00
if ('whitelist' in config && !config.whitelist.includes(url.hostname)) {
2024-05-28 18:27:50 +02:00
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
}
/**
* 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
*/
2024-05-31 17:28:43 +02:00
async function loadUrlAsync(testurl) {
try {
let url = new URL(testurl);
// If its an online url, test whether its reachable
2024-06-03 12:16:11 +02:00
if (['https:', 'http:', 'https', 'http'].includes(url.protocol)) {
reachable = await isReachable(url.href);
2024-05-31 17:28:43 +02:00
if (reachable) {
log.info("Successfull access to " + url.hostname);
log.info("Loading " + config.url);
win.loadURL(config.url);
} else {
log.warn("Could not reach " + url.hostname);
for await (_ of setInterval(1000)) {
2024-06-03 12:16:11 +02:00
reachable = await isReachable(url.href);
if (reachable) {
2024-05-31 17:28:43 +02:00
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);
}
}
2024-06-03 12:16:11 +02:00
2024-05-31 17:28:43 +02:00
}
}
// 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
//------------------------------------------------------------------------------
2024-05-29 22:50:18 +02:00
// Init electron app
2024-05-28 18:27:50 +02:00
//------------------------------------------------------------------------------
2024-06-03 12:16:11 +02:00
// https://github.com/electron/electron/issues/17972
app.commandLine.appendSwitch('--no-sandbox');
2024-06-03 12:24:44 +02:00
app.commandLine.appendSwitch('no-sandbox');
2024-06-03 12:16:11 +02:00
2024-05-28 14:25:32 +02:00
app.whenReady().then(() => {
2024-06-03 12:16:11 +02:00
// configModule.load();
2024-05-28 14:25:32 +02:00
loadConfig();
2024-05-28 18:27:50 +02:00
log.initialize();
if (config != undefined && 'logPath' in config) {
2024-05-28 18:27:50 +02:00
log.transports.file.resolvePathFn = () => config.logPath;
}
2024-05-31 10:57:15 +02:00
log.info('----------------------------------------------');
log.info('Starting Tooloop Kiosk Browser...');
createMenu();
2024-06-03 12:16:11 +02:00
createMainWindow();
2024-05-28 14:25:32 +02:00
});
2024-05-31 10:57:15 +02:00
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
2024-06-03 12:16:11 +02:00
createMainWindow();
2024-05-31 10:57:15 +02:00
}
})
2024-05-29 22:50:18 +02:00
2024-05-28 14:25:32 +02:00
app.on('window-all-closed', () => {
2024-05-28 16:43:25 +02:00
app.quit()
2024-05-28 18:27:50 +02:00
});
2024-05-29 22:50:18 +02:00
2024-05-31 10:57:15 +02:00
app.on('quit', () => {
log.info('Quit');
2024-05-31 17:28:43 +02:00
});
// prevent error dialogs in case of exceptions
process.on('uncaughtException', function (error) {
log.error(error);
2024-05-31 10:57:15 +02:00
});