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)
81
// vals can be an array, to make multiple args with the same name
82
// To handle this, make non-array objects into an array, then loop
83
if (!(vals instanceof Array))
86
for (i=0; i<vals.length; i++)
96
qs += encodeURIComponent(key) + "=" + encodeURIComponent(vals[i]);
102
function enter_line()
104
var inp = document.getElementById('inputText');
105
var digest = hex_md5(inp.value + magic);
106
var xmlhttp = new XMLHttpRequest();
107
xmlhttp.open("POST", "chat", false);
108
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
109
xmlhttp.send(make_post_body({"digest":digest, "text":inp.value}))
110
var res = JSON.parse(xmlhttp.responseText);
111
var output = document.getElementById("output");
113
var pre = document.createElement("pre");
114
pre.setAttribute("class", "inputMsg");
115
pre.appendChild(document.createTextNode(inp.value + "\n"));
116
output.appendChild(pre);
118
if (res.hasOwnProperty('okay'))
121
// print out the output (res.okay[0])
122
var pre = document.createElement("pre");
123
pre.setAttribute("class", "outputMsg");
124
pre.appendChild(document.createTextNode(res.okay[0]));
125
output.appendChild(pre);
126
// print out the return value (res.okay[1])
129
var pre = document.createElement("pre");
130
pre.setAttribute("class", "outputMsg");
131
pre.appendChild(document.createTextNode(res.okay[1] + "\n"));
132
output.appendChild(pre);
134
// set the prompt to >>>
135
var prompt = document.getElementById("prompt");
136
prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
138
else if (res.hasOwnProperty('exc'))
141
// print out the error message (res.exc)
142
var pre = document.createElement("pre");
143
pre.setAttribute("class", "errorMsg");
144
pre.appendChild(document.createTextNode(res.exc));
145
output.appendChild(pre);
147
else if (res.hasOwnProperty('more'))
149
// Need more input, so set the prompt to ...
150
var prompt = document.getElementById("prompt");
151
prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
154
// assert res.hasOwnProperty('input')
155
var prompt = document.getElementById("prompt");
156
prompt.replaceChild(document.createTextNode("+++ "), prompt.firstChild);
160
function catch_input(key)
162
var inp = document.getElementById('inputText');
167
inp.value = hist.curr();
172
inp.value = hist.curr();
177
inp.value = hist.curr();