logging, domain white-listing
This commit is contained in:
+6
-1
@@ -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"
|
||||
}
|
||||
@@ -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()
|
||||
});
|
||||
});
|
||||
|
||||
Generated
+9
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user