-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
304 lines (266 loc) · 9.21 KB
/
script.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
const socket = io();
const form = document.getElementById('form');
const input = document.getElementById('input');
const imageUpload = document.getElementById('imageUpload');
const messages = document.getElementById('messages');
const nicknameForm = document.getElementById('nicknameForm');
const nicknameInput = document.getElementById('nicknameInput');
const enterChat = document.getElementById('enterChat');
const phraseKeyInput = document.getElementById('phraseKeyInput');
const locationInfo = document.getElementById('locationInfo');
const chatContainer = document.querySelector('.chat-container');
const userList = document.getElementById('userList');
const changeKeyBtn = document.getElementById('changeKeyBtn');
const clearCookiesBtn = document.getElementById('clearCookiesBtn');
const addImageBtn = document.getElementById('addImageBtn');
const menuContent = document.getElementById('menuContent');
const countdown = document.getElementById('countdown');
let nickname = '';
let phraseKey = '';
let nextClearTime;
let lastReadMessageId = null;
const sonnetKey = "sonnet_key";
function encryptMessage(message, key) {
return CryptoJS.AES.encrypt(message, key).toString();
}
function decryptMessage(encryptedMsg, key) {
try {
const bytes = CryptoJS.AES.decrypt(encryptedMsg, key);
return bytes.toString(CryptoJS.enc.Utf8);
} catch (error) {
console.error('Error decrypting message:', error);
return '解密错误:无效的密钥';
}
}
function toggleMenu() {
menuContent.classList.toggle('show');
}
socket.on('location info', (location) => {
locationInfo.textContent = `当前位置:${location}`;
locationInfo.style.display = 'block';
});
if (localStorage.getItem('nickname') && localStorage.getItem('phraseKey')) {
nickname = localStorage.getItem('nickname');
phraseKey = localStorage.getItem('phraseKey');
nicknameForm.style.display = 'none';
chatContainer.style.display = 'flex';
document.querySelector('.menu-container').style.display = 'flex';
socket.emit('user joined', nickname);
setInterval(() => {
socket.emit('user report', nickname);
}, 60000);
showWelcomeMessage();
}
function showWelcomeMessage() {
const welcomeMessage = document.createElement('li');
welcomeMessage.textContent = '欢迎来到聊天室!请注意,聊天记录会每分钟被清除一次。';
welcomeMessage.style.textAlign = 'center';
welcomeMessage.style.color = '#888';
welcomeMessage.style.fontStyle = 'italic';
messages.appendChild(welcomeMessage);
}
enterChat.addEventListener('click', () => {
nickname = nicknameInput.value.trim();
phraseKey = phraseKeyInput.value.trim();
if (nickname && phraseKey) {
localStorage.setItem('nickname', nickname);
localStorage.setItem('phraseKey', phraseKey);
nicknameForm.style.display = 'none';
chatContainer.style.display = 'flex';
document.querySelector('.menu-container').style.display = 'flex';
socket.emit('user joined', nickname);
setInterval(() => {
socket.emit('user report', nickname);
}, 60000);
showWelcomeMessage();
}
});
changeKeyBtn.addEventListener('click', () => {
phraseKey = prompt('请输入新的密钥:');
if (phraseKey) {
localStorage.setItem('phraseKey', phraseKey);
alert('密钥已更新');
}
});
addImageBtn.addEventListener('click', () => {
imageUpload.click();
});
imageUpload.addEventListener('change', () => {
if (imageUpload.files.length > 0) {
const file = imageUpload.files[0];
const reader = new FileReader();
reader.onload = function(e) {
sendMessage(e.target.result, true);
};
reader.readAsDataURL(file);
}
});
form.addEventListener('submit', (e) => {
e.preventDefault();
sendMessage();
});
function sendMessage(content = '', isImage = false) {
let message = '';
if (isImage && content) {
message = `${nickname}: [img]${content}[/img]`;
} else if (input.value) {
message = `${nickname}: ${input.value}`;
}
if (message) {
const encryptedMessage = encryptMessage(message, phraseKey);
socket.emit('chat message', { msg: encryptedMessage, key: phraseKey });
input.value = '';
imageUpload.value = '';
}
}
socket.on('chat message', (msg) => {
appendMessage(msg);
});
socket.on('chat history', (history) => {
messages.innerHTML = '';
history.forEach(appendMessage);
messages.scrollTop = messages.scrollHeight;
showWelcomeMessage();
});
socket.on('update user list', (users) => {
userList.innerHTML = '';
users.forEach(user => {
const userItem = document.createElement('div');
userItem.textContent = `${user.nickname} (${user.location})`;
userList.appendChild(userItem);
});
});
socket.on('chat history cleared', () => {
messages.innerHTML = '';
const clearMessage = document.createElement('li');
clearMessage.textContent = '聊天记录已被清除';
clearMessage.style.textAlign = 'center';
clearMessage.style.color = 'gray';
clearMessage.style.fontStyle = 'italic';
messages.appendChild(clearMessage);
});
socket.on('next clear time', (time) => {
nextClearTime = time;
updateCountdown();
});
function updateCountdown() {
const now = Date.now();
const timeLeft = Math.max(0, nextClearTime - now);
const seconds = Math.ceil(timeLeft / 1000);
countdown.textContent = `本轮聊天记录将在 ${seconds} 秒后消失`;
if (seconds <= 10) {
countdown.style.color = 'red';
countdown.style.fontWeight = 'bold';
} else {
countdown.style.color = '';
countdown.style.fontWeight = '';
}
if (timeLeft > 0) {
setTimeout(updateCountdown, 1000);
} else {
countdown.textContent = '聊天记录即将被清除...';
countdown.style.color = 'red';
countdown.style.fontWeight = 'bold';
}
}
function appendMessage(msg) {
const item = document.createElement('li');
const messageContent = document.createElement('div');
messageContent.classList.add('message-content');
messageContent.dataset.messageId = msg.id;
let decryptedMessage;
if (msg.key === sonnetKey) {
messageContent.classList.add('sonnet-message');
socket.emit('decrypt sonnet', msg.msg);
decryptedMessage = '正在解密古神的低语...';
} else {
decryptedMessage = decryptMessage(msg.msg, phraseKey);
}
const isOwnMessage = decryptedMessage.startsWith(`${nickname}:`);
if (isOwnMessage) {
messageContent.classList.add('message-right');
} else {
messageContent.classList.add('message-left');
}
const messageId = document.createElement('div');
messageId.classList.add('message-id');
const nameAndMessage = decryptedMessage.split(': ');
messageId.textContent = nameAndMessage[0];
const messageText = document.createElement('div');
const contentText = nameAndMessage.slice(1).join(': ');
const imgMatch = contentText.match(/\[img\](.*?)\[\/img\]/);
if (imgMatch) {
const imgUrl = imgMatch[1];
const img = document.createElement('img');
img.src = imgUrl;
img.alt = '聊天图片';
img.classList.add('message-image');
messageText.appendChild(img);
} else {
messageText.textContent = contentText;
}
const messageTime = document.createElement('div');
messageTime.classList.add('message-time');
const timestamp = new Date(msg.timestamp);
messageTime.textContent = timestamp.toLocaleString('zh-CN', {
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
const readStatus = document.createElement('div');
readStatus.classList.add('read-status');
updateReadStatus(readStatus, msg.readBy || []);
messageContent.appendChild(messageId);
messageContent.appendChild(messageText);
messageContent.appendChild(messageTime);
messageContent.appendChild(readStatus);
item.appendChild(messageContent);
messages.appendChild(item);
messages.scrollTop = messages.scrollHeight;
if (!isOwnMessage && !msg.readBy.includes(nickname)) {
socket.emit('message read', msg.id);
}
}
function updateReadStatus(element, readBy) {
if (readBy.length === 0) {
element.textContent = '未读';
} else if (readBy.length === 1) {
element.textContent = `已读 by ${readBy[0]}`;
} else {
element.textContent = `已读 by ${readBy.length} 人`;
}
}
socket.on('message read', (data) => {
const messages = document.querySelectorAll('.message-content');
messages.forEach(message => {
if (message.dataset.messageId === data.messageId) {
const readStatus = message.querySelector('.read-status');
if (readStatus) {
const currentReadBy = readStatus.textContent.startsWith('已读')
? readStatus.textContent.split('by ')[1].split(', ')
: [];
if (!currentReadBy.includes(data.readBy)) {
currentReadBy.push(data.readBy);
}
updateReadStatus(readStatus, currentReadBy);
}
}
});
});
socket.on('decrypted sonnet', (decryptedMsg) => {
const sonnetMessages = document.querySelectorAll('.sonnet-message');
const latestSonnetMessage = sonnetMessages[sonnetMessages.length - 1];
if (latestSonnetMessage) {
const messageText = latestSonnetMessage.querySelector('div:nth-child(2)');
if (messageText) {
messageText.textContent = decryptedMsg;
}
}
});
clearCookiesBtn.addEventListener('click', () => {
localStorage.removeItem('nickname');
localStorage.removeItem('phraseKey');
window.location.reload();
});