This repository has been archived by the owner on Jul 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ws-repl.js
133 lines (113 loc) · 4.74 KB
/
ws-repl.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
/*
ws-repl
(c) 2017 Rowan Thorpe
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function wsrepl(customArgs) {
var defArgs = {
widgetName: 'repl',
serverName: '127.0.0.1',
socketPrefix: '/ws-repl',
socketProto: 'ws',
bufferLines: 2000,
replElName: 'output',
inputElName: 'msg',
replSendColour: 'black',
replReplyColour: 'darkred',
replOpenBgColour: '#9c9',
replClosedBgColour: '#933',
replCursor: '৆',
replMargin: '0.4em auto 0.4em 0.4em',
replPadding: '0.4em',
replHeight: 'calc(100vh / 4)',
replWidth: '25%',
replBorderWidth: '0.1em',
replBorderColour: '#000',
replBorderStyle: 'solid',
replTextAlign: 'left',
replOverflow: 'auto',
inputMargin: '0px auto auto 0.4em',
inputPadding: '0.2em',
inputWidth: '25%',
inputHeight: '2em'
};
var arg = Object.assign({}, defArgs, customArgs);
// constants
var splitBuf = [''];
var mainEl = document.getElementById(arg.widgetName);
var hiddenEl = document.createElement('textarea');
var replEl = document.createElement('pre');
replEl.setAttribute('id', arg.replElName);
var inputEl = document.createElement('input');
inputEl.setAttribute('id', arg.inputElName);
inputEl.setAttribute('type', 'text');
inputEl.setAttribute('autofocus', 'autofocus');
inputEl.setAttribute('tabindex', '1');
var sty = document.createElement('style');
sty.setAttribute('rel', 'stylesheet');
sty.setAttribute('type', 'text/css');
// functions
function htmlEncode(str) {
hiddenEl.innerHTML = str;
return hiddenEl.innerHTML;
}
function htmlDecode(str) {
hiddenEl.innerHTML = str;
return hiddenEl.childNodes.length === 0 ? '' : hiddenEl.childNodes[0].nodeValue;
}
function nonGraphicalToPage(e) {
if (e.keyCode === 8) { // backspace
var len = splitBuf.length;
splitBuf[len - 1] = htmlEncode(htmlDecode(splitBuf[len - 1]).slice(0, -1));
replEl.innerHTML = splitBuf.join('\n') + arg.replCursor;
replEl.scrollTop = 999999;
}
}
function typeToPage(e) {
if (e.keyCode === 13) { // enter
splitBuf[splitBuf.length] = '';
send();
replEl.innerHTML = splitBuf.join('\n') + arg.replCursor;
replEl.scrollTop = 999999;
return false;
} else {
var len = splitBuf.length;
splitBuf[len - 1] = htmlEncode(splitBuf[len - 1] + String.fromCharCode(e.keyCode));
replEl.innerHTML = splitBuf.join('\n') + arg.replCursor;
}
replEl.scrollTop = 999999;
}
function replyToPage(str) {
var len = splitBuf.length;
splitBuf[len - 1] = '<span style="color:' + arg.replReplyColour + '">' + htmlEncode(str) + '</span>';
splitBuf[len] = '';
replEl.innerHTML = splitBuf.join('\n') + arg.replCursor;
replEl.scrollTop = 999999;
}
function send() {
ws.send(inputEl.value);
inputEl.value = '';
inputEl.focus();
}
// main
sty.innerHTML = '#' + arg.replElName + ' {\n margin: ' + arg.replMargin + ';\n padding: ' + arg.replPadding + ';\n height: ' + arg.replHeight + ';\n width: ' + arg.replWidth + ';\n color: ' + arg.replSendColour + ';\n background-color: ' + arg.replClosedBgColour + ';\n border-width: ' + arg.replBorderWidth + ';\n border-color: ' + arg.replBorderColour + ';\n border-style: ' + arg.replBorderStyle + ';\n text-align: ' + arg.replTextAlign + ';\n overflow: ' + arg.replOverflow + ';\n}\n#' + arg.inputElName + ' {\n margin: ' + arg.inputMargin + ';\n padding: ' + arg.inputPadding + ';\n width: ' + arg.inputWidth + ';\n height: ' + arg.inputHeight + ';\n}';
document.getElementsByTagName('head')[0].appendChild(sty);
var ws = new WebSocket(arg.socketProto + '://' + arg.serverName + arg.socketPrefix);
ws.addEventListener('open', function (event) { replEl.style.backgroundColor = arg.replOpenBgColour; });
ws.addEventListener('message', function (event) { replyToPage(event.data, true); });
ws.addEventListener('close', function (event) { replEl.style.backgroundColor = arg.replClosedBgColour; });
replEl.innerHTML = arg.replCursor;
inputEl.addEventListener('keypress', function (event) { typeToPage(event); });
inputEl.addEventListener('keydown', function (event) { nonGraphicalToPage(event); });
mainEl.appendChild(replEl);
mainEl.appendChild(inputEl);
}