ari.lt/content/js/shell/index.js

65 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-09-05 02:29:04 +03:00
let cmd_prompt = document.getElementById('prompt');
let cmd_output = document.getElementById('command_output');
let cmd_history = document.getElementById('cmd_hist');
let shell = document.getElementById('shell');
2021-09-05 02:21:11 +03:00
var is_root = false;
function main() {
cmd_prompt.onkeypress = (e) => {
if (!cmd_prompt.value) return;
let code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
let command_list = cmd_prompt.value.split(' ');
let command = command_list[0];
let argv = command_list.slice(1);
if (commands[command]) {
2021-09-05 16:40:51 +03:00
if (commands[command.toLowerCase()]['root_only'] && !root) {
2021-09-05 16:38:44 +03:00
cmd_output.innerHTML = `'${command.toLowerCase()}' can <i>only</i> be ran as <b>root</b>. see <b>help su</b>`
2021-09-05 02:21:11 +03:00
} else {
2021-09-05 16:38:44 +03:00
cmd_output.innerHTML = commands[command.toLowerCase()]['func'](argv);
2021-09-05 02:21:11 +03:00
}
} else {
2021-09-05 16:38:44 +03:00
cmd_output.innerHTML = `${command.toLowerCase()}: command not found`
2021-09-05 02:21:11 +03:00
}
if (cmd_output.innerHTML.toString().replace(/\s/g, '')) {
let shell_old = document.createElement('div');
shell_old.setAttribute('class', 'shell');
shell_old.setAttribute('prompt', shell.getAttribute('prompt'));
let cmd = document.createElement('input');
cmd.setAttribute('class', 'prompt');
cmd.setAttribute('value', cmd_prompt.value);
cmd.setAttribute('readonly', '');
2021-09-05 03:56:25 +03:00
cmd.setAttribute('disabled', 'disabled')
2021-09-05 02:21:11 +03:00
let output = document.createElement('div');
output.setAttribute('class', 'output')
output.innerHTML = cmd_output.innerHTML;
shell_old.appendChild(cmd);
shell_old.appendChild(output);
cmd_history.appendChild(shell_old);
}
cmd_prompt.value = '';
cmd_output.innerHTML = '';
window.scrollTo(0, document.body.scrollHeight);
if (root) {
shell.setAttribute('prompt', 'root')
} else {
shell.setAttribute('prompt', '')
}
}
};
}
main()