11
function historyDown()
13
if (this.cursor < this.items.length)
19
function historyCurr()
21
if (this.cursor < 0 || this.cursor >= this.items.length)
25
return this.items[this.cursor];
28
function historyAdd(text)
30
this.items[this.items.length] = text;
31
this.cursor = this.items.length;
34
function historyShow()
37
if (this.cursor == -1)
41
for (var i = 0; i < this.items.length; i++)
47
res += this.items[i].toString();
54
if (this.cursor == this.items.length)
63
this.items = new Array();
66
this.down = historyDown;
67
this.curr = historyCurr;
68
this.add = historyAdd;
69
this.show = historyShow;
72
var hist = new History();
74
function make_post_body(args)
80
// vals can be an array, to make multiple args with the same name
81
// To handle this, make non-array objects into an array, then loop
82
if (!(vals instanceof Array))
85
for (i=0; i<vals.length; i++)
91
qs += encodeURIComponent(key) + "=" + encodeURIComponent(vals[i]);
99
var inp = document.getElementById('inputText');
100
var digest = hex_md5(inp.value + magic);
101
var xmlhttp = new XMLHttpRequest();
102
xmlhttp.open("POST", "chat", false);
103
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
104
xmlhttp.send(make_post_body({"digest":digest, "text":inp.value}))
105
var res = JSON.parse(xmlhttp.responseText);
106
var output = document.getElementById("output");
108
var pre = document.createElement("pre");
109
pre.setAttribute("class", "inputMsg");
110
pre.appendChild(document.createTextNode(inp.value + "\n"));
111
output.appendChild(pre);
113
if (res.hasOwnProperty('okay'))
116
// print out the output (res.okay[0])
117
var pre = document.createElement("pre");
118
pre.setAttribute("class", "outputMsg");
119
pre.appendChild(document.createTextNode(res.okay[0]));
120
output.appendChild(pre);
121
// print out the return value (res.okay[1])
124
var pre = document.createElement("pre");
125
pre.setAttribute("class", "outputMsg");
126
pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
127
output.appendChild(pre);
129
// set the prompt to >>>
130
var prompt = document.getElementById("prompt");
131
prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
133
else if (res.hasOwnProperty('exc'))
136
// print out the error message (res.exc)
137
var pre = document.createElement("pre");
138
pre.setAttribute("class", "errorMsg");
139
pre.appendChild(document.createTextNode(res.exc));
140
output.appendChild(pre);
142
else if (res.hasOwnProperty('more'))
144
// Need more input, so set the prompt to ...
145
var prompt = document.getElementById("prompt");
146
prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
149
// assert res.hasOwnProperty('input')
150
var prompt = document.getElementById("prompt");
151
prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
155
function catch_input(key)
157
var inp = document.getElementById('inputText');
162
inp.value = hist.curr();
167
inp.value = hist.curr();
172
inp.value = hist.curr();