-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhack.js
133 lines (111 loc) · 3.09 KB
/
hack.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var _debug = false,
_blink = null,
_input = new Array(),
_history = new Array(),
_userText = '[goat@remotehost ~]$ ';
window.onload = function () {
addEventListener('keydown', keyDown);
addEventListener('keyup', keyUp);
addEventListener('keypress', keyPress);
// Initialize positions of input line, cursor, history, and caret
$('#input').insertBefore( $('#cursor') );
$('#history').insertBefore( $('#input') );
$('#caret').insertBefore( $('#input') );
$('#caret').text( _userText );
// Begin blinking interval for cursor
$('#cursor').css( 'visibility', 'visible' );
_blink = window.setInterval( toggleVisibility, 1000 );
}
function toggleVisibility () {
// DEBUG: SHOW STATUS OF CURSOR
if ( _debug )
console.log( $('#cursor').css( 'visibility' ) );
// If visible make hidden, if hidden make visible
if ( $('#cursor').css( 'visibility' ) == 'visible' )
$('#cursor').css( 'visibility', 'hidden' );
else
$('#cursor').css( 'visibility', 'visible' );
}
function keyDown (e) {
var _key = e.charCode || e.keyCode;
// Detect backspace and delete characters from input line
if ( _key == 8 ) {
if ( _input.length > 0 ) {
_input[_input.length-1].remove();
_input.pop();
}
}
// Don't process the enter key twice in case of FireFox
if ( _key == 13 ) { }
}
function keyUp (e) {
// Prevent default behaviour of keypresses
try {
e.preventDefault();
}
catch ( err ) { }
}
function keyPress (e) {
var _key = e.charCode || e.keyCode;
// DEBUG: SHOW KEYCODE
if ( _debug )
console.log( _key );
// Enter key pressed, process command
if ( _key == 13 ) {
// Add command to history above the input line
$('#history').append( _userText );
$('#history').append( $('#input').text() );
$('#history').append( '<br>' );
$('#history').insertBefore( $('#input') );
_history.push( _input.join('') );
var command = $('#input').text();
// Process the command
if ( command == 'exit' )
window.close();
else {
$('#history').append( commandHelper( command ) );
$('#history').insertBefore( $('#input') );
}
// Reset input line
$('#input').text( '' );
$('#caret').insertBefore( $('#input') );
$('#caret').text( _userText );
// DEBUG: SHOW COMMAND
if ( _debug )
console.log( command );
}
// Characters written to input line
else {
var _char = '';
if ( _char == '' ) _char = $( '<span>'+String.fromCharCode(_key)+'</span>' );
$('#input').append( _char );
_input.push( _char );
$('#input').insertBefore( $('#cursor') );
}
// Prevent default behaviour of keypresses
try {
e.preventDefault();
}
catch ( err ) { }
}
function help() {
return 'HELP - Print this help prompt'+
'<br>';
}
function exit() {
window.close();
return 'Attempting to exit';
}
function commandHelper (command) {
try {
// DEBUG: SHOW COMMAND BEING TRIED
if( _debug )
console.log( 'trying: '+command );
return this[command]();
}
catch ( err ) {
// DEBUG: SHOW COMMAND ERROR
if ( _debug )
console.log( err );
}
}