forked from dreamland-mud/mudjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
83 lines (67 loc) · 2.06 KB
/
main.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
var send = function() {}, notify = function() {};
$(document).ready(function() {
function process(s) {
$('#terminal').trigger('output', [s]);
$('#input input').focus();
}
function connect() {
ws = new WebSocket(wsUrl, ['binary']);
ws.binaryType = 'arraybuffer';
ws.onmessage = function(e) {
var b = new Uint8Array(e.data);
b = String.fromCharCode.apply(null, b);
b = decodeURIComponent(escape(b));
process(b);
}
ws.onopen = function(e) {
send('7');
}
ws.onclose = function(e) {
process('\u001b[1;31m#################### DISCONNECTED ####################\n');
$('#reconnect').show();
$('#input input').hide();
ws = null;
}
send = function(text) {
ws.send(text + '\n');
}
process('Connecting....\n');
$('#reconnect').hide();
$('#input input').show();
}
connect();
$('#reconnect').click(function(e) {
e.preventDefault();
connect();
});
$('body').on('keydown', function(e) {
var input = $('#input input');
// dont autofocus if something in the panel is in focus
if($('#settings-panel :focus').length != 0)
return;
if(e.ctrlKey || e.altKey)
return;
if(input.is(':focus'))
return;
input.focus();
});
if('Notification' in window) {
Promise.resolve(Notification.permission)
.then(function(perm) {
if(perm === 'granted') {
return perm;
} else {
return Notification.requestPermission();
}
})
.then(function(perm) {
if(perm === 'granted') {
notify = function(text) {
if(document.hidden) {
new Notification(text);
}
}
}
});
}
});