-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
140 lines (122 loc) · 3.47 KB
/
main.ts
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
import * as http from 'http';
import * as express from 'express';
import { server as WebSocketServer, request as WebSocketRequest } from 'websocket';
const app = express()
const port = 3000
const srv = http.createServer(app);
const clipProtocolToken = "clipboard-prot"
const reloadMessage = '85c76dcae2299d2235c793fe3425bd88376f5c4721cfddc1c9a9c452c20c5d33';
const connections = [];
var clipboardValue = "";
// dummy function hack for syntax highlighting functionality by `lit-html`
function html(strings: TemplateStringsArray, ...values: any[]): string {
return strings.reduce((p, c, i) => p + c + (values?.[i]?.toString() || ''), '');
}
function setupServer() {
app.get('/', (req, res) => res.send(html`
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Clipboard</title>
<style>
.field {
display: flex;
height: 100px;
}
.field .control textarea {
resize: none;
border: 0;
margin: 0;
}
.field .control,
.field .control > * {
display: block;
height: 100%;
border: 1px solid #aaa;
}
</style>
</head>
<body>
<div class="field">
<div class="control">
<textarea id="field"></textarea>
</div>
<div class="control">
<button id="to-clipboard">📋</button>
</div>
</div>
<script>
const field = document.getElementById("field");
const clpBtn = document.getElementById("to-clipboard");
const mainSocket = new WebSocket("ws://" + window.location.host, "${clipProtocolToken}");
mainSocket.onmessage = function(event) {
const msg = event.data;
switch (msg) {
case '${reloadMessage}':
setTimeout(() => location.reload(true), 3999);
document.body.innerHTML = document.body.innerHTML + "<br>Will reload in ~3s!";
break;
default:
field.value = msg;
}
}
field.oninput = (e) => {
mainSocket.send(field.value);
}
clpBtn.onclick = () => {
const oldFocusEl = document.activeElement;
field.select();
field.setSelectionRange(0, 9999);
document.execCommand("copy");
field.setSelectionRange(0, 0);
oldFocusEl.focus();
}
</script>
</body>
</html>
`))
const wsServer = new WebSocketServer({
httpServer: srv,
fragmentOutgoingMessages: false
});
wsServer.on('request', function (request: WebSocketRequest) {
const connection = request.accept(clipProtocolToken);
connections.push(connection);
console.log(`${connection.remoteAddress} connected`);
connection.sendUTF(clipboardValue);
connection.on('close', function () {
console.log(`${connection.remoteAddress} disconnected`);
const index = connections.indexOf(connection);
if (index !== -1) {
connections.splice(index, 1);
}
});
connection.on('message', function (message) {
if (message.type === 'utf8') {
try {
const msg = message.utf8Data;
if (msg != reloadMessage) {
clipboardValue = msg;
console.log(clipboardValue);
connections.forEach(function (tgt) {
tgt.sendUTF(msg);
});
}
}
catch (e) {
}
}
});
});
}
process.once("SIGHUP", function () {
connections.forEach(function (tgt) {
tgt.sendUTF(reloadMessage);
});
process.exit(0);
})
setupServer();
srv.listen(port, () => console.log(`Clipboard app listening on port ${port}!`));