Files
Tooloop-Kiosk-Browser/main.js
T

156 lines
4.0 KiB
JavaScript
Raw Normal View History

2024-05-28 16:43:25 +02:00
const { app, BrowserWindow } = require('electron/main');
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');
//------------------------------------------------------------------------------
// 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.
* 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')`
* - MacOS: `path.resolve(app.getPath('exe'), "../../../")`
* 3. `__dirname` (Development)
*/
2024-05-28 14:25:32 +02:00
function loadConfig() {
const locations = [
'/assets/data',
os.platform() == 'darwin' ?
path.resolve(app.getPath('exe'), "../../../") : app.getPath('exe'),
__dirname
];
let filePath;
// Check all locations
for (let i = 0; i < locations.length; i++) {
// Update the filepath
filePath = path.join(locations[i], 'config.json');
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);
// Break the loop if successful
return false;
} catch (err) {
console.warn('No config file found at ' + filePath);
}
2024-05-28 16:43:25 +02:00
}
2024-05-28 14:25:32 +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-28 16:43:25 +02:00
win.on("closed", function () {
win = null;
2024-05-28 14:25:32 +02:00
});
win.webContents.addListener("will-navigate", validateDomain);
2024-05-28 18:27:50 +02:00
2024-05-28 16:43:25 +02:00
win.once("ready-to-show", () => {
win.setKiosk(true);
win.show();
2024-05-28 14:25:32 +02:00
});
2024-05-28 16:43:25 +02:00
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);
}
2024-05-28 16:43:25 +02:00
}
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) {
let url = new URL(event.url);
if ('allowedDomains' in config && !config.allowedDomains.includes(url.hostname)) {
event.preventDefault();
log.info("Navigation to " + event.url + " prevented");
}
}
//------------------------------------------------------------------------------
// 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();
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
});