heystreamer/views/overlay.ejs
Bashy 463c6580f4 Chat: paper slips under the camera, Twitch + 7TV emotes
- public/chat/chat.css + chat.js (window.HSChat): cream paper slips with the
  name on a washi-tape label, newest on top sliding out from under the webcam,
  older ones glide down and fade at the bottom; message_timeout / max_messages
- Emotes: Twitch (tags.emotes) and 7TV global + channel sets (refreshed every
  10 min), zero-width 7TV emotes stack, emote-only messages get bigger emotes
- Deleted messages, timeouts, bans and /clear remove slips at once
- overlay.ejs: old chat CSS/JS removed, new files load only for ?parts=chat

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 12:08:12 +02:00

222 lines
9.3 KiB
Text

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Caveat:wght@700&family=Nunito:wght@800;900&display=swap">
<% const has = p => parts.includes(p); %>
<% if (has('alerts')) { %>
<link rel="stylesheet" href="/alerts/polaroid.css">
<link rel="stylesheet" href="/alerts/cassette.css">
<link rel="stylesheet" href="/alerts/fx.css">
<% } %>
<% if (has('chat')) { %><link rel="stylesheet" href="/chat/chat.css"><% } %>
<% if (has('poll')) { %><link rel="stylesheet" href="/alerts/poll.css"><% } %>
<% if (has('hypebar')) { %><link rel="stylesheet" href="/alerts/hypebar.css"><% } %>
<% if (has('raid')) { %><link rel="stylesheet" href="/alerts/raidcard.css"><% } %>
<%# Now playing (Feishin) is switched off for now. To bring it back: add 'nowplaying' to PARTS in
src/routers/overlay.js, uncomment the /nowplaying route in src/app.js and restore the NOWPLAYING lines here:
<link rel="stylesheet" href="/alerts/nowplaying.css"> (inside an if (has('nowplaying')))
<script src="/alerts/nowplaying.js"></script> (same)
the nowplaying line in ws.onmessage below %>
<style>
/* Legacy text/video action alerts. Values were taken from the layout editor (removed 2026-11-20);
change them here, or override with Custom CSS in Settings. The chat has its own /chat/chat.css. */
:root {
--chat-font-family: Inter, sans-serif;
--alert-left: 50%;
--alert-top: 40%;
--alert-font-size: 32px;
--alert-bg-opacity: 0.7;
--alert-text-color: #ffffff;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: transparent; overflow: hidden; width: 1920px; height: 1080px; }
#alert-box {
position: fixed;
left: var(--alert-left);
top: var(--alert-top);
transform: translate(-50%, -50%);
font-family: var(--chat-font-family);
font-size: var(--alert-font-size);
color: var(--alert-text-color);
background: rgba(0, 0, 0, var(--alert-bg-opacity));
border-radius: 12px;
padding: 16px 28px;
text-align: center;
display: none;
}
#alert-video {
position: fixed;
left: var(--alert-left);
top: var(--alert-top);
transform: translate(-50%, -50%);
max-width: 640px;
max-height: 480px;
border-radius: 12px;
display: none;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(6px); }
to { opacity: 1; transform: translateY(0); }
}
<%- settings.custom_css %>
</style>
</head>
<body>
<% if (has('chat')) { %><div id="chat"></div><% } %>
<% if (has('alerts')) { %><div id="alert-box"></div>
<video id="alert-video" autoplay></video><% } %>
<script src="/vendor/tmi.min.js"></script>
<% if (has('chat')) { %><script src="/chat/chat.js"></script><% } %>
<% if (has('alerts')) { %>
<script src="/alerts/cassette.js"></script>
<script src="/alerts/fx.js"></script>
<script src="/alerts/polaroid.js"></script>
<% } %>
<% if (has('poll')) { %><script src="/alerts/poll.js"></script><% } %>
<% if (has('hypebar')) { %><script src="/alerts/hypebar.js"></script><% } %>
<% if (has('raid')) { %><script src="/alerts/raidcard.js"></script><% } %>
<script>
const S = <%- JSON.stringify(settings) %>;
window.S = S; // chat.js reads message_timeout / max_messages from here (settings_update keeps it current)
// which parts this browser source shows (?parts=chat / ?parts=nochat / ?parts=chat,raid / nothing = all)
const PARTS = new Set(<%- JSON.stringify(parts) %>);
const chat = document.getElementById('chat'); // null when this source has no chat part
const alertBox = document.getElementById('alert-box');
const alertVideo = document.getElementById('alert-video');
let lastMsgTime = 0;
// --- Chat via tmi.js ---
if (S.channel) {
const client = new tmi.Client({ channels: [S.channel] });
client.connect().catch(console.error);
// Watch streaks only exist as a chat USERNOTICE (msg-id=viewermilestone).
// Report them to the server, which logs them and sends the alert back to every overlay.
client.on('raw_message', (_copy, m) => {
const t = m.tags || {};
if (m.command !== 'USERNOTICE' || t['msg-id'] !== 'viewermilestone' || t['msg-param-category'] !== 'watch-streak') return;
sendUp({ type: 'chat_notice', kind: 'watch_streak', id: t.id,
user: t['display-name'] || t.login, streak: t['msg-param-value'] });
});
// channel id for the 7TV channel emotes, and moderation: deleted messages / timeouts / bans leave the screen
if (PARTS.has('chat')) {
client.on('roomstate', (_ch, state) => HSChat.setRoom(state['room-id']));
client.on('messagedeleted', (_ch, _user, _msg, tags) => HSChat.removeId(tags['target-msg-id']));
client.on('timeout', (_ch, user) => HSChat.removeUser(user));
client.on('ban', (_ch, user) => HSChat.removeUser(user));
client.on('clearchat', () => HSChat.clear());
}
client.on('message', (channel, tags, message) => {
if (!PARTS.has('chat')) return; // other sources only listen to chat for watch streaks
const now = Date.now();
const silence = (now - lastMsgTime) / 1000;
console.log(`[gap] silence=${silence.toFixed(1)}s threshold=${S.gap_audio_min_silence}s enabled=${S.gap_audio_enabled} url=${!!S.gap_audio_url} lastMsg=${lastMsgTime>0}`);
if (S.gap_audio_enabled && S.gap_audio_url && lastMsgTime > 0 && silence >= S.gap_audio_min_silence) {
const t = Math.min(
(silence - S.gap_audio_min_silence) / Math.max(S.gap_audio_loud_after - S.gap_audio_min_silence, 1),
1
);
const vol = S.gap_audio_min_volume + t * (S.gap_audio_max_volume - S.gap_audio_min_volume);
console.log(`[gap] PLAY — t=${t.toFixed(2)} vol=${vol.toFixed(2)} min=${S.gap_audio_min_volume} max=${S.gap_audio_max_volume}`);
playAudio(S.gap_audio_url, vol);
}
lastMsgTime = now;
HSChat.add(tags, message, tags['message-type'] === 'action');
});
}
// --- Alert WebSocket ---
// ?overlay=<id> marks this page as the real overlay, so the server accepts chat_notice messages from it
let ws = null;
const pending = [];
function sendUp(obj) {
if (ws && ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(obj));
else pending.push(obj);
}
function connectWS() {
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
ws = new WebSocket(`${proto}://${location.host}/ws/overlay?overlay=${encodeURIComponent(S.overlay_id)}`);
ws.onopen = () => { while (pending.length) ws.send(JSON.stringify(pending.shift())); };
ws.onmessage = (e) => {
const msg = JSON.parse(e.data);
if (msg.type === 'settings_update') {
Object.assign(S, msg.settings);
return;
}
// every source gets every message; each one only reacts to its own parts (so nothing plays twice)
if (msg.type === 'poll') { if (PARTS.has('poll')) PollCard.update(msg.phase, msg.poll); return; }
if (msg.type === 'hype_bar') { if (PARTS.has('hypebar')) HypeBar.update(msg.phase, msg.train); return; }
// NOWPLAYING: if (msg.type === 'nowplaying') { if (PARTS.has('nowplaying')) NowPlaying.update(msg.np); return; }
if (msg.type === 'nowplaying') return;
if (msg.type === 'raid_card') { if (PARTS.has('raid')) RaidCard.update(msg.phase, msg.raid); return; }
if (!PARTS.has('alerts')) return; // alerts and legacy actions below
if (msg.type === 'alert') {
// test buttons: replace other test alerts at once, but never cut off a real alert
if (msg.alert.test === true) PolaroidAlerts.playTest(msg.alert);
else PolaroidAlerts.play(msg.alert);
return;
}
document.dispatchEvent(new CustomEvent(`heystreamer:${msg.event_type}`, { detail: msg }));
handleAction(msg);
};
ws.onclose = () => setTimeout(connectWS, 3000);
}
connectWS();
function handleAction(msg) {
if (msg.action_type === 'sound' && msg.media_url) {
playAudio(msg.media_url, 1.0);
} else if (msg.action_type === 'video' && msg.media_url) {
showVideo(msg.media_url);
} else if (msg.action_type === 'alert') {
showAlert(msg.alert_text || '', msg.username);
}
}
function playAudio(url, volume) {
const a = new Audio(url);
a.volume = Math.max(0, Math.min(1, volume));
a.play().catch(() => {});
}
function showAlert(text, username) {
alertBox.textContent = text.replace('{username}', username || '');
alertBox.style.display = 'block';
setTimeout(() => { alertBox.style.display = 'none'; }, 5000);
}
function showVideo(url) {
alertVideo.src = url;
alertVideo.style.display = 'block';
alertVideo.play();
alertVideo.onended = () => {
alertVideo.style.display = 'none';
alertVideo.src = '';
};
}
function esc(str) {
return String(str)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
<%- settings.custom_js %>
</script>
</body>
</html>