FuckingChat/index.html

914 lines
33 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FuckingChat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: #1a1a2e;
color: #e0e0e0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.chat-container {
background: #16213e;
border-radius: 12px;
box-shadow: 0 8px 32px rgba(0,0,0,0.5);
width: 100%;
max-width: 900px;
height: 90vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.chat-header {
background: #0f3460;
padding: 16px 20px;
border-bottom: 1px solid #1a4a7a;
display: flex;
justify-content: space-between;
align-items: center;
}
.chat-header h1 {
color: #e94560;
font-size: 18px;
font-weight: 700;
letter-spacing: 1px;
}
.chat-header .status {
font-size: 12px;
background: #1a4a7a;
padding: 4px 12px;
border-radius: 12px;
color: #a0c4ff;
}
.channel-bar {
display: flex;
background: #1a2744;
padding: 8px 12px;
gap: 6px;
border-bottom: 1px solid #0f3460;
overflow-x: auto;
}
.channel-btn {
padding: 6px 16px;
border: 1px solid #2a4a7a;
border-radius: 20px;
background: transparent;
color: #8899aa;
cursor: pointer;
font-size: 13px;
white-space: nowrap;
transition: all 0.2s;
}
.channel-btn:hover {
background: #1a3a5a;
color: #c0d0e0;
}
.channel-btn.active {
background: #e94560;
color: #fff;
border-color: #e94560;
}
.chat-messages {
flex: 1;
overflow-y: auto;
padding: 16px 20px;
background: #121e38;
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-track { background: #0d1528; }
.chat-messages::-webkit-scrollbar-thumb { background: #2a4a7a; border-radius: 3px; }
.message {
background: #1a2d4a;
border-radius: 8px;
padding: 10px 14px;
margin-bottom: 8px;
border-left: 3px solid #e94560;
}
.message .msg-ip {
font-size: 11px;
color: #6a8aaa;
margin-bottom: 4px;
}
.message .msg-content {
font-size: 14px;
line-height: 1.5;
word-wrap: break-word;
}
.message .msg-time {
font-size: 11px;
color: #556677;
text-align: right;
margin-top: 4px;
}
.empty-state {
text-align: center;
color: #556677;
padding: 40px;
font-size: 14px;
}
.input-area {
background: #0f3460;
padding: 12px 16px;
border-top: 1px solid #1a4a7a;
display: flex;
gap: 10px;
align-items: center;
}
.input-area input {
flex: 1;
padding: 10px 14px;
border: 1px solid #1a4a7a;
border-radius: 8px;
background: #121e38;
color: #e0e0e0;
font-size: 14px;
outline: none;
transition: border 0.2s;
}
.input-area input:focus { border-color: #e94560; }
.input-area input::placeholder { color: #4a6a8a; }
.input-area button {
padding: 10px 20px;
background: #e94560;
color: #fff;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
.input-area button:hover { background: #d63850; }
.input-area button:disabled {
background: #4a5a7a;
cursor: not-allowed;
}
.input-area .gif-btn {
padding: 8px 12px;
background: transparent;
border: 1px solid #2a4a7a;
border-radius: 8px;
color: #a0c4ff;
font-size: 18px;
cursor: pointer;
transition: all 0.2s;
line-height: 1;
}
.input-area .gif-btn:hover {
background: #1a3a5a;
border-color: #e94560;
}
.gif-picker {
display: none;
background: #0d1528;
border-top: 1px solid #1a4a7a;
padding: 12px 16px;
max-height: 300px;
overflow-y: auto;
}
.gif-picker.open { display: block; }
.gif-search {
display: flex;
gap: 8px;
margin-bottom: 10px;
}
.gif-search input {
flex: 1;
padding: 8px 12px;
border: 1px solid #1a4a7a;
border-radius: 6px;
background: #121e38;
color: #e0e0e0;
font-size: 13px;
outline: none;
}
.gif-search input:focus { border-color: #e94560; }
.gif-search button {
padding: 8px 14px;
background: #e94560;
color: #fff;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 13px;
}
.gif-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
gap: 8px;
}
.gif-grid img {
width: 100%;
border-radius: 6px;
cursor: pointer;
transition: transform 0.15s;
}
.gif-grid img:hover {
transform: scale(1.05);
box-shadow: 0 4px 12px rgba(233,69,96,0.3);
}
.gif-spinner {
text-align: center;
padding: 20px;
color: #556677;
font-size: 24px;
}
.message .msg-content img.inline-gif {
max-width: 200px;
max-height: 150px;
border-radius: 6px;
display: block;
margin-top: 6px;
}
.toast {
position: fixed;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
padding: 10px 24px;
border-radius: 8px;
font-size: 14px;
opacity: 0;
transition: opacity 0.3s;
pointer-events: none;
z-index: 1000;
}
.toast.visible { opacity: 1; }
.toast.error { background: #c0392b; color: #fff; }
.toast.success { background: #27ae60; color: #fff; }
.toast.info { background: #2980b9; color: #fff; }
.poll-card {
background: #1a2d4a;
border-radius: 8px;
padding: 14px;
margin-bottom: 12px;
border-left: 3px solid #f39c12;
}
.poll-card .poll-question {
font-size: 15px;
font-weight: 600;
color: #f1c40f;
margin-bottom: 10px;
}
.poll-card .poll-meta {
font-size: 11px;
color: #6a8aaa;
margin-bottom: 8px;
}
.poll-card .poll-option {
display: flex;
align-items: center;
padding: 8px 10px;
margin-bottom: 4px;
background: #0f2040;
border-radius: 6px;
cursor: pointer;
transition: background 0.15s;
position: relative;
overflow: hidden;
}
.poll-card .poll-option:hover { background: #1a3a5a; }
.poll-card .poll-option .poll-bar {
position: absolute;
left: 0; top: 0; bottom: 0;
background: rgba(46, 204, 113, 0.15);
border-radius: 6px;
transition: width 0.3s;
}
.poll-card .poll-option .poll-label {
position: relative;
z-index: 1;
flex: 1;
font-size: 14px;
}
.poll-card .poll-option .poll-pct {
position: relative;
z-index: 1;
font-size: 12px;
color: #7ec8a3;
min-width: 36px;
text-align: right;
}
.poll-card .poll-option .poll-votes {
position: relative;
z-index: 1;
font-size: 11px;
color: #556677;
margin-left: 8px;
min-width: 20px;
text-align: right;
}
.poll-card .poll-option.voted {
border: 1px solid #2ecc71;
}
.poll-card .poll-footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 8px;
font-size: 12px;
color: #556677;
}
.poll-card .poll-footer button {
padding: 4px 12px;
background: transparent;
border: 1px solid #6a8aaa;
border-radius: 4px;
color: #a0c4ff;
font-size: 11px;
cursor: pointer;
transition: all 0.15s;
}
.poll-card .poll-footer button:hover {
background: #e94560;
border-color: #e94560;
color: #fff;
}
.poll-card .poll-status {
font-size: 11px;
padding: 2px 8px;
border-radius: 4px;
}
.poll-card .poll-status.active { background: #27ae60; color: #fff; }
.poll-card .poll-status.closed { background: #7f8c8d; color: #fff; }
.poll-modal-overlay {
display: none;
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(0,0,0,0.6);
z-index: 2000;
justify-content: center;
align-items: center;
}
.poll-modal-overlay.open { display: flex; }
.poll-modal {
background: #16213e;
border-radius: 12px;
padding: 24px;
width: 90%;
max-width: 500px;
max-height: 80vh;
overflow-y: auto;
}
.poll-modal h2 {
color: #f1c40f;
margin-bottom: 16px;
font-size: 18px;
}
.poll-modal label {
display: block;
font-size: 13px;
color: #a0c4ff;
margin-bottom: 4px;
margin-top: 12px;
}
.poll-modal input[type="text"],
.poll-modal textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid #1a4a7a;
border-radius: 6px;
background: #121e38;
color: #e0e0e0;
font-size: 14px;
outline: none;
}
.poll-modal input:focus, .poll-modal textarea:focus { border-color: #e94560; }
.poll-modal textarea { resize: vertical; min-height: 60px; font-family: inherit; }
.poll-modal .poll-opt-row {
display: flex;
gap: 8px;
margin-bottom: 6px;
align-items: center;
}
.poll-modal .poll-opt-row input { flex: 1; }
.poll-modal .poll-opt-row button {
padding: 6px 10px;
background: transparent;
border: 1px solid #e74c3c;
border-radius: 4px;
color: #e74c3c;
cursor: pointer;
font-size: 14px;
}
.poll-modal .poll-opt-row button:hover { background: #e74c3c; color: #fff; }
.poll-modal .add-opt-btn {
padding: 6px 14px;
background: transparent;
border: 1px dashed #2a4a7a;
border-radius: 6px;
color: #6a8aaa;
cursor: pointer;
font-size: 13px;
margin-top: 4px;
width: 100%;
}
.poll-modal .add-opt-btn:hover {
border-color: #e94560;
color: #e94560;
}
.poll-modal .modal-actions {
display: flex;
gap: 10px;
margin-top: 16px;
justify-content: flex-end;
}
.poll-modal .modal-actions button {
padding: 8px 20px;
border: none;
border-radius: 6px;
font-size: 14px;
cursor: pointer;
font-weight: 600;
}
.poll-modal .modal-actions .btn-cancel {
background: #2a3a5a;
color: #8899aa;
}
.poll-modal .modal-actions .btn-cancel:hover { background: #3a4a6a; }
.poll-modal .modal-actions .btn-create {
background: #e94560;
color: #fff;
}
.poll-modal .modal-actions .btn-create:hover { background: #d63850; }
.poll-modal .modal-actions .btn-create:disabled {
background: #4a5a7a;
cursor: not-allowed;
}
.poll-modal .poll-check-row {
display: flex;
gap: 16px;
margin-top: 12px;
}
.poll-modal .poll-check-row label {
display: flex;
align-items: center;
gap: 6px;
margin: 0;
cursor: pointer;
font-size: 13px;
}
.poll-modal .poll-check-row input[type="checkbox"] {
accent-color: #e94560;
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
<h1>💬 FuckingChat</h1>
<span class="status" id="status-badge">Encrypted 🔒</span>
</div>
<div class="channel-bar" id="channel-bar"></div>
<div class="chat-messages" id="messages"></div>
<div class="gif-picker" id="gif-picker">
<div class="gif-search">
<input type="text" id="gif-search-input" placeholder="Search GIFs...">
<button id="gif-search-btn">Search</button>
</div>
<div class="gif-grid" id="gif-grid"></div>
</div>
<div class="input-area">
<button class="gif-btn" id="gif-btn" title="Add GIF">GIF</button>
<button class="gif-btn" id="poll-btn" title="Create Poll">📊</button>
<input type="text" id="msg-input" placeholder="Type a message..." maxlength="2000">
<button id="send-btn">Send</button>
</div>
</div>
<div class="toast" id="toast"></div>
<div class="poll-modal-overlay" id="poll-modal">
<div class="poll-modal">
<h2>📊 Create Poll</h2>
<label>Question</label>
<textarea id="poll-question" maxlength="500" placeholder="What do you want to ask?"></textarea>
<label>Options</label>
<div id="poll-options">
<div class="poll-opt-row">
<input type="text" class="poll-opt-input" maxlength="200" placeholder="Option 1">
<button class="poll-remove-opt" style="visibility:hidden"></button>
</div>
<div class="poll-opt-row">
<input type="text" class="poll-opt-input" maxlength="200" placeholder="Option 2">
<button class="poll-remove-opt" style="visibility:hidden"></button>
</div>
</div>
<button class="add-opt-btn" id="poll-add-opt">+ Add option</button>
<div class="poll-check-row">
<label><input type="checkbox" id="poll-multiple"> Allow multiple choices</label>
<label><input type="checkbox" id="poll-anonymous"> Anonymous votes</label>
</div>
<div class="modal-actions">
<button class="btn-cancel" id="poll-cancel">Cancel</button>
<button class="btn-create" id="poll-create">Create Poll</button>
</div>
</div>
</div>
<script>
const CHANNELS = [
{ id: 'general', name: 'General', icon: '💬' },
{ id: 'games', name: 'Games', icon: '🎮' },
{ id: 'anime', name: 'Anime', icon: '🎌' },
{ id: 'politics', name: 'Politics', icon: '⚖️' }
];
let currentChannel = 'general';
let pollInterval = null;
let selectedGifUrl = null;
let votedPolls = new Set();
function getClientId() {
let id = localStorage.getItem('clientId');
if (!id) {
id = crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(36).substring(2) + Date.now().toString(36);
localStorage.setItem('clientId', id);
}
return id;
}
function apiFetch(url, options = {}) {
const headers = options.headers || {};
headers['X-Client-Id'] = getClientId();
return fetch(url, { ...options, headers });
}
function loadVotedPolls() {
try {
const saved = localStorage.getItem('votedPolls_' + currentChannel);
if (saved) votedPolls = new Set(JSON.parse(saved));
} catch(e) {}
}
function saveVotedPolls() {
try {
localStorage.setItem('votedPolls_' + currentChannel, JSON.stringify([...votedPolls]));
} catch(e) {}
}
const messagesEl = document.getElementById('messages');
const inputEl = document.getElementById('msg-input');
const sendBtn = document.getElementById('send-btn');
const toastEl = document.getElementById('toast');
const channelBar = document.getElementById('channel-bar');
const gifPicker = document.getElementById('gif-picker');
const gifBtn = document.getElementById('gif-btn');
const gifGrid = document.getElementById('gif-grid');
const gifSearchInput = document.getElementById('gif-search-input');
const gifSearchBtn = document.getElementById('gif-search-btn');
function escapeHtml(text) {
const d = document.createElement('div');
d.textContent = text;
return d.innerHTML;
}
// --- Tenor GIF integration ---
async function searchGifs(query) {
gifGrid.innerHTML = '<div class="gif-spinner">⏳</div>';
try {
const url = query
? `/api/gifs/search?q=${encodeURIComponent(query)}&limit=20`
: `/api/gifs/trending?limit=20`;
const r = await apiFetch(url);
const data = await r.json();
if (data.success) renderGifs(data.results || []);
else gifGrid.innerHTML = '<div class="empty-state">Failed to load GIFs</div>';
} catch (e) {
gifGrid.innerHTML = '<div class="empty-state">Network error</div>';
}
}
function renderGifs(results) {
if (!results.length) {
gifGrid.innerHTML = '<div class="empty-state">No GIFs found</div>';
return;
}
gifGrid.innerHTML = results.map(gif => {
// Klipy format: file = { hd: {gif:{url,width,height}}, md: {...}, sm: {...}, xs: {...} }
const f = gif.file || {};
const thumbUrl = f.sm?.gif?.url || f.xs?.gif?.url || f.md?.gif?.url || '';
const fullUrl = f.hd?.gif?.url || f.md?.gif?.url || thumbUrl;
return `<img src="${escapeHtml(thumbUrl)}" alt="gif" data-gif-url="${escapeHtml(fullUrl)}">`;
}).join('');
gifGrid.querySelectorAll('img').forEach(img => {
img.addEventListener('click', () => {
const gifUrl = img.dataset.gifUrl;
selectedGifUrl = gifUrl;
inputEl.placeholder = 'GIF selected! Add a caption or send...';
gifPicker.classList.remove('open');
showToast('GIF selected ✓', 'success');
});
});
}
gifBtn.addEventListener('click', () => {
const isOpen = gifPicker.classList.toggle('open');
if (isOpen) {
searchGifs('');
}
});
gifSearchBtn.addEventListener('click', () => {
const q = gifSearchInput.value.trim();
searchGifs(q || '');
});
gifSearchInput.addEventListener('keydown', e => {
if (e.key === 'Enter') gifSearchBtn.click();
});
// --- Poll creation ---
const pollModal = document.getElementById('poll-modal');
const pollBtn = document.getElementById('poll-btn');
const pollCancel = document.getElementById('poll-cancel');
const pollCreate = document.getElementById('poll-create');
const pollAddOpt = document.getElementById('poll-add-opt');
const pollQuestion = document.getElementById('poll-question');
const pollOptionsEl = document.getElementById('poll-options');
const pollMultiple = document.getElementById('poll-multiple');
const pollAnonymous = document.getElementById('poll-anonymous');
function resetPollForm() {
pollQuestion.value = '';
pollMultiple.checked = false;
pollAnonymous.checked = false;
pollOptionsEl.innerHTML = '';
for (let i = 1; i <= 2; i++) {
const row = document.createElement('div');
row.className = 'poll-opt-row';
const inp = document.createElement('input');
inp.type = 'text';
inp.className = 'poll-opt-input';
inp.maxLength = 200;
inp.placeholder = 'Option ' + i;
const btn = document.createElement('button');
btn.className = 'poll-remove-opt';
btn.textContent = '✕';
btn.style.visibility = i <= 2 ? 'hidden' : 'visible';
btn.addEventListener('click', () => row.remove());
row.appendChild(inp);
row.appendChild(btn);
pollOptionsEl.appendChild(row);
}
pollCreate.disabled = false;
}
pollBtn.addEventListener('click', () => {
resetPollForm();
pollModal.classList.add('open');
});
pollCancel.addEventListener('click', () => {
pollModal.classList.remove('open');
});
pollModal.addEventListener('click', e => {
if (e.target === pollModal) pollModal.classList.remove('open');
});
pollAddOpt.addEventListener('click', () => {
const rows = pollOptionsEl.querySelectorAll('.poll-opt-row');
const idx = rows.length + 1;
const row = document.createElement('div');
row.className = 'poll-opt-row';
const inp = document.createElement('input');
inp.type = 'text';
inp.className = 'poll-opt-input';
inp.maxLength = 200;
inp.placeholder = 'Option ' + idx;
const btn = document.createElement('button');
btn.className = 'poll-remove-opt';
btn.textContent = '✕';
btn.addEventListener('click', () => row.remove());
row.appendChild(inp);
row.appendChild(btn);
pollOptionsEl.appendChild(row);
// Show remove on all rows if > 2
if (rows.length + 1 > 2) {
pollOptionsEl.querySelectorAll('.poll-remove-opt').forEach(b => b.style.visibility = 'visible');
}
});
pollCreate.addEventListener('click', async () => {
const question = pollQuestion.value.trim();
const optionInputs = pollOptionsEl.querySelectorAll('.poll-opt-input');
const options = [];
optionInputs.forEach(inp => {
const v = inp.value.trim();
if (v) options.push(v);
});
if (!question) { showToast('Please enter a question', 'error'); return; }
if (options.length < 2) { showToast('At least 2 options required', 'error'); return; }
pollCreate.disabled = true;
try {
const r = await apiFetch('/api/polls', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
channel: currentChannel,
question,
options,
is_multiple: pollMultiple.checked,
is_anonymous: pollAnonymous.checked,
})
});
const data = await r.json();
if (data.success) {
showToast('Poll created!', 'success');
pollModal.classList.remove('open');
loadMessages();
} else {
showToast(data.error || 'Failed to create poll', 'error');
}
} catch (e) {
showToast('Network error', 'error');
}
pollCreate.disabled = false;
});
function showToast(msg, type = 'info') {
toastEl.textContent = msg;
toastEl.className = 'toast visible ' + type;
setTimeout(() => toastEl.classList.remove('visible'), 3000);
}
function renderChannels() {
channelBar.innerHTML = '';
CHANNELS.forEach(ch => {
const btn = document.createElement('button');
btn.className = 'channel-btn' + (ch.id === currentChannel ? ' active' : '');
btn.textContent = ch.icon + ' ' + ch.name;
btn.onclick = () => switchChannel(ch.id);
channelBar.appendChild(btn);
});
}
function switchChannel(channel) {
if (channel === currentChannel) return;
currentChannel = channel;
loadVotedPolls();
renderChannels();
messagesEl.innerHTML = '<div class="empty-state">Loading...</div>';
loadMessages();
}
async function loadMessages() {
try {
const r = await apiFetch(`/api/messages?channel=${currentChannel}&limit=50`);
const data = await r.json();
if (data.success) {
window._lastMessages = data.messages || [];
renderMessages(window._lastMessages);
}
else showToast('Failed to load messages', 'error');
} catch (e) {
showToast('Network error', 'error');
}
}
function renderMessages(msgs) {
if (msgs.length === 0) {
messagesEl.innerHTML = '<div class="empty-state">💬 No messages yet. Be the first!</div>';
return;
}
let html = '';
msgs.forEach(msg => {
if (msg.poll) {
const poll = msg.poll;
const isClosed = poll.status === 'closed';
const isExpired = poll.expires_at && Date.now() > poll.expires_at;
const hasVoted = votedPolls.has(poll.id);
const total = poll.total_votes || 0;
let optionsHtml = '';
poll.options.forEach(opt => {
const pct = total > 0 ? Math.round((opt.votes / total) * 100) : 0;
const votedClass = hasVoted ? ' voted' : '';
if (isClosed || isExpired || hasVoted) {
optionsHtml += `<div class="poll-option${votedClass}">
<div class="poll-bar" style="width:${pct}%"></div>
<span class="poll-label">${escapeHtml(opt.label)}</span>
<span class="poll-pct">${pct}%</span>
<span class="poll-votes">${opt.votes}</span>
</div>`;
} else {
optionsHtml += `<div class="poll-option" data-poll-id="${poll.id}" data-opt-id="${opt.id}">
<span class="poll-label">${escapeHtml(opt.label)}</span>
<span class="poll-pct" style="color:#6a8aaa">${opt.votes} votes</span>
</div>`;
}
});
const statusLabel = isClosed ? 'Closed' : isExpired ? 'Expired' : 'Active';
const statusClass = isClosed || isExpired ? 'closed' : 'active';
const creatorLabel = poll.is_anonymous ? 'Anonymous' : escapeHtml(poll.creator_ip);
html += `<div class="poll-card" data-poll-id="${poll.id}">
<div class="poll-question">📊 ${escapeHtml(poll.question)}</div>
<div class="poll-meta">by ${creatorLabel} · ${total} vote${total !== 1 ? 's' : ''} · <span class="poll-status ${statusClass}">${statusLabel}</span></div>
<div class="poll-options">${optionsHtml}</div>
</div>`;
} else {
const time = new Date(msg.timestamp).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' });
let contentHtml = escapeHtml(msg.content || '');
let gifToShow = msg.gif_url || null;
if (!gifToShow) {
const gifMatch = msg.content.match(/https?:\/\/[^\s]+\.(gif|webp)(\?[^\s]*)?/i);
if (gifMatch) gifToShow = gifMatch[0];
}
if (gifToShow) {
contentHtml += `<img class="inline-gif" src="${escapeHtml(gifToShow)}" alt="gif">`;
}
html += `<div class="message">
<div class="msg-ip">🔑 ${escapeHtml(msg.ip)}</div>
<div class="msg-content">${contentHtml}</div>
<div class="msg-time">${time}</div>
</div>`;
}
});
messagesEl.innerHTML = html;
attachPollHandlers();
messagesEl.scrollTop = messagesEl.scrollHeight;
}
function attachPollHandlers() {
document.querySelectorAll('.poll-card .poll-option:not(.voted)').forEach(el => {
const pollId = parseInt(el.dataset.pollId);
const optId = parseInt(el.dataset.optId);
if (!pollId || !optId) return;
if (votedPolls.has(pollId)) return;
el.addEventListener('click', async () => {
try {
const r = await apiFetch(`/api/polls/${pollId}/vote`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ option_id: optId })
});
const data = await r.json();
if (data.success) {
votedPolls.add(pollId);
saveVotedPolls();
showToast('Vote recorded!', 'success');
loadMessages();
} else {
showToast(data.error || 'Failed to vote', 'error');
loadMessages();
}
} catch (e) {
showToast('Network error', 'error');
}
});
});
}
async function sendMessage() {
const text = inputEl.value.trim();
if (!text && !selectedGifUrl) return;
let payload = text;
let gif_url = selectedGifUrl || null;
sendBtn.disabled = true;
try {
const r = await apiFetch('/api/messages', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ channel: currentChannel, message: payload, gif_url: gif_url })
});
const data = await r.json();
if (data.success) {
inputEl.value = '';
selectedGifUrl = null;
inputEl.placeholder = 'Type a message...';
loadMessages();
} else {
showToast(data.error || 'Failed to send', 'error');
}
} catch (e) {
showToast('Network error', 'error');
}
sendBtn.disabled = false;
}
// Init
loadVotedPolls();
renderChannels();
loadMessages();
sendBtn.addEventListener('click', sendMessage);
inputEl.addEventListener('keydown', e => { if (e.key === 'Enter') sendMessage(); });
// Auto-refresh every 3 seconds
setInterval(() => {
loadMessages();
}, 3000);
</script>
</body>
</html>