DmitrMakeev commited on
Commit
0461f08
·
verified ·
1 Parent(s): 5ee51b7

Update chat.html

Browse files
Files changed (1) hide show
  1. chat.html +20 -15
chat.html CHANGED
@@ -244,23 +244,28 @@ async function clearChat() {
244
  }
245
 
246
  function renderMessages(msgs, clear = true) {
247
- if (clear) messagesDiv.innerHTML = '';
248
- msgs.forEach(m => {
249
- lastId = Math.max(lastId, m.id);
250
- let decrypted = '[🔒 Зашифровано]';
251
- if (encryptionKey) {
252
- try {
253
- decrypted = CryptoJS.AES.decrypt(m.encrypted_text, encryptionKey).toString(CryptoJS.enc.Utf8) || decrypted;
254
- } catch {}
255
- }
256
- const div = document.createElement('div');
257
- div.className = 'message ' + (m.user_id === userId ? 'own' : 'other');
258
- div.innerHTML = `<div class="message-info">${m.username} • ${m.timestamp}</div><div>${decrypted}</div>`;
259
- messagesDiv.appendChild(div);
260
- });
261
- messagesDiv.scrollTop = messagesDiv.scrollHeight;
 
 
 
 
262
  }
263
 
 
264
  setInterval(loadNewMessages, 2000);
265
  loadMessages();
266
  </script>
 
244
  }
245
 
246
  function renderMessages(msgs, clear = true) {
247
+ if (clear) messagesDiv.innerHTML = '';
248
+ msgs.forEach(m => {
249
+ // если это уже было добавлено — пропускаем
250
+ if (document.getElementById('msg-' + m.id)) return;
251
+
252
+ lastId = Math.max(lastId, m.id);
253
+ let decrypted = '[🔒 Сообщение зашифровано]';
254
+ if (encryptionKey) {
255
+ try {
256
+ decrypted = CryptoJS.AES.decrypt(m.encrypted_text, encryptionKey).toString(CryptoJS.enc.Utf8) || decrypted;
257
+ } catch {}
258
+ }
259
+ const div = document.createElement('div');
260
+ div.id = 'msg-' + m.id; // уникальный ID для проверки
261
+ div.className = 'message ' + (m.user_id === userId ? 'own' : 'other');
262
+ div.innerHTML = `<div><b>${m.username}</b> • ${m.timestamp}</div><div>${decrypted}</div>`;
263
+ messagesDiv.appendChild(div);
264
+ });
265
+ messagesDiv.scrollTop = messagesDiv.scrollHeight;
266
  }
267
 
268
+
269
  setInterval(loadNewMessages, 2000);
270
  loadMessages();
271
  </script>