(function initChat() { if (document.getElementById('horeca-chat-btn')) return; const chatHTML = `
πŸ’¬
`; document.body.insertAdjacentHTML('beforeend', chatHTML); const btn = document.getElementById('horeca-chat-btn'); const win = document.getElementById('horeca-chat-window'); const input = document.getElementById('horeca-chat-input'); const sendBtn = document.getElementById('horeca-chat-send'); const msgs = document.getElementById('horeca-chat-messages'); btn.onclick = () => { win.style.display = win.style.display === 'none' ? 'flex' : 'none'; }; const addMsg = (text, isClient) => { const align = isClient ? 'align-self:flex-end; background:#F1F5F9; color:#0288d1;' : 'align-self:flex-start; background:#fff; border:1px solid #ddd; color:#333;'; msgs.innerHTML += `
${text}
`; msgs.scrollTop = msgs.scrollHeight; }; const script = document.createElement('script'); script.src = "https://cdn.socket.io/4.7.2/socket.io.min.js"; document.head.appendChild(script); script.onload = () => { // ДостаСм Ρ‚ΠΎΠΊΠ΅Π½ ΠΈΠ· Ρ…Ρ€Π°Π½ΠΈΠ»ΠΈΡ‰Π° Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° const savedToken = localStorage.getItem('token'); // ΠŸΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌΡΡ с ΠΏΡ€Π΅Π΄ΡŠΡΠ²Π»Π΅Π½ΠΈΠ΅ΠΌ Ρ‚ΠΎΠΊΠ΅Π½Π° const socket = io({ path: '/api/socket.io', auth: { token: savedToken } }); const sendMessage = () => { if (!input.value.trim()) return; addMsg(input.value, true); socket.emit('client_message', input.value); input.value = ''; }; sendBtn.onclick = sendMessage; input.onkeypress = (e) => { if(e.key === 'Enter') sendMessage(); }; socket.on('server_message', (text) => addMsg(text, false)); }; })();