From 7244a4ceaaf3444587902ddd4d71575527a1798d Mon Sep 17 00:00:00 2001 From: Julian Appel Date: Sun, 2 Jun 2024 22:31:11 +0200 Subject: [PATCH] Added browser start and kill function --- scoreboard/controllers/cli.js | 40 +++++++++++++++++++------- scoreboard/public/javascripts/admin.js | 8 ++++++ scoreboard/routes/admin.js | 13 +++++++-- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/scoreboard/controllers/cli.js b/scoreboard/controllers/cli.js index 8b5f6bd..da21296 100644 --- a/scoreboard/controllers/cli.js +++ b/scoreboard/controllers/cli.js @@ -1,13 +1,31 @@ -const exec = require('node:child_process'); +const { spawn } = require('node:child_process') -// run the `ls` command using exec -exec('ls ./', (err, output) => { - // once the command has completed, the callback function is called - if (err) { - // log and return if we encounter an error - console.error("could not execute command: ", err) - return +let command; +let browserOpen = false; + +async function openBrowser() { + if(!browserOpen) { + // const command = spawn("chromium-browser --display=:0 --noerrors --disable-session-crashed-bubble --disable-infobars --start-fullscreen http://localhost:3000 &") + command = spawn('chromium-browser', ['--display=:0', '--incognito', '--noerrors', '--hide-crash-restore-bubble', '--disable-infobars', '--kiosk', 'http://localhost:3000']); + + command.stdout.on('data', data => { + console.log("stdout: ", data.toString()); + }); + + command.stderr.on('data', data => { + console.log("sterr: ", data.toString()); + }); + browserOpen = true; } - // log the output received from the command - console.log("Output: \n", output) -}) \ No newline at end of file +} + +async function killBrowser() { + if(browserOpen) { + command.kill(); + browserOpen = false; + } +} + +module.exports = { + openBrowser, killBrowser +} \ No newline at end of file diff --git a/scoreboard/public/javascripts/admin.js b/scoreboard/public/javascripts/admin.js index fe316ce..fc35162 100644 --- a/scoreboard/public/javascripts/admin.js +++ b/scoreboard/public/javascripts/admin.js @@ -132,6 +132,14 @@ async function refreshMonitor() { const response = await fetch("/admin/refreshMonitor"); // Call API Endpoint /admin/timerStart } +async function openBrowser() { + const response = await fetch("/admin/openBrowser"); // Call API Endpoint /admin/openBrowser +} + +async function killBrowser() { + const response = await fetch("/admin/killBrowser"); // Call API Endpoint /admin/killBrowser +} + // ###################################################################################################################################################### // Timerfunctions // ###################################################################################################################################################### diff --git a/scoreboard/routes/admin.js b/scoreboard/routes/admin.js index fbdea15..43c277a 100644 --- a/scoreboard/routes/admin.js +++ b/scoreboard/routes/admin.js @@ -4,6 +4,7 @@ const io = require('../controllers/socketio'); const timer = require('../controllers/timer'); const score = require('../controllers/score'); +const cli = require('../controllers/cli'); const db = require('../controllers/db'); // ###################################################################################################################################################### @@ -18,8 +19,16 @@ router.get('/refreshMonitor', function(req, res, next) { res.send(); // send empty response }); -// Express router endpoint to do cli stuff -router.get('/cli', function(req, res, next) { +// Express router endpoint to open browser +router.get('/openBrowser', function(req, res, next) { + cli.openBrowser(); + res.status(200); // Set http status code to 200 (success) + res.send(); // send empty response +}); + +// Express router endpoint to kill browser +router.get('/killBrowser', function(req, res, next) { + cli.killBrowser(); res.status(200); // Set http status code to 200 (success) res.send(); // send empty response });