diff --git a/config.json b/config.json index 5d7a278..62dc20b 100644 --- a/config.json +++ b/config.json @@ -1,3 +1,8 @@ { - "url": "https://www.tooloop.de" + "url": "https://www.tooloop.de", + "allowedDomains": [ + "tooloop.de", + "www.tooloop.de" + ], + "logPath": "/Users/vollstock/Arbeit/Playground/tooloop-kiosk-browser-logs/tooloop-kiosk-browser.log" } \ No newline at end of file diff --git a/main.js b/main.js index 7a37e54..03a87bf 100644 --- a/main.js +++ b/main.js @@ -1,22 +1,37 @@ const { app, BrowserWindow } = require('electron/main'); const path = require('node:path'); const fs = require('fs'); -// const http = require('http'); +const log = require('electron-log/main'); + + +//------------------------------------------------------------------------------ +// Properties +//------------------------------------------------------------------------------ let config; let win; +//------------------------------------------------------------------------------ +// Function +//------------------------------------------------------------------------------ + +/** + * Loads a config file from disc. + * The file is expected to be in the application path and called “config.json”. + */ function loadConfig() { try { const data = fs.readFileSync(path.join(__dirname, "config.json"), { encoding: 'utf8' }); config = JSON.parse(data); } catch (err) { - console.log(err); + console.error(err); } } - +/** + * Creates the browser window + */ async function createWindow() { win = new BrowserWindow({ width: 1920, @@ -35,6 +50,8 @@ async function createWindow() { win = null; }); + win.webContents.addListener("will-navigate", validateDomain); + win.once("ready-to-show", () => { win.setKiosk(true); win.show(); @@ -51,20 +68,45 @@ async function createWindow() { // path: '/' // }; // let req = http.request(options, function (r) { - // console.log(r.headers); + // log.info(r.headers); // }); // req.end(); win.loadURL(config.url); - + } catch (err) { - console.log(err); + log.error(err); } finally { } } + +/** + * 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 +//------------------------------------------------------------------------------ + app.whenReady().then(() => { loadConfig(); + + log.initialize(); + if ('logPath' in config) { + log.transports.file.resolvePathFn = () => config.logPath; + } + createWindow(); app.on('activate', () => { @@ -76,4 +118,4 @@ app.whenReady().then(() => { app.on('window-all-closed', () => { app.quit() -}); \ No newline at end of file +}); diff --git a/package-lock.json b/package-lock.json index 8f5c5c6..0ed5815 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "electron-icon-builder": "^2.0.1", + "electron-log": "^5.1.5", "electron-squirrel-startup": "^1.0.1" }, "devDependencies": { @@ -4401,6 +4402,14 @@ "node": ">=10" } }, + "node_modules/electron-log": { + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/electron-log/-/electron-log-5.1.5.tgz", + "integrity": "sha512-vuq10faUAxRbILgQx7yHoMObKZDEfj7hMSZrJPsVrDNeCpV/HN11dU7QuY4UDUe055pzBxhSCB3m0+6D3Aktjw==", + "engines": { + "node": ">= 14" + } + }, "node_modules/electron-squirrel-startup": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/electron-squirrel-startup/-/electron-squirrel-startup-1.0.1.tgz", diff --git a/package.json b/package.json index 50c9314..037f758 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "dependencies": { "electron-icon-builder": "^2.0.1", + "electron-log": "^5.1.5", "electron-squirrel-startup": "^1.0.1" }, "name": "tooloop-kiosk-browser",