-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnm_qjs.js
36 lines (33 loc) · 788 Bytes
/
nm_qjs.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
#!/usr/bin/env -S ./qjs --std
// QuickJS Native Messaging host
// guest271314, 5-6-2022
function getMessage() {
const header = new Uint32Array(1);
std.in.read(header.buffer, 0, 4);
const output = new Uint8Array(header[0]);
std.in.read(output.buffer, 0, output.length);
return output;
}
function sendMessage(message) {
const header = Uint32Array.from({
length: 4,
},
(_, index) => (message.length >> (index * 8)) & 0xff
);
const output = new Uint8Array(header.length + message.length);
output.set(header, 0);
output.set(message, 4);
std.out.write(output.buffer, 0, output.length);
std.out.flush();
}
function main() {
while (true) {
const message = getMessage();
sendMessage(message);
}
}
try {
main();
} catch (e) {
std.exit(0);
}