-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwhos-typing.user.js
More file actions
173 lines (133 loc) · 5.44 KB
/
whos-typing.user.js
File metadata and controls
173 lines (133 loc) · 5.44 KB
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
// ==UserScript==
// @name Who's Typing
// @namespace http://tampermonkey.net/
// @version 1.0.1
// @description Shows who's typing in SE cat
// @author Radvylf Programs
// @match https://chat.stackexchange.com/rooms/*
// @grant none
// ==/UserScript==
(async () => {
const POSITIONING = 0; // 0: On top; 1: To right
const LOCALHOST = 0;
var ids = new Map();
var typing = new Map();
var brcast = null;
var sync = 0;
var room = (location.href.match(/^https:\/\/chat\.stackexchange\.com\/rooms\/(\d+)/) || [])[1];
var user_id = ([...((document.querySelector("#active-user") || {}).classList || [])].find(c => c.match(/^user-\d+$/)) || "").slice(5);
var user_name = document.querySelector("#active-user .avatar img").title;
if (!room || !user_id)
return;
var who_typing = document.createElement("div");
who_typing.style.fontSize = "12px";
who_typing.style.lineHeight = "1.25";
who_typing.style.fontWeight = "bold";
who_typing.style.whiteSpace = "pre-wrap";
if (POSITIONING == 0) {
document.querySelector("#input-area").insertBefore(who_typing, document.querySelector("#input-area").firstChild);
who_typing.style.position = "absolute";
who_typing.style.top = "-27px";
who_typing.style.left = "82px";
who_typing.style.padding = "6px";
who_typing.style.color = "#000000";
who_typing.style.backgroundColor = "rgba(255, 255, 255, 0.5)";
} else {
document.querySelector("#chat-buttons").appendChild(who_typing);
who_typing.style.color = "#ffffff";
who_typing.style.margin = "5px";
}
var show_typing = () => {
var typing_ids = [];
var tick = Infinity;
var now = Date.now() - sync;
for (var id of [...typing].sort((a, b) => a[1][1] - b[1][1])) {
if (now < id[1][0] + 2000) {
typing_ids.push(id[0]);
tick = Math.min(tick, (id[1][0] + 2000) - now);
}
}
var names = typing_ids.map(i => ids.get(i).replace(/[\u0000-\u002f\u003a-\u0040\u005b-\u0060\u007b-\u00ff]/g, c => "&#" + ("000" + c.charCodeAt(0)).slice(-4) + ";") || "<i>user" + i + "</i>");
if (typing_ids.length == 0) {
who_typing.textContent = "";
} else if (typing_ids.length == 1) {
who_typing.innerHTML = names[0] + " is typing...";
} else if (typing_ids.length == 2) {
who_typing.innerHTML = names[0] + " and " + names[1] + " are typing...";
} else {
who_typing.innerHTML = names[0] + " and " + (names.length - 1) + " others are typing.";
}
if (brcast) {
if (now >= brcast[1] + 20000) {
brcast = null;
} else {
who_typing.textContent = brcast[0];
tick = (Date.now() + 20000) - brcast[1];
}
}
if (tick != Infinity)
setTimeout(show_typing, tick);
};
var ws;
var start_ws = async (fails = 0) => {
try {
ws = new WebSocket(LOCALHOST ? "ws://localhost:8077" : "wss://rydwolf.xyz/whos_typing");
ws.onopen = () => {
ws.send("id\n" + Date.now() + "\n" + room + "\n" + user_id + ":" + JSON.stringify(user_name));
};
ws.onmessage = async (info) => {
var data = info.data.split("\n");
console.log(data);
if (data[1]) {
sync = +data[1].split(" ")[1] - (+data[1].split(" ")[0] + Date.now()) / 2 | 0;
console.log(sync);
}
var id;
if (!data[0]) {
var i = 0;
for (id of data.slice(2))
typing.set(id.split(":")[0], [+id.split(":")[1], i++]);
show_typing();
} else if (data[0] == "id") {
for (id of data.slice(2))
ids.set(id.split(":")[0], JSON.parse(id.split(":").slice(1).join(":")));
} else if (data[0] == "sync") {
} else if (data[0] == "brcast") {
brcast = [data.slice(3).join("\n"), Date.now()];
show_typing();
}
};
ws.onclose = (...info) => {
setTimeout(start_ws, fails < 4 ? 100 * 2 ** fails : fails < 8 ? 1000 * 2 ** (fails - 4) : 12000, fails + 1);
};
} catch (info) {
setTimeout(start_ws, fails < 4 ? 100 * 2 ** fails : fails < 8 ? 1000 * 2 ** (fails - 4) : 12000, fails + 1);
}
};
start_ws();
var status = 0;
var from = null;
var from_to_ws = () => {
if (from == null) {
status = 0;
return;
}
if (ws && ws.readyState == 1)
ws.send("\n" + Date.now() + "\n" + (Date.now() - sync));
from = null;
setTimeout(from_to_ws, 1000);
};
document.querySelector("#input").addEventListener("input", () => {
if (document.querySelector("#input").value.length) {
if (status == 0) {
if (ws && ws.readyState == 1)
ws.send("\n" + Date.now() + "\n" + (Date.now() - sync));
status = 1;
from = null;
setTimeout(from_to_ws, 1000);
} else if (status == 1) {
from = Date.now() + sync;
}
}
}, false);
})();