-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcon.js
100 lines (96 loc) · 2.57 KB
/
con.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
function Console() {
this.screen = document.getElementById("screen");
this.linesep = "<br>";
this.output = function(txt) {
var numSpaces;
for (numSpaces=0; numSpaces<txt.length; numSpaces++) {
if (txt.charAt(numSpaces)!=" ") {
break;
}
}
if (numSpaces > 0) {
var newtxt = "";
for (var i=0; i<numSpaces; i++) {
newtxt += " ";
}
newtxt += txt;
txt = newtxt;
}
this.screen.innerHTML += txt;
this.screen.innerHTML += this.linesep;
/*
var lines = this.screen.innerHTML.split(this.linesep);
while(lines.length>20) {
lines.shift();
}
this.screen.innerHTML = lines.join(this.linesep);
*/
this.focus();
window.scrollTo(0,9999999);
}
this.clear = function() {
this.screen.innerHTML="";
}
this.keypress = function(e,id) {
var input = document.getElementById("userInput"+id);
if (e.keyCode == 13 || input.value == "\n") {
if (input.value!=="") {
input.form.submit();
this.screen.removeChild(input.form);
}
}
}
this.addPrompt = function(txt,onCR,type) {
if (txt.length<1) {
txt="> ";
}
if (type===undefined||typeof(type)==="undefined") {
type = null;
}
var t=new Date();
var id=t.getTime();
var holder = document.createElement("form");
holder.id = "promptHolder"+id;
holder.setAttribute("action", "javascript:;");
if (type=="echo") {
holder.setAttribute("onsubmit", "sys.exec('"+onCR+" '+sys.con.getInput("+id+",true))");
} else {
holder.setAttribute("onsubmit", "sys.exec('"+onCR+" '+sys.con.getInput("+id+",false))");
}
var prompt = document.createElement("span");
prompt.id = "promptText"+id;
prompt.innerHTML = txt;
var input = document.createElement("input");
if (type=="pw") {
input.type = "password";
}
input.id = "userInput"+id;
input.size = 80-txt.length;
input.className = "userInput";
//input.setAttribute("onkeydown","sys.con.keypress(event,"+id+")");
holder.appendChild(prompt);
holder.appendChild(input);
this.screen.appendChild(holder);
input.focus();
return id;
}
this.numPrompts = function() {
var p = this.screen.getElementsByTagName("form");
return p.length;
}
this.getInput = function(id, echo) {
var input = document.getElementById("userInput"+id);
var val = input.value;
if (echo) {
this.output(document.getElementById("promptText"+id).innerHTML+val);
}
this.screen.removeChild(document.getElementById("promptHolder"+id));
return val;
}
this.focus = function() {
var forms = this.screen.getElementsByTagName("form");
if (forms.length > 0) {
forms[0].getElementsByTagName("input")[0].focus();
}
}
}