~azzar1/unity/add-show-desktop-key

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var magic = 'xyzzy';

function historyUp()
{
    if (this.cursor >= 0)
    {
        this.cursor--;
    }
}

function historyDown()
{
    if (this.cursor < this.items.length)
    {
        this.cursor++;
    }
}

function historyCurr()
{
    if (this.cursor < 0 || this.cursor >= this.items.length)
    {
        return "";
    }
    return this.items[this.cursor];
}

function historyAdd(text)
{
    this.items[this.items.length] = text;
    this.cursor = this.items.length
}

function historyShow()
{
    var res = "";
    if (this.cursor == -1)
    {
        res += "[]";
    }
    for (var i = 0; i < this.items.length; i++)
    {
        if (i == this.cursor)
        {
            res += "["
        }
        res += this.items[i].toString();
        if (i == this.cursor)
        {
            res += "]"
        }
        res += " "
    }
    if (this.cursor == this.items.length)
    {
        res += "[]";
    }
    return res;
}

function History()
{
    this.items = new Array();
    this.cursor = -1;
    this.up = historyUp;
    this.down = historyDown;
    this.curr = historyCurr;
    this.add = historyAdd;
    this.show = historyShow;
}

var hist = new History();

function make_query_string(pagename, args)
{
    var first = true;
    var qs = pagename;
    for (key in args)
    {
        vals = args[key];
        // vals can be an array, to make multiple args with the same name
        // To handle this, make non-array objects into an array, then loop
        if (!(vals instanceof Array))
            vals = [vals];
        for each (val in vals)
        {
            if (first)
            {
                qs += "?";
                first = false;
            }
            else
            {
                qs += "&";
            }
            qs += encodeURI(key) + "=" + encodeURI(val);
        }
    }
    return qs;
}

function make_post_body(args)
{
    var first = true;
    var qs = '';
    for (key in args)
    {
        vals = args[key];
        // vals can be an array, to make multiple args with the same name
        // To handle this, make non-array objects into an array, then loop
        if (!(vals instanceof Array))
            vals = [vals];
        for each (val in vals)
        {
            if (first)
            {
                first = false;
            }
            else
            {
                qs += "&";
            }
            qs += encodeURI(key) + "=" + encodeURI(val);
        }
    }
    return qs;
}

function enter_line()
{
    var inp = document.getElementById('inputText');
    var digest = hex_md5(inp.value + magic);
    var xmlhttp = new XMLHttpRequest();
    alert(inp.value);
    xmlhttp.open("POST", "chat", false);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(make_post_body({"digest":digest, "text":inp.value}))
    var res = JSON.parse(xmlhttp.responseText);
    var output = document.getElementById("output")
    {
        var pre = document.createElement("pre");
        pre.setAttribute("class", "inputMsg");
        pre.appendChild(document.createTextNode(inp.value + "\n"));
        output.appendChild(pre);
    }
    if (res && res[0])
    {
        // Success!
        // print out the output (res[0])
        var pre = document.createElement("pre");
        pre.setAttribute("class", "outputMsg");
        pre.appendChild(document.createTextNode(res[0]));
        output.appendChild(pre);
        // print out the return value (res[1])
        if (res[1])
        {
            var pre = document.createElement("pre");
            pre.setAttribute("class", "outputMsg");
            pre.appendChild(document.createTextNode(res[1] + "\n"));
            output.appendChild(pre);
        }
        // set the prompt to >>>
        var prompt = document.getElementById("prompt");
        prompt.replaceChild(document.createTextNode(">>> "), prompt.firstChild);
    }
    else if (res)
    {
        // Failure!
        // print out the error message (res[2])
        var pre = document.createElement("pre");
        pre.setAttribute("class", "errorMsg");
        pre.appendChild(document.createTextNode(res[2]));
        output.appendChild(pre);
    }
    else
    {
        // Need more input, so set the prompt to ...
        var prompt = document.getElementById("prompt");
        prompt.replaceChild(document.createTextNode("... "), prompt.firstChild);
    }
}

function catch_input(key)
{
    var inp = document.getElementById('inputText');
    if (key == 13)
    {
        enter_line();
        hist.add(inp.value);
        inp.value = hist.curr();
    }
    if (key == 38)
    {
        hist.up();
        inp.value = hist.curr();
    }
    if (key == 40)
    {
        hist.down();
        inp.value = hist.curr();
    }
}