I was looking into the jQuery terminal emulator plugin: https://terminal.jcubic.pl/ but could not find what I was looking for amongst the documentation and actually found it a bit too advanced for me to understand so I am here asking how would I use the plugin to load commands and argument replies from a json object in say PHP or just a separate file altogether?
Asked
Active
Viewed 558 times
1 Answers
1
You need to do exactly the same thing as you would do with your normal ajax application. You need to send ajax request POST or GET and parse the response, after that you echo the result (using jQuery Terminal instead of swapping html or other things). And ajax request need to be triggered in function as first argument t jquery terminal, so you get raw command data.
$(function() {
$('body').terminal(function(command, term) {
$.post('script.php', {command: command}, function(response) {
// response is already parsed by jQuery
if (response.output) {
term.echo(response.output);
}
// you can do other things with other values, like execute
// terminal methods
if (response.exec) {
term[response.exec.method].apply(term, response.exec.args);
}
}, 'json');
}, {
greetings: 'php example',
onBlur: function() {
return false;
}
});
});
and in php you need to use code like this:
<?php
if (isset($_POST['command'])) {
// process command
echo json_encode($arrayorobject);
}
?>
and if you want to have the same file with server and client you can use trick with $_SERVER['HTTP_X_REQUESTED_WITH']
, check the source code for my leash shell.
And if you have json object in a file that have mapping {command: reply}
you can this:
$.get('commands.json', function(commands) {
$('body').terminal(function(command, term) {
var cmd = $.terminal.parse_command(command)
if (commands[cmd.name]) {
this.echo(commands[cmd.name]);
}
});
});
if you also have arguments you will need more complicated logic,

jcubic
- 61,973
- 54
- 229
- 402