-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
200 lines (166 loc) · 4.79 KB
/
example.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
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
/**
* Determine the mobile operating system.
*
* @returns {Boolean}
*/
function isMobile() {
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) {
return true;
}
if (/android/i.test(userAgent)) {
return true;
}
// iOS detection from: http://stackoverflow.com/a/9039885/177710
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return true;
}
return false;
}
const WebFontSize = 14;
const MobileFontSize = 9;
const term = new Terminal({
convertEol: true,
disableStdin: true,
fontFamily: 'Noto Sans Mono',
fontSize: isMobile() ? MobileFontSize: WebFontSize
});
const unicode14Addon = new Unicode14Addon(true);
term.loadAddon(unicode14Addon);
term.unicode.activeVersion = '14';
const fitAddon = new FitAddon.FitAddon();
term.loadAddon(fitAddon);
const weblinksAddon = new WebLinksAddon.WebLinksAddon();
term.loadAddon(weblinksAddon);
let history = [];
let history_index = -1;
let current_command = "";
function get_appropriate_ws_url(extra_url) {
let pcol;
let u = document.URL;
/*
* We open the websocket encrypted if this page came on an
* https:// url itself, otherwise unencrypted
*/
if (u.substring(0, 5) === "https") {
pcol = "wss://";
u = u.substr(8);
} else {
pcol = "ws://";
if (u.substring(0, 4) === "http")
u = u.substr(7);
}
u = u.split("/");
/* + "/xxx" bit is for IE10 workaround */
return pcol + u[0] + "/" + extra_url;
}
function new_ws(urlpath, protocol) {
if (typeof MozWebSocket != "undefined")
return new MozWebSocket(urlpath, protocol);
return new WebSocket(urlpath, protocol);
}
function onload() {
const el_container = document.getElementById('container');
const el_terminal = document.getElementById('terminal');
const el_input = document.getElementById('command');
window.addEventListener('scroll', function(e) {
window.scrollTo(0,0);
fitAddon.fit();
e.preventDefault();
e.stopPropagation();
});
window.addEventListener('touchmove', function(e) {
e.preventDefault();
e.stopPropagation();
});
window.addEventListener('resize', function (e) {
fitAddon.fit();
});
if(isMobile()) {
el_container.style.height = "50vh";
fitAddon.fit();
} else {
el_container.style.height = $(window).height();
fitAddon.fit();
}
el_input.setAttribute("disabled", "disabled");
el_input.style.fontFamily = term.getOption("fontFamily");
el_input.style.fontSize = term.getOption("fontSize") + "px";
term.open(el_terminal);
fitAddon.fit();
let ws = new_ws(get_appropriate_ws_url(""), "ascii");
const attachAddon = new AttachAddon.AttachAddon(ws);
term.loadAddon(attachAddon);
el_input.focus();
try {
ws.onopen = function () {
term.write("\033[9999;1H");
term.write("\r\n======== Connected.\r\n");
el_input.removeAttribute("disabled");
el_input.focus();
};
ws.onclose = function () {
term.write("\033[9999;1H");
term.write("\r\n======== Connection Lost.\r\n");
el_input.setAttribute("disabled", "disabled");
};
} catch (exception) {
alert("<p>Error " + exception);
}
el_input.addEventListener("blur", function(e) {
e.stopPropagation();
e.preventDefault();
el_input.focus();
});
el_input.addEventListener("keydown", function(e) {
if (e.key === "Tab") {
e.stopPropagation();
e.preventDefault();
return ;
}
if (e.key === "Enter") {
if(el_input.value) {
history.push(el_input.value);
if (history.length > 20) {
history.shift();
}
history_index = -1;
}
var content = el_input.value + "\n";
ws.send(content);
term.write(content + "\r");
el_input.value = "";
} else if (e.key === "ArrowUp") {
e.stopPropagation();
e.preventDefault();
// If there is history, show last one
if(history.length > 0) {
// remember current command
if(history_index === -1) {
current_command = el_input.value;
}
if (history_index < history.length - 1) {
history_index = history_index + 1;
el_input.value = history[history.length - history_index - 1];
}
}
} else if (e.key === "ArrowDown") {
e.stopPropagation();
e.preventDefault();
if(history_index > -1) {
history_index = history_index - 1;
if(history_index >= 0) {
el_input.value = history[history.length - history_index - 1];
} else {
el_input.value = current_command;
}
}
}
});
}
document.addEventListener("DOMContentLoaded", function () {
new FontFaceObserver("Noto Sans Mono").load().then(function () {
onload();
});
}, false);