-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal.html
105 lines (97 loc) · 2.4 KB
/
terminal.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
function clear_screen() {
$('#output').text('');
}
function htmlEncode(value){
return $('<div/>').text(value).html();
}
function stdout(data) {
//console.log(data);
//console.log(typeof data);
if(typeof data != 'string') {
if(data.type == 'Buffer') {
data=data.data
}
for(i in data) {
//console.log(String.fromCharCode(data[i]));
$('#output').append(htmlEncode(String.fromCharCode(data[i])));
}
} else {
$('#output').append(htmlEncode(data));
}
}
var sio = io.connect(), socket = sio.socket.of('/sock');
socket.on('stdout', function (data) {
//console.log(data);
stdout(data);
});
socket.on('stderr', function (data) {
//console.log(data);
stdout(data);
});
socket.on('exit', function (data) {
//console.log(data);
stdout(data);
$('#form-cmd').hide();
});
$( document ).ready(function() {
setFocus = function(e) {
$('#cmd').focus();
};
$('body').click(setFocus);
$('#output').click(setFocus);
$('#form-cmd').click(setFocus);
$('#form-cmd').submit( function(e) {
var cmd = $('#cmd').val();
if(cmd == 'clear') {
clear_screen();
} else {
socket.emit('message', cmd);
}
$('#cmd').val('');
e.preventDefault();
});
$('#cmd').keydown(function(e) {
if ( e.which == 67 && e.ctrlKey ) {
//console.log('ctrl-c');
socket.emit('ctrl', 'c');
e.preventDefault();
}
if ( e.which == 68 && e.ctrlKey ) {
//console.log('ctrl-d');
socket.emit('ctrl', 'd');
e.preventDefault();
}
if ( e.keyCode == 9 ) {
//console.log('tab');
e.preventDefault();
}
});
});
</script>
<style type="text/css">
body, p, input {
font-family: FreeMono, monospace, fixed;
font-size: 13px;
border: none;
color: #ddd;
background: #444;
}
p { white-space: pre; }
p, form, input { margin:0; padding:0; }
input:focus { outline: none; border: none; }
#cmd { cursor: text; width: 90%; }
</style>
</head>
<body>
<div><p id="output"></p></div>
<form id="form-cmd" autocomplete="off">
$ <input id="cmd" />
</form>
</body>
</html>