TerminalHomepage/js/commands.js

44 lines
1.4 KiB
JavaScript
Raw Normal View History

function cmd_help() {
2023-01-13 11:37:25 +00:00
// Display 'help' message
return "Commands list:<br>" +
" -about Information about this website<br>"+
" -clear Clear terminal screen<br>" +
" -exec Execute arbitrary math and logic equations";
}
function cmd_about() {
// Display 'about' message
return "This website is based on the general idea and design of a terminal.<br>" +
"It serves the purpose of a homepage. It exists just for the fun of creating it.";
2023-01-13 06:27:13 +00:00
}
function cmd_clear() {
2023-01-13 11:37:25 +00:00
// Clear terminal by reloading page
2023-01-13 06:27:13 +00:00
window.location.reload();
return "";
}
function cmd_exec(input) {
2023-01-13 11:37:25 +00:00
// No input was given
if (input === undefined)
return "You must enter parameters!";
2023-01-13 11:37:25 +00:00
// Input contains letters or invalid characters
const str = input.join(' ');
if (/[',:;a-zA-Z]/.test(str) || str === "")
return "Invalid input!";
2023-01-13 11:37:25 +00:00
// Input is inside of character range with exceptions
const chars = str.split('').filter(e => {
const code = e.charCodeAt(0);
const s = code === 32;
const a = code > 36;
const b = code < 63;
const c = code === 94;
const d = code === 124;
return !(s || a && b || c || d);
});
2023-01-13 11:37:25 +00:00
// If exceptions remain, invalid input was given
if (chars.length > 0)
return "Invalid input!";
2023-01-13 11:37:25 +00:00
// Execute input and return output
return (new Function(`return ${str}`))();
}