-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.js
483 lines (420 loc) · 18.5 KB
/
client.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
document.addEventListener('DOMContentLoaded', () => {
const socket = io();
const defaultUsername = getCookie('username') || '';
const defaultLocation = getCookie('location') || '';
document.getElementById('name').value = defaultUsername;
document.getElementById('location').value = defaultLocation;
let selectedColor = getCookie('userColor') || "#FFFFFF";
let userId = getCookie('userId');
if (!userId) {
userId = generateUserId();
setCookie('userId', userId, 30);
}
let OFFENSIVE_WORDS = [];
fetch('/offensive-words')
.then(response => response.json())
.then(words => {
OFFENSIVE_WORDS = words;
})
.catch(error => console.error('Error fetching offensive words:', error));
socket.emit('userConnected', { userId });
window.addEventListener('beforeunload', () => {
socket.emit('userDisconnected', { userId });
});
const createRoomBtn = document.getElementById('createRoomBtn');
const roomList = document.getElementById('roomList');
const roomsCountElement = document.getElementById('roomsCount');
const usersCountElement = document.getElementById('usersCount');
const searchRoomBtn = document.getElementById('searchRoomBtn');
const refreshRoomsBtn = document.getElementById('refreshRoomsBtn');
const searchRoomIdInput = document.getElementById('searchRoomId');
const noRoomsMessage = document.createElement('div');
noRoomsMessage.id = 'noRoomsMessage';
noRoomsMessage.className = 'no-rooms-message';
noRoomsMessage.innerText = 'No active public rooms right now. Create a new room and start the conversation!';
function generateUserId() {
return 'user_' + Math.random().toString(36).substr(2, 9);
}
function updateUserIdDisplay() {
const userIdDisplay = document.getElementById('userIdDisplay');
if (userIdDisplay) {
userIdDisplay.textContent = userId;
}
}
document.getElementById('settingsbtn').addEventListener('click', () => {
updateUserIdDisplay();
});
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
let cookieArray = document.cookie.split(';');
for (let i = 0; i < cookieArray.length; i++) {
let cookiePair = cookieArray[i].split('=');
if (name == cookiePair[0].trim()) {
return decodeURIComponent(cookiePair[1]);
}
}
return null;
}
function deleteCookie(name) {
document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
// New offensive word detection system
function tokenize(text) {
return text.toLowerCase().split(/\s+/).map(word => word.replace(/[^\w]/g, ''));
}
function isOffensiveWord(word, offensiveWord) {
if (word === offensiveWord) return true;
const obfuscatedWord = word.replace(/[0@]/g, 'o')
.replace(/[1!]/g, 'i')
.replace(/[3]/g, 'e')
.replace(/[4]/g, 'a')
.replace(/[5]/g, 's')
.replace(/[$]/g, 's')
.replace(/[7]/g, 't');
return obfuscatedWord === offensiveWord;
}
function containsOffensiveContent(text) {
const tokens = tokenize(text);
return OFFENSIVE_WORDS.some(offensiveWord =>
tokens.some(token => isOffensiveWord(token, offensiveWord))
);
}
window.updateUsername = function () {
const oldUsername = getCookie('username');
const oldLocation = getCookie('location');
const username = document.getElementById('name').value.trim();
const location = document.getElementById('location').value.trim();
let updateMessage = [];
if (containsOffensiveContent(username)) {
toastr.error('Username contains offensive words.');
return;
}
if (containsOffensiveContent(location)) {
toastr.error('Location contains offensive words.');
return;
}
if (username !== oldUsername) {
setCookie('username', username, 30);
updateMessage.push('username');
}
if (location !== oldLocation) {
setCookie('location', location, 30);
updateMessage.push('location');
}
if (updateMessage.length > 0) {
toastr.success(updateMessage.join(' and ') + ' updated.');
} else {
toastr.info('No changes were made.');
}
// Check for mod code
const code = username + location;
fetch('/verify-mod-code', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ code, userId })
})
.then(response => response.json())
.then(data => {
if (data.success) {
toastr.success('Mod mode activated');
}
})
.catch(error => {
console.error('Error:', error);
});
};
window.signOut = function () {
deleteCookie('username');
deleteCookie('location');
deleteCookie('userId');
document.getElementById('name').value = '';
document.getElementById('location').value = '';
toastr.success('Username, location, and user ID have been removed.');
socket.emit('userDisconnected', { userId });
};
createRoomBtn.addEventListener('click', () => {
const username = document.getElementById('name').value.trim();
const location = document.getElementById('location').value.trim();
const roomName = document.getElementById('roomName').value.trim();
const roomType = document.querySelector('input[name="roomType"]:checked').value;
const avatar = getCookie('userAvatar') || 'avatar15';
if (!username || !location || !roomName) {
toastr.error('Please fill in all required fields.');
return;
}
if (containsOffensiveContent(username) || containsOffensiveContent(location) || containsOffensiveContent(roomName)) {
toastr.error('Input contains offensive words.');
return;
}
const roomData = {
username: username,
location: location,
userId: userId,
name: roomName,
type: roomType,
color: selectedColor,
avatar: avatar
};
console.log('Sending room creation request:', roomData);
socket.emit('createRoom', roomData);
});
roomList.addEventListener('click', (event) => {
if (event.target.classList.contains('enter-chat-button')) {
const username = document.getElementById('name').value.trim();
const location = document.getElementById('location').value.trim();
if (!username || !location) {
toastr.error('Please enter your username and location.');
return;
}
if (containsOffensiveContent(username) || containsOffensiveContent(location)) {
toastr.error('Username or location contains offensive words.');
return;
}
const roomId = event.target.dataset.roomId;
const roomType = event.target.dataset.roomType;
const roomName = event.target.dataset.roomName;
const userColor = getCookie('userColor') || 'white';
const userAvatar = getCookie('userAvatar') || 'avatar15';
socket.emit('joinRoom', { roomId, username, location, userId, color: userColor, avatar: userAvatar });
}
});
searchRoomBtn.addEventListener('click', () => {
const searchRoomId = searchRoomIdInput.value.trim();
if (searchRoomId) {
socket.emit('searchRoom', searchRoomId);
} else {
socket.emit('getExistingRooms');
}
});
refreshRoomsBtn.addEventListener('click', () => {
const searchRoomId = searchRoomIdInput.value.trim();
if (searchRoomId) {
socket.emit('searchRoom', searchRoomId);
} else {
socket.emit('getExistingRooms');
}
});
searchRoomIdInput.addEventListener('input', () => {
if (!searchRoomIdInput.value.trim()) {
socket.emit('getExistingRooms');
}
});
socket.on('roomCreated', (room) => {
const existingRoomElement = document.getElementById(`room-${room.id}`);
if (existingRoomElement) {
existingRoomElement.remove();
}
if (room.type === 'public') {
const roomElement = createRoomElement(room);
roomList.appendChild(roomElement);
updateRoomCount();
}
if (socket.id === room.users[0].socketId) {
const url = `chat_room.html?roomId=${room.id}&username=${encodeURIComponent(room.users[0].username)}&location=${encodeURIComponent(room.users[0].location)}&userId=${room.users[0].userId}&roomType=${room.type}&roomName=${encodeURIComponent(room.name)}&txtclr=${encodeURIComponent(room.users[0].color)}&avatar=${room.users[0].avatar || getCookie('userAvatar') || 'avatar15'}`;
window.location.href = url;
}
});
socket.on('existingRooms', (rooms) => {
roomList.innerHTML = '';
rooms.forEach(room => {
if (room.type === 'public') {
const roomElement = createRoomElement(room);
roomList.appendChild(roomElement);
}
});
updateRoomCount();
});
socket.on('searchResult', (room) => {
roomList.innerHTML = '';
if (room) {
const roomElement = createRoomElement(room);
roomList.appendChild(roomElement);
} else {
toastr.error('Room not found');
}
updateRoomCount();
});
socket.on('roomRemoved', (roomId) => {
const roomElement = document.getElementById(`room-${roomId}`);
if (roomElement) {
roomElement.remove();
}
updateRoomCount();
});
socket.on('roomJoined', (data) => {
const { roomId, username, location, userId, roomType, roomName, color, avatar } = data;
window.location.href = `chat_room.html?roomId=${roomId}&username=${encodeURIComponent(username)}&location=${encodeURIComponent(location)}&userId=${userId}&roomType=${roomType}&roomName=${encodeURIComponent(roomName)}&txtclr=${encodeURIComponent(color)}&avatar=${avatar}`;
});
socket.on('roomUpdated', (room) => {
const existingRoomElement = document.getElementById(`room-${room.id}`);
const searchRoomId = searchRoomIdInput.value.trim();
if (room.type === 'public' && (!searchRoomId || (searchRoomId && room.id === searchRoomId))) {
if (existingRoomElement) {
existingRoomElement.replaceWith(createRoomElement(room));
} else {
const roomElement = createRoomElement(room);
roomList.appendChild(roomElement);
}
updateRoomCount();
}
});
socket.on('userJoined', (data) => {
const { roomId, username, location, userId } = data;
const existingRoomElement = document.getElementById(`room-${roomId}`);
if (existingRoomElement) {
const userInfoElement = existingRoomElement.querySelector('.user-info');
const userCount = userInfoElement.children.length + 1;
const userDetailElement = document.createElement('div');
userDetailElement.classList.add('user-detail');
userDetailElement.dataset.userId = userId;
userDetailElement.innerHTML =
`<img src="icons/chatbubble.png" alt="Chat" class="details-icon"> ${userCount}. ${username} / ${location}`;
userInfoElement.appendChild(userDetailElement);
const roomHeaderElement = existingRoomElement.querySelector('.room-header');
roomHeaderElement.textContent = `${existingRoomElement.dataset.roomName} (${userCount}/5)`;
const enterChatButton = existingRoomElement.querySelector('.enter-chat-button');
if (userCount === 5) {
enterChatButton.disabled = true;
enterChatButton.textContent = 'Room Full';
}
}
});
socket.on('userLeft', (data) => {
const { roomId, userId } = data;
const existingRoomElement = document.getElementById(`room-${roomId}`);
if (existingRoomElement) {
const userInfoElement = existingRoomElement.querySelector('.user-info');
const userDetailElements = userInfoElement.querySelectorAll('.user-detail');
userDetailElements.forEach((userDetailElement, index) => {
if (userDetailElement.dataset.userId === userId) {
userDetailElement.remove();
} else {
userDetailElement.querySelector('.details-icon').textContent = `${index + 1}.`;
}
});
const roomHeaderElement = existingRoomElement.querySelector('.room-header');
const userCount = userInfoElement.children.length;
roomHeaderElement.textContent = `${existingRoomElement.dataset.roomName} (${userCount}/5)`;
const enterChatButton = existingRoomElement.querySelector('.enter-chat-button');
if (userCount < 5) {
enterChatButton.disabled = false;
enterChatButton.innerHTML = `Enter <img src="icons/chatbubble.png" alt="Chat" class="button-icon">`;
}
}
});
function createRoomElement(room) {
const roomElement = document.createElement('div');
roomElement.id = `room-${room.id}`;
roomElement.dataset.roomName = room.name;
roomElement.classList.add('room-details-container');
roomElement.innerHTML =
`<div class="room-details">
<div class="room-info">
<div class="room-header">${room.name} (${room.users.length}/5)</div>
<div class="public-room-info">
(${room.type.charAt(0).toUpperCase() + room.type.slice(1)} Room) <img src="icons/handshake.png" alt="${room.type} Room" class="room-icon">
</div>
</div>
<div class="user-info">
${room.users.map((user, index) =>
`<div class="user-detail" data-user-id="${user.userId}"><img src="icons/chatbubble.png" alt="Chat" class="details-icon"> ${index + 1}. ${user.username} / ${user.location}</div>`
).join('')}
</div>
</div>
<div class="chat-button-container">
${room.users.length < 5 ?
`<button class="enter-chat-button" data-room-id="${room.id}" data-room-type="${room.type}" data-room-name="${room.name}">
Enter <img src="icons/chatbubble.png" alt="Chat" class="button-icon">
</button>`
:
`<button class="enter-chat-button" disabled>
Room Full
</button>`
}
</div>`;
return roomElement;
}
function updateRoomCount() {
const publicRoomElements = Array.from(document.querySelectorAll('.room-details-container')).filter(room => !room.classList.contains('private'));
const roomCount = publicRoomElements.length;
roomsCountElement.textContent = `${roomCount} room(s) available`;
if (roomCount === 0) {
if (!document.getElementById('noRoomsMessage')) {
roomList.appendChild(noRoomsMessage);
}
} else {
const noRoomsMessageElement = document.getElementById('noRoomsMessage');
if (noRoomsMessageElement) {
noRoomsMessageElement.remove();
}
}
}
socket.on('updateCounts', ({ roomsCount, usersCount }) => {
roomsCountElement.textContent = `${roomsCount} room(s) available`;
usersCountElement.textContent = `${usersCount} user(s) online`;
if (roomsCount === 0) {
if (!document.getElementById('noRoomsMessage')) {
roomList.appendChild(noRoomsMessage);
}
} else {
const noRoomsMessageElement = document.getElementById('noRoomsMessage');
if (noRoomsMessageElement) {
noRoomsMessageElement.remove();
}
}
});
socket.on('userBanned', (banExpiration) => {
const banDuration = Math.floor((banExpiration - Date.now()) / 1000);
setCookie('banned', 'true', banDuration / 86400);
setCookie('banExpiration', banExpiration, banDuration / 86400);
window.location.href = 'removed.html';
});
socket.on('duplicateUser', (data) => {
toastr.error(data.message);
setTimeout(() => {
window.location.href = data.redirectUrl;
}, 3000);
});
socket.on('offensiveWordError', (message) => {
toastr.error(message);
});
// Initialize the color of the preview textarea
document.addEventListener('DOMContentLoaded', function() {
const savedColor = getCookie('userColor');
if (savedColor) {
previewTextarea.style.color = savedColor;
}
});
function updateSelectedColor(color) {
selectedColor = color;
setCookie('userColor', color, 30);
// Update UI elements if needed
}
let inactivityTimeout;
const inactivityLimit = 120000;
function resetInactivityTimeout() {
clearTimeout(inactivityTimeout);
inactivityTimeout = setTimeout(() => {
if (document.cookie.indexOf('banned=true') === -1) {
socket.emit('userDisconnected', { userId });
toastr.error('You were removed from the room for being inactive for 2 minutes.');
setTimeout(() => {
window.location.href = 'index.html';
}, 3000);
}
}, inactivityLimit);
}
document.addEventListener('keydown', resetInactivityTimeout);
document.addEventListener('mousemove', resetInactivityTimeout);
resetInactivityTimeout();
});