This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchat.js
251 lines (221 loc) · 8.24 KB
/
chat.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import domReady from "../dom-ready";
import { network as config } from "../config";
import { userState } from "../user-state";
import * as Cookies from "js-cookie";
import ReconnectingWebSocket from "reconnecting-websocket";
let wsUri = `ws${config.ssl ? "s" : ""}://${window.location.hostname}${config.websockets.localReroute ? "" : `:${config.websockets.port}`}/ws/chat`;
let ws = new ReconnectingWebSocket(wsUri, [], {
minReconnectionDelay: 1000,
maxReconnectionDelay: 30000,
reconnectionDelayGrowFactor: 2
});
let cookieData;
function inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
domReady.then(() => {
// String formatted timestamp
let timestamp = function() {
return new Date()
.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit"
})
.replace(/ /g, "")
.toLowerCase();
};
if (inIframe()) {
document.body.style.background = "#06060666";
}
// Add discord link open as new window
document.getElementById("discordLink").addEventListener("click", authenticationWindow, false);
// Get some references to DOM
let chatMessages = document.getElementById("chatMessages");
let chatMessageTemplate = document.getElementById("messageTemplate");
// Check for former authentication
cookieData = Cookies.getJSON("user_data");
checkAuthentication();
let lastMessageSent = Date.now();
let sendMessage = function(message) {
// Check whether messages are not being spammed.
let messageTimestamp = Date.now();
if (messageTimestamp > lastMessageSent + 500) {
ws.send(JSON.stringify({
access_token: cookieData.access_token,
id: cookieData.id,
avatar: cookieData.avatar,
content: message
}));
lastMessageSent = messageTimestamp;
return true;
} else {
return false;
}
};
// On websocket open (connection), add welcome message
ws.addEventListener("open", function() {
let clone = chatMessageTemplate.cloneNode(true);
clone.removeAttribute("id");
clone.getElementsByClassName("timestamp")[0].innerText = timestamp();
clone.getElementsByClassName("content")[0].removeChild(clone.getElementsByClassName("username")[0]);
clone.getElementsByClassName("content")[0].innerText = "Successfully connected to chat. Say !marble to join the race!";
clone.getElementsByClassName("content")[0].style.marginLeft = "0px";
clone.getElementsByClassName("content")[0].style.color = "#090";
clone.getElementsByClassName("content")[0].style.fontStyle = "italic";
chatMessages.insertAdjacentElement("beforeend", clone);
chatMessages.scrollTop = chatMessages.scrollHeight;
});
// On message, add message to DOM
ws.addEventListener("message", function(event) {
let data = JSON.parse(event.data);
let clone = chatMessageTemplate.cloneNode(true);
clone.removeAttribute("id");
clone.getElementsByClassName("timestamp")[0].innerText = timestamp();
clone.getElementsByClassName("name")[0].innerText = data.username;
clone.getElementsByClassName("name")[0].title = `${data.username}#${data.discriminator}`;
clone.getElementsByClassName("text")[0].innerText = data.content;
chatMessages.insertAdjacentElement("beforeend", clone);
chatMessages.scrollTop = chatMessages.scrollHeight;
});
// On websocket close (disconnect), add warning message. Reconnecting happens automagically.
ws.addEventListener("close", () => {
let clone = chatMessageTemplate.cloneNode(true);
clone.removeAttribute("id");
clone.getElementsByClassName("timestamp")[0].innerText = timestamp();
clone.getElementsByClassName("content")[0].removeChild(clone.getElementsByClassName("username")[0]);
clone.getElementsByClassName("content")[0].innerText =
`Lost connection... Attempt #${ws._retryCount} to reconnect in ${
Math.min(Math.ceil(
ws._options.minReconnectionDelay
* ws._options.reconnectionDelayGrowFactor
** (ws._retryCount - 1)
/ 1000
), 30)
} seconds`;
clone.getElementsByClassName("content")[0].style.marginLeft = "0px";
clone.getElementsByClassName("content")[0].style.color = "#900";
clone.getElementsByClassName("content")[0].style.fontStyle = "italic";
chatMessages.insertAdjacentElement("beforeend", clone);
chatMessages.scrollTop = chatMessages.scrollHeight;
});
// Be able to send chat messages back
let chatInput = document.getElementById("chatInput");
chatInput.addEventListener("keypress", function(event) {
let message = this.value;
if (event.keyCode === 13 && this.checkValidity() && message != "") {
sendMessage(message);
// Clean input
this.value = "";
}
}, false);
// Make send button functional
let chatButtonSend = document.getElementById("buttonSend");
chatButtonSend.addEventListener("click", function() {
if (chatInput.checkValidity() && chatInput.value != "") {
sendMessage(chatInput.value);
// Clean input
chatInput.value = "";
}
}, false);
// Make !marble button functional
let chatButtonMarble = document.getElementById("buttonMarble");
chatButtonMarble.addEventListener("click", function() {
sendMessage("!marble");
}, false);
// Make log out button functional
for (let chatButtonLogOut of document.getElementsByClassName("buttonLogOut")) {
chatButtonLogOut.addEventListener("click", function() {
if (confirm("Do you really wish to log out?")) {
Cookies.remove("user_data",
{
path: "/",
domain: window.location.hostname
}
);
// Send to parent if applicable
if (inIframe()) {
window.top.postMessage(userState.AUTH_CHANGED, `${window.location.origin}/client`);
}
window.location.reload(true);
}
}, false);
}
});
let authWindow;
function authenticationWindow() {
let authorizationUrl = "https://discordapp.com/api/oauth2/authorize?response_type=code";
authorizationUrl += `&client_id=${this.dataset.client_id}`;
authorizationUrl += `&scope=${this.dataset.scope}`;
authorizationUrl += `&redirect_uri=${this.dataset.redirect_uri}`;
if (this.dataset.state)
authorizationUrl += `&state=${this.dataset.state}`;
authWindow = window.open(authorizationUrl, "_blank", "location=yes,height=800,width=720,scrollbars=yes,status=yes");
}
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
if (event.data && event.data.success && event.origin === window.location.origin) {
cookieData = Cookies.getJSON("user_data");
authWindow.close();
checkAuthentication();
// Send to parent if applicable
if (inIframe()) {
window.top.postMessage(userState.AUTH_CHANGED, `${window.location.origin}/client`);
}
}
}
function onAuthorization(data) {
document.getElementById("userAvatar").src = `https://cdn.discordapp.com/avatars/${data.id}/${data.avatar}.jpg`;
document.getElementById("userName").innerText = `${data.username}#${data.discriminator}`;
document.getElementById("chatInputContainer").className = "authorized";
}
function onBanned() {
document.getElementById("chatInputContainer").className = "banned";
}
function checkAuthentication() {
if (
// If there is former data, check if it is not outdated.
cookieData
// See if current date is later than origin date + expiration period
&& Date.now() < cookieData.access_granted + cookieData.expires_in * 1000
) {
// Request a fresh token
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
let response = JSON.parse(xhr.responseText);
if (response.authorized && response.refreshed && response.tokenBody) {
response.tokenBody.id = cookieData.id;
response.tokenBody.username = cookieData.username;
response.tokenBody.discriminator = cookieData.discriminator;
response.tokenBody.avatar = cookieData.avatar;
let days = (response.tokenBody.expires_in / 62400) - 0.1; // seconds to days minus some slack
Cookies.set("user_data", response.tokenBody, {
expires: days,
path: "/",
domain: window.location.hostname,
secure: config.ssl
});
cookieData = response.tokenBody;
}
if (response.authorized) {
onAuthorization(cookieData);
} else if (response.banned) {
onBanned();
}
}
};
xhr.open("POST", "/chat", true);
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.send(
JSON.stringify({
"type": "refresh_token",
"id": cookieData.id,
"access_token": cookieData.access_token
})
);
}
}