Added browser start and kill function

This commit is contained in:
Julian Appel 2024-06-02 22:31:11 +02:00
parent 8f402058ad
commit 7244a4ceaa
3 changed files with 48 additions and 13 deletions

View File

@ -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)
})
}
async function killBrowser() {
if(browserOpen) {
command.kill();
browserOpen = false;
}
}
module.exports = {
openBrowser, killBrowser
}

View File

@ -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
// ######################################################################################################################################################

View File

@ -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
});