Spaces:
Running
Running
Download index.html from Metaprime/Wavelength: direct link, hf CLI and curl.
- Browser
- Download file 35.6 kB
-
https://huggingface.co/spaces/Metaprime/Wavelength/resolve/main/index.html
- Command line
-
hf download hf://spaces/Metaprime/Wavelength/index.html
-
curl -L -o index.html https://huggingface.co/spaces/Metaprime/Wavelength/resolve/main/index.html
35.6 kB
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> | |
| <title>Wavelength — watch or listen together, from anywhere</title> | |
| <script src="https://cdn.jsdelivr.net/npm/partysocket@1.0.2/dist/partysocket.min.js"></script> | |
| <style> | |
| :root { | |
| --night: #10121a; | |
| --panel: #191c27; | |
| --panel-raised: #20232f; | |
| --line: #2c303e; | |
| --signal: #e8a33d; | |
| --signal-dim: #7a5a2c; | |
| --teal: #5fa8a0; | |
| --paper: #f1efe7; | |
| --paper-dim: #a8a6a0; | |
| --danger: #d9695f; | |
| --radius: 10px; | |
| --serif: "Fraunces", Georgia, "Iowan Old Style", serif; | |
| --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; | |
| } | |
| @font-face { | |
| font-family: "Fraunces"; | |
| src: local("Fraunces"); | |
| } | |
| * { box-sizing: border-box; -webkit-tap-highlight-color: transparent; } | |
| html, body { | |
| margin: 0; padding: 0; height: 100%; | |
| background: var(--night); color: var(--paper); | |
| font-family: var(--sans); | |
| } | |
| @import url('https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,500;9..144,600&family=Inter:wght@400;500;600&display=swap'); | |
| ::selection { background: var(--signal-dim); } | |
| button { font-family: var(--sans); cursor: pointer; } | |
| input, textarea { font-family: var(--sans); } | |
| /* ---------- Join gate ---------- */ | |
| #gate { | |
| position: fixed; inset: 0; z-index: 50; | |
| display: flex; align-items: center; justify-content: center; | |
| background: radial-gradient(circle at 30% 20%, #1a1e2c 0%, var(--night) 65%); | |
| padding: 24px; | |
| } | |
| #gate.hidden { display: none; } | |
| .gate-card { | |
| width: 100%; max-width: 380px; | |
| background: var(--panel); | |
| border: 1px solid var(--line); | |
| border-radius: var(--radius); | |
| padding: 32px 28px; | |
| } | |
| .gate-mark { | |
| width: 34px; height: 34px; border-radius: 50%; | |
| border: 2px solid var(--signal); | |
| position: relative; margin-bottom: 18px; | |
| } | |
| .gate-mark::after { | |
| content: ""; position: absolute; inset: 8px; | |
| border-radius: 50%; background: var(--signal); | |
| animation: pulse 2.4s ease-in-out infinite; | |
| } | |
| @keyframes pulse { 0%,100% { opacity: .35; } 50% { opacity: 1; } } | |
| .gate-card h1 { | |
| font-family: var(--serif); font-weight: 600; font-size: 26px; | |
| margin: 0 0 6px; | |
| } | |
| .gate-card p.sub { color: var(--paper-dim); font-size: 14px; margin: 0 0 24px; line-height: 1.5; } | |
| label { display: block; font-size: 12.5px; color: var(--paper-dim); margin: 0 0 6px; } | |
| .field { margin-bottom: 16px; } | |
| .field input { | |
| width: 100%; background: var(--night); border: 1px solid var(--line); | |
| color: var(--paper); border-radius: 7px; padding: 10px 12px; font-size: 15px; | |
| } | |
| .field input:focus { outline: none; border-color: var(--signal); } | |
| .room-row { display: flex; gap: 8px; } | |
| .room-row input { text-transform: uppercase; letter-spacing: 2px; } | |
| .btn-ghost { | |
| background: transparent; border: 1px solid var(--line); color: var(--paper-dim); | |
| border-radius: 7px; padding: 0 14px; font-size: 13px; white-space: nowrap; | |
| } | |
| .btn-ghost:hover { border-color: var(--signal); color: var(--signal); } | |
| .btn-primary { | |
| width: 100%; background: var(--signal); border: none; color: #241a09; | |
| font-weight: 600; font-size: 15px; padding: 12px; border-radius: 7px; margin-top: 6px; | |
| } | |
| .btn-primary:hover { background: #f0b053; } | |
| .btn-primary:disabled { opacity: .5; cursor: default; } | |
| /* ---------- Main layout ---------- */ | |
| #app { display: none; height: 100%; flex-direction: column; } | |
| #app.active { display: flex; } | |
| header { | |
| display: flex; align-items: center; justify-content: space-between; | |
| padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--panel); | |
| flex-shrink: 0; gap: 12px; flex-wrap: wrap; | |
| } | |
| .brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-size: 18px; font-weight: 600; } | |
| .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); flex-shrink: 0; } | |
| .dot.live { background: var(--teal); box-shadow: 0 0 8px var(--teal); } | |
| .room-chip { | |
| display: flex; align-items: center; gap: 8px; | |
| background: var(--panel-raised); border: 1px solid var(--line); | |
| border-radius: 20px; padding: 5px 6px 5px 14px; font-size: 13px; color: var(--paper-dim); | |
| } | |
| .room-chip b { color: var(--paper); letter-spacing: 1.5px; } | |
| .room-chip button { | |
| background: none; border: none; color: var(--signal); font-size: 12px; | |
| padding: 4px 10px; border-radius: 14px; | |
| } | |
| .room-chip button:hover { background: var(--signal-dim); } | |
| .viewers { font-size: 13px; color: var(--paper-dim); } | |
| .viewers b { color: var(--paper); } | |
| #inviteBanner { | |
| display: flex; align-items: center; gap: 10px; | |
| background: var(--panel-raised); border-bottom: 1px solid var(--line); | |
| padding: 8px 18px; font-size: 12.5px; color: var(--paper-dim); flex-wrap: wrap; | |
| } | |
| #inviteLinkInput { | |
| flex: 1; min-width: 220px; background: var(--night); border: 1px solid var(--line); | |
| color: var(--signal); border-radius: 6px; padding: 6px 10px; font-size: 12.5px; | |
| font-family: monospace; | |
| } | |
| #inviteLinkInput:focus { outline: none; border-color: var(--signal); } | |
| #copyLinkBtn2 { | |
| background: var(--signal); border: none; color: #241a09; font-weight: 600; | |
| border-radius: 6px; padding: 6px 14px; font-size: 12.5px; flex-shrink: 0; | |
| } | |
| main { flex: 1; display: flex; min-height: 0; } | |
| /* Stage */ | |
| #stage { flex: 1; display: flex; flex-direction: column; padding: 20px; min-width: 0; } | |
| #mediaFrame { | |
| flex: 1; background: #000; border-radius: var(--radius); | |
| display: flex; align-items: center; justify-content: center; | |
| position: relative; overflow: hidden; min-height: 240px; | |
| } | |
| #mediaFrame video { width: 100%; height: 100%; object-fit: contain; } | |
| #ytContainer { display: none; width: 100%; height: 100%; } | |
| #ytContainer iframe { width: 100%; height: 100%; display: block; border: none; } | |
| #audioArt { | |
| display: none; flex-direction: column; align-items: center; gap: 14px; color: var(--paper-dim); | |
| } | |
| #audioArt .ring { | |
| width: 120px; height: 120px; border-radius: 50%; | |
| border: 2px solid var(--signal-dim); display: flex; align-items: center; justify-content: center; | |
| } | |
| #audioArt .ring::after { content: "♪"; font-size: 40px; color: var(--signal); } | |
| #emptyStage { | |
| display: flex; flex-direction: column; align-items: center; gap: 10px; color: var(--paper-dim); text-align: center; padding: 20px; | |
| } | |
| #emptyStage h2 { font-family: var(--serif); font-weight: 500; font-size: 20px; color: var(--paper); margin: 0; } | |
| #emptyStage p { margin: 0; font-size: 14px; max-width: 320px; } | |
| .reaction-layer { position: absolute; inset: 0; pointer-events: none; overflow: hidden; } | |
| .flying-emoji { position: absolute; font-size: 30px; animation: rise 2.6s ease-out forwards; } | |
| @keyframes rise { | |
| 0% { transform: translateY(0) scale(0.6); opacity: 0; } | |
| 15% { opacity: 1; transform: translateY(-10px) scale(1); } | |
| 100% { transform: translateY(-220px) scale(1.1); opacity: 0; } | |
| } | |
| /* Controls */ | |
| #controls { margin-top: 12px; } | |
| #seekbar { | |
| position: relative; height: 26px; display: flex; align-items: center; cursor: pointer; | |
| } | |
| #seekTrack { | |
| height: 4px; width: 100%; background: var(--line); border-radius: 2px; position: relative; | |
| } | |
| #seekFill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--signal); border-radius: 2px; width: 0%; } | |
| .marker { | |
| position: absolute; top: -4px; width: 3px; height: 12px; background: var(--teal); border-radius: 2px; cursor: pointer; | |
| } | |
| .transport { display: flex; align-items: center; gap: 14px; margin-top: 8px; } | |
| #playBtn { | |
| width: 38px; height: 38px; border-radius: 50%; background: var(--panel-raised); | |
| border: 1px solid var(--line); color: var(--paper); font-size: 15px; flex-shrink: 0; | |
| display: flex; align-items: center; justify-content: center; | |
| } | |
| #playBtn:hover { border-color: var(--signal); color: var(--signal); } | |
| .time { font-size: 12.5px; color: var(--paper-dim); font-variant-numeric: tabular-nums; min-width: 92px; } | |
| .spacer { flex: 1; } | |
| .icon-btn { | |
| background: none; border: 1px solid var(--line); color: var(--paper-dim); | |
| border-radius: 7px; padding: 7px 12px; font-size: 13px; | |
| } | |
| .icon-btn:hover { border-color: var(--signal); color: var(--signal); } | |
| .load-row { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; } | |
| .load-row input { | |
| flex: 1; min-width: 200px; background: var(--panel); border: 1px solid var(--line); | |
| color: var(--paper); border-radius: 7px; padding: 9px 12px; font-size: 13.5px; | |
| } | |
| .load-row input:focus { outline: none; border-color: var(--signal); } | |
| .load-row button { | |
| background: var(--signal); border: none; color: #241a09; font-weight: 600; | |
| border-radius: 7px; padding: 9px 16px; font-size: 13.5px; | |
| } | |
| .reactions-row { display: flex; gap: 8px; margin-top: 14px; } | |
| .reactions-row button { | |
| background: var(--panel-raised); border: 1px solid var(--line); border-radius: 8px; | |
| font-size: 19px; padding: 6px 10px; | |
| } | |
| .reactions-row button:hover { border-color: var(--signal); transform: translateY(-1px); } | |
| /* Sidebar */ | |
| #side { | |
| width: 320px; flex-shrink: 0; border-left: 1px solid var(--line); | |
| background: var(--panel); display: flex; flex-direction: column; min-height: 0; | |
| } | |
| .tabs { display: flex; border-bottom: 1px solid var(--line); flex-shrink: 0; } | |
| .tab { | |
| flex: 1; background: none; border: none; color: var(--paper-dim); | |
| padding: 13px; font-size: 13.5px; font-weight: 500; border-bottom: 2px solid transparent; | |
| } | |
| .tab.active { color: var(--paper); border-bottom-color: var(--signal); } | |
| .panel { flex: 1; overflow-y: auto; padding: 14px; display: none; flex-direction: column; gap: 10px; } | |
| .panel.active { display: flex; } | |
| .msg { font-size: 13.5px; line-height: 1.45; } | |
| .msg .who { color: var(--teal); font-weight: 600; margin-right: 6px; } | |
| .msg .when { color: var(--paper-dim); font-size: 11px; margin-left: 6px; } | |
| .msg.system { color: var(--paper-dim); font-style: italic; } | |
| .moment { border-left: 2px solid var(--signal); padding: 6px 0 6px 10px; font-size: 13.5px; } | |
| .moment .stamp { | |
| color: var(--signal); font-variant-numeric: tabular-nums; font-size: 12px; cursor: pointer; | |
| display: inline-block; margin-bottom: 2px; | |
| } | |
| .moment .stamp:hover { text-decoration: underline; } | |
| .moment .who { color: var(--teal); font-weight: 600; } | |
| .empty-hint { color: var(--paper-dim); font-size: 13px; text-align: center; margin-top: 30px; } | |
| .composer { | |
| display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); flex-shrink: 0; | |
| } | |
| .composer input { | |
| flex: 1; background: var(--night); border: 1px solid var(--line); color: var(--paper); | |
| border-radius: 7px; padding: 9px 11px; font-size: 13.5px; | |
| } | |
| .composer input:focus { outline: none; border-color: var(--signal); } | |
| .composer button { | |
| background: var(--panel-raised); border: 1px solid var(--line); color: var(--signal); | |
| border-radius: 7px; padding: 0 14px; font-size: 13px; font-weight: 600; | |
| } | |
| .composer button:hover { border-color: var(--signal); } | |
| @media (max-width: 780px) { | |
| main { flex-direction: column; } | |
| #side { width: 100%; border-left: none; border-top: 1px solid var(--line); height: 42vh; } | |
| #stage { padding: 14px; } | |
| } | |
| @media (prefers-reduced-motion: reduce) { | |
| .flying-emoji, .gate-mark::after { animation: none ; } | |
| } | |
| :focus-visible { outline: 2px solid var(--signal); outline-offset: 2px; } | |
| </style> | |
| </head> | |
| <body> | |
| <!-- Join gate --> | |
| <div id="gate"> | |
| <div class="gate-card"> | |
| <div class="gate-mark"></div> | |
| <h1>Wavelength</h1> | |
| <p class="sub">One room, one signal. Everyone hears and sees the same thing at the same moment, wherever they're tuning in from.</p> | |
| <div class="field"> | |
| <label for="nameInput">Your name</label> | |
| <input id="nameInput" type="text" maxlength="20" placeholder="How should people see you?"> | |
| </div> | |
| <div class="field"> | |
| <label for="roomInput">Room code</label> | |
| <div class="room-row"> | |
| <input id="roomInput" type="text" maxlength="6" placeholder="Auto-generated"> | |
| <button class="btn-ghost" id="newCodeBtn" type="button">New code</button> | |
| </div> | |
| </div> | |
| <button class="btn-primary" id="enterBtn" type="button">Enter the room</button> | |
| </div> | |
| </div> | |
| <!-- Main app --> | |
| <div id="app"> | |
| <header> | |
| <div class="brand"><span class="dot" id="statusDot"></span> Wavelength</div> | |
| <div class="room-chip"> | |
| Room <b id="roomLabel">——</b> | |
| <button id="copyLinkBtn" type="button">Copy invite link</button> | |
| </div> | |
| <div class="viewers"><b id="viewerCount">1</b> listening in</div> | |
| </header> | |
| <div id="inviteBanner"> | |
| <span>Share this exact link so others drop into your room:</span> | |
| <input id="inviteLinkInput" type="text" readonly> | |
| <button id="copyLinkBtn2" type="button">Copy</button> | |
| </div> | |
| <main> | |
| <div id="stage"> | |
| <div id="mediaFrame"> | |
| <video id="videoEl" playsinline></video> | |
| <div id="audioArt"><div class="ring"></div><span id="audioLabel">Audio room</span></div> | |
| <div id="ytContainer"></div> | |
| <div id="emptyStage"> | |
| <h2>Nothing playing yet</h2> | |
| <p>Paste a direct video/audio link or a YouTube link below to start the room for everyone.</p> | |
| </div> | |
| <div class="reaction-layer" id="reactionLayer"></div> | |
| </div> | |
| <div id="controls"> | |
| <div id="seekbar"> | |
| <div id="seekTrack"> | |
| <div id="seekFill"></div> | |
| <div id="markerLayer"></div> | |
| </div> | |
| </div> | |
| <div class="transport"> | |
| <button id="playBtn" type="button" aria-label="Play">▶</button> | |
| <span class="time"><span id="curTime">0:00</span> / <span id="durTime">0:00</span></span> | |
| <span class="spacer"></span> | |
| <button class="icon-btn" id="pinMomentBtn" type="button">Pin this moment</button> | |
| </div> | |
| <div class="load-row"> | |
| <input id="mediaUrlInput" type="text" placeholder="https://example.com/video.mp4 or .mp3"> | |
| <button id="loadBtn" type="button">Play for everyone</button> | |
| </div> | |
| <div class="reactions-row" id="reactionsRow"> | |
| <button data-emoji="🔥">🔥</button> | |
| <button data-emoji="😂">😂</button> | |
| <button data-emoji="😮">😮</button> | |
| <button data-emoji="❤️">❤️</button> | |
| <button data-emoji="👏">👏</button> | |
| <button data-emoji="😢">😢</button> | |
| </div> | |
| </div> | |
| </div> | |
| <div id="side"> | |
| <div class="tabs"> | |
| <button class="tab active" data-tab="chat">Chat</button> | |
| <button class="tab" data-tab="moments">Moments</button> | |
| </div> | |
| <div class="panel active" id="chatPanel"></div> | |
| <div class="panel" id="momentsPanel"></div> | |
| <div class="composer" id="chatComposer"> | |
| <input id="chatInput" type="text" maxlength="240" placeholder="Say something to the room…"> | |
| <button id="chatSendBtn" type="button">Send</button> | |
| </div> | |
| </div> | |
| </main> | |
| </div> | |
| <script> | |
| (function () { | |
| "use strict"; | |
| // ---------- Identity & room ---------- | |
| let myName = ""; | |
| let roomCode = ""; | |
| let socket = null; | |
| let viewers = new Map(); // name -> lastSeen | |
| function randomCode(len) { | |
| const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; | |
| const bytes = new Uint32Array(len); | |
| crypto.getRandomValues(bytes); | |
| let out = ""; | |
| for (let i = 0; i < len; i++) out += chars[bytes[i] % chars.length]; | |
| return out; | |
| } | |
| function suggestName() { | |
| const adjectives = ["Quiet", "Amber", "Distant", "Late", "Curious", "Nightowl", "Calm", "Bright"]; | |
| const a = adjectives[Math.floor(Math.random() * adjectives.length)]; | |
| return a + "-" + randomCode(3); | |
| } | |
| const nameInput = document.getElementById("nameInput"); | |
| const roomInput = document.getElementById("roomInput"); | |
| nameInput.value = suggestName(); | |
| function codeFromURL() { | |
| const q = new URLSearchParams(location.search).get("room"); | |
| if (q) return q.toUpperCase(); | |
| const m = location.hash.match(/room=([A-Z0-9]{4,8})/i); // back-compat with old links | |
| return m ? m[1].toUpperCase() : ""; | |
| } | |
| const sharedRoom = codeFromURL(); | |
| if (sharedRoom) { | |
| roomInput.value = sharedRoom; | |
| roomInput.readOnly = true; | |
| document.getElementById("newCodeBtn").style.display = "none"; | |
| const roomLabel = document.querySelector('label[for="roomInput"]'); | |
| if (roomLabel) roomLabel.textContent = "Joining room (from your invite link)"; | |
| } else { | |
| roomInput.value = randomCode(5); | |
| } | |
| document.getElementById("newCodeBtn").addEventListener("click", () => { | |
| roomInput.value = randomCode(5); | |
| }); | |
| document.getElementById("enterBtn").addEventListener("click", enterRoom); | |
| [nameInput, roomInput].forEach(el => el.addEventListener("keydown", e => { | |
| if (e.key === "Enter") enterRoom(); | |
| })); | |
| function enterRoom() { | |
| const name = nameInput.value.trim() || suggestName(); | |
| const code = (roomInput.value.trim() || randomCode(5)).toUpperCase().slice(0, 8); | |
| myName = escapeText(name).slice(0, 20); | |
| roomCode = code; | |
| try { | |
| history.replaceState(null, "", "?room=" + roomCode); | |
| } catch (e) { /* some embedded/sandboxed contexts restrict this — safe to ignore */ } | |
| document.getElementById("roomLabel").textContent = roomCode; | |
| refreshInviteLink(); | |
| document.getElementById("gate").classList.add("hidden"); | |
| document.getElementById("app").classList.add("active"); | |
| connect(); | |
| } | |
| function escapeText(s) { | |
| const d = document.createElement("div"); | |
| d.textContent = s; | |
| return d.textContent; | |
| } | |
| // ---------- Connection ---------- | |
| function connect() { | |
| socket = new PartySocket({ | |
| host: "partykit-provider.partykit.dev", | |
| room: "wavelength-" + roomCode | |
| }); | |
| socket.addEventListener("open", () => { | |
| setLive(true); | |
| viewers.set(myName, Date.now()); | |
| renderViewerCount(); | |
| send({ type: "PRESENCE", name: myName, action: "join" }); | |
| send({ type: "REQUEST_STATE" }); | |
| }); | |
| socket.addEventListener("close", () => { | |
| setLive(false); | |
| setTimeout(() => { if (socket) connect(); }, 2000); | |
| }); | |
| socket.addEventListener("error", () => setLive(false)); | |
| socket.addEventListener("message", (event) => { | |
| let data; | |
| try { data = JSON.parse(event.data); } catch (e) { return; } | |
| handleMessage(data); | |
| }); | |
| window.addEventListener("beforeunload", () => { | |
| send({ type: "PRESENCE", name: myName, action: "leave" }); | |
| }); | |
| } | |
| function send(obj) { | |
| if (socket && socket.readyState === WebSocket.OPEN) socket.send(JSON.stringify(obj)); | |
| } | |
| function setLive(isLive) { | |
| document.getElementById("statusDot").classList.toggle("live", isLive); | |
| } | |
| function renderViewerCount() { | |
| const cutoff = Date.now() - 30000; | |
| for (const [name, ts] of viewers) if (ts < cutoff) viewers.delete(name); | |
| document.getElementById("viewerCount").textContent = Math.max(viewers.size, 1); | |
| } | |
| setInterval(renderViewerCount, 5000); | |
| // ---------- Media state ---------- | |
| const videoEl = document.getElementById("videoEl"); | |
| const audioEl = new Audio(); | |
| audioEl.crossOrigin = "anonymous"; | |
| let mediaType = null; // 'video' | 'audio' | 'youtube' | |
| let currentUrl = null; | |
| let applyingRemote = false; | |
| let comments = []; | |
| let chatLog = []; | |
| let hasSyncedHistory = false; | |
| let ytPlayer = null; | |
| let ytPendingVideoId = null; | |
| let ytPollTimer = null; | |
| const ytAdapter = { | |
| get paused() { return !ytPlayer || typeof ytPlayer.getPlayerState !== "function" || ytPlayer.getPlayerState() !== 1; }, | |
| get currentTime() { return (ytPlayer && typeof ytPlayer.getCurrentTime === "function") ? ytPlayer.getCurrentTime() : 0; }, | |
| set currentTime(t) { if (ytPlayer && typeof ytPlayer.seekTo === "function") ytPlayer.seekTo(t, true); }, | |
| get duration() { return (ytPlayer && typeof ytPlayer.getDuration === "function") ? ytPlayer.getDuration() : 0; }, | |
| play() { if (ytPlayer && typeof ytPlayer.playVideo === "function") ytPlayer.playVideo(); }, | |
| pause() { if (ytPlayer && typeof ytPlayer.pauseVideo === "function") ytPlayer.pauseVideo(); } | |
| }; | |
| function activeEl() { | |
| if (mediaType === "audio") return audioEl; | |
| if (mediaType === "youtube") return ytAdapter; | |
| return videoEl; | |
| } | |
| function extractYouTubeId(url) { | |
| try { | |
| const u = new URL(url); | |
| if (u.hostname.replace(/^www\./, "") === "youtu.be") return u.pathname.slice(1) || null; | |
| if (u.hostname.replace(/^www\./, "") === "youtube.com" || u.hostname.replace(/^www\./, "") === "m.youtube.com") { | |
| if (u.pathname === "/watch") return u.searchParams.get("v"); | |
| const m = u.pathname.match(/^\/(shorts|embed)\/([^/?]+)/); | |
| if (m) return m[2]; | |
| } | |
| } catch (e) { /* not a valid URL */ } | |
| return null; | |
| } | |
| function detectType(url) { | |
| if (extractYouTubeId(url)) return "youtube"; | |
| const clean = url.split("?")[0].toLowerCase(); | |
| if (/\.(mp3|wav|m4a|aac|ogg|flac)$/.test(clean)) return "audio"; | |
| return "video"; | |
| } | |
| function ensureYouTubeAPI(onReady) { | |
| if (window.YT && window.YT.Player) { onReady(); return; } | |
| const prevCallback = window.onYouTubeIframeAPIReady; | |
| window.onYouTubeIframeAPIReady = function () { | |
| if (typeof prevCallback === "function") prevCallback(); | |
| onReady(); | |
| }; | |
| if (!document.getElementById("ytApiScript")) { | |
| const tag = document.createElement("script"); | |
| tag.id = "ytApiScript"; | |
| tag.src = "https://www.youtube.com/iframe_api"; | |
| document.head.appendChild(tag); | |
| } | |
| } | |
| function loadYouTube(videoId) { | |
| if (!videoId) return; | |
| ytPendingVideoId = videoId; | |
| ensureYouTubeAPI(() => { | |
| if (ytPlayer && typeof ytPlayer.loadVideoById === "function") { | |
| ytPlayer.loadVideoById(ytPendingVideoId); | |
| return; | |
| } | |
| if (ytPlayer) return; // already being created | |
| ytPlayer = new YT.Player("ytContainer", { | |
| videoId: ytPendingVideoId, | |
| playerVars: { playsinline: 1, rel: 0, modestbranding: 1 }, | |
| events: { | |
| onReady: () => wireMediaEvents(), | |
| onStateChange: onYTStateChange | |
| } | |
| }); | |
| }); | |
| } | |
| function onYTStateChange(e) { | |
| if (applyingRemote) return; | |
| if (e.data === 1) { broadcastState(); setPlayIcon(true); } | |
| else if (e.data === 2) { broadcastState(); setPlayIcon(false); } | |
| } | |
| function whenMediaReady(cb) { | |
| if (mediaType === "youtube") { | |
| const poll = () => { | |
| if (ytPlayer && typeof ytPlayer.getDuration === "function" && ytPlayer.getDuration() > 0) cb(); | |
| else setTimeout(poll, 150); | |
| }; | |
| poll(); | |
| return; | |
| } | |
| const el = activeEl(); | |
| if (el.readyState >= 1) cb(); | |
| else el.addEventListener("loadedmetadata", cb, { once: true }); | |
| } | |
| function loadMedia(url, type, broadcast) { | |
| mediaType = type; | |
| currentUrl = url; | |
| document.getElementById("emptyStage").style.display = "none"; | |
| videoEl.style.display = "none"; | |
| document.getElementById("audioArt").style.display = "none"; | |
| document.getElementById("ytContainer").style.display = "none"; | |
| if (type === "audio") { | |
| document.getElementById("audioArt").style.display = "flex"; | |
| audioEl.src = url; | |
| audioEl.load(); | |
| } else if (type === "youtube") { | |
| document.getElementById("ytContainer").style.display = "block"; | |
| loadYouTube(extractYouTubeId(url)); | |
| } else { | |
| videoEl.style.display = "block"; | |
| videoEl.src = url; | |
| videoEl.load(); | |
| } | |
| wireMediaEvents(); | |
| if (broadcast) broadcastState(); | |
| } | |
| function wireMediaEvents() { | |
| if (mediaType === "youtube") { | |
| if (!ytPollTimer) ytPollTimer = setInterval(updateSeekUI, 400); | |
| return; | |
| } | |
| const el = activeEl(); | |
| if (el._wired) return; | |
| el._wired = true; | |
| el.addEventListener("timeupdate", updateSeekUI); | |
| el.addEventListener("loadedmetadata", updateSeekUI); | |
| el.addEventListener("play", () => { if (!applyingRemote) broadcastState(); setPlayIcon(true); }); | |
| el.addEventListener("pause", () => { if (!applyingRemote) broadcastState(); setPlayIcon(false); }); | |
| } | |
| function setPlayIcon(playing) { | |
| document.getElementById("playBtn").textContent = playing ? "❚❚" : "▶"; | |
| } | |
| function fmt(t) { | |
| if (!isFinite(t) || t < 0) t = 0; | |
| const m = Math.floor(t / 60), s = Math.floor(t % 60); | |
| return m + ":" + String(s).padStart(2, "0"); | |
| } | |
| function updateSeekUI() { | |
| const el = activeEl(); | |
| const dur = el.duration || 0; | |
| const pct = dur ? (el.currentTime / dur) * 100 : 0; | |
| document.getElementById("seekFill").style.width = pct + "%"; | |
| document.getElementById("curTime").textContent = fmt(el.currentTime); | |
| document.getElementById("durTime").textContent = fmt(dur); | |
| } | |
| document.getElementById("playBtn").addEventListener("click", () => { | |
| const el = activeEl(); | |
| if (!currentUrl) return; | |
| if (el.paused) el.play().catch(() => {}); else el.pause(); | |
| }); | |
| const seekbar = document.getElementById("seekbar"); | |
| seekbar.addEventListener("click", (e) => { | |
| const el = activeEl(); | |
| if (!currentUrl || !el.duration) return; | |
| const rect = seekbar.getBoundingClientRect(); | |
| const pct = Math.min(1, Math.max(0, (e.clientX - rect.left) / rect.width)); | |
| el.currentTime = pct * el.duration; | |
| broadcastState(); | |
| }); | |
| document.getElementById("loadBtn").addEventListener("click", () => { | |
| const raw = document.getElementById("mediaUrlInput").value.trim(); | |
| if (!raw) return; | |
| let url; | |
| try { url = new URL(raw); } catch (e) { alert("That doesn't look like a valid URL."); return; } | |
| if (url.protocol !== "https:" && url.protocol !== "http:") { alert("Use an http(s) link."); return; } | |
| const type = detectType(url.href); | |
| if (type === "youtube" && !extractYouTubeId(url.href)) { alert("Couldn't find a video ID in that YouTube link."); return; } | |
| const finalUrl = type === "youtube" ? ("https://www.youtube.com/watch?v=" + extractYouTubeId(url.href)) : url.href; | |
| comments = []; | |
| renderMoments(); | |
| loadMedia(finalUrl, type, true); | |
| addSystemMsg(myName + " loaded a new " + (type === "youtube" ? "YouTube video" : type) + " for the room"); | |
| }); | |
| function broadcastState() { | |
| if (!currentUrl) return; | |
| const el = activeEl(); | |
| send({ | |
| type: "STATE", | |
| url: currentUrl, | |
| mediaType: mediaType, | |
| isPlaying: !el.paused, | |
| currentTime: el.currentTime, | |
| ts: Date.now() | |
| }); | |
| } | |
| setInterval(() => { if (currentUrl && !activeEl().paused) broadcastState(); }, 6000); | |
| function applyState(data) { | |
| applyingRemote = true; | |
| if (data.url !== currentUrl) { | |
| loadMedia(data.url, data.mediaType, false); | |
| } | |
| const el = activeEl(); | |
| const latency = data.isPlaying ? (Date.now() - data.ts) / 1000 : 0; | |
| const target = data.currentTime + Math.max(0, latency); | |
| const applyDrift = () => { | |
| if (Math.abs(el.currentTime - target) > 0.75) el.currentTime = target; | |
| if (data.isPlaying && el.paused) el.play().catch(() => {}); | |
| if (!data.isPlaying && !el.paused) el.pause(); | |
| setPlayIcon(data.isPlaying); | |
| applyingRemote = false; | |
| renderAllMarkers(); | |
| }; | |
| whenMediaReady(applyDrift); | |
| } | |
| // ---------- Reactions ---------- | |
| document.getElementById("reactionsRow").addEventListener("click", (e) => { | |
| const btn = e.target.closest("button[data-emoji]"); | |
| if (!btn) return; | |
| const emoji = btn.dataset.emoji; | |
| send({ type: "REACTION", emoji, name: myName }); | |
| spawnEmoji(emoji); | |
| }); | |
| function spawnEmoji(emoji) { | |
| const layer = document.getElementById("reactionLayer"); | |
| const span = document.createElement("span"); | |
| span.className = "flying-emoji"; | |
| span.textContent = emoji; | |
| span.style.left = (10 + Math.random() * 80) + "%"; | |
| layer.appendChild(span); | |
| setTimeout(() => span.remove(), 2700); | |
| } | |
| // ---------- Chat ---------- | |
| function addChatMsg(name, text, when, system) { | |
| const panel = document.getElementById("chatPanel"); | |
| const div = document.createElement("div"); | |
| div.className = "msg" + (system ? " system" : ""); | |
| if (!system) { | |
| const who = document.createElement("span"); | |
| who.className = "who"; who.textContent = name; | |
| div.appendChild(who); | |
| } | |
| div.appendChild(document.createTextNode(text)); | |
| const when_ = document.createElement("span"); | |
| when_.className = "when"; when_.textContent = new Date(when).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); | |
| div.appendChild(when_); | |
| panel.appendChild(div); | |
| panel.scrollTop = panel.scrollHeight; | |
| } | |
| function addSystemMsg(text) { addChatMsg(null, text, Date.now(), true); } | |
| document.getElementById("chatSendBtn").addEventListener("click", sendChat); | |
| document.getElementById("chatInput").addEventListener("keydown", e => { if (e.key === "Enter") sendChat(); }); | |
| function sendChat() { | |
| const input = document.getElementById("chatInput"); | |
| const text = input.value.trim(); | |
| if (!text) return; | |
| const msg = { type: "CHAT", name: myName, text: text.slice(0, 240), ts: Date.now() }; | |
| send(msg); | |
| chatLog.push({ name: myName, text: msg.text, ts: msg.ts }); | |
| addChatMsg(myName, msg.text, msg.ts, false); | |
| input.value = ""; | |
| } | |
| // ---------- Moments (timestamped comments) ---------- | |
| document.getElementById("pinMomentBtn").addEventListener("click", () => { | |
| if (!currentUrl) { alert("Load something first."); return; } | |
| const text = prompt("Pin a note at " + fmt(activeEl().currentTime) + ":"); | |
| if (!text) return; | |
| const c = { type: "COMMENT", name: myName, text: text.slice(0, 200), atTime: activeEl().currentTime, ts: Date.now() }; | |
| send(c); | |
| addMoment(c); | |
| }); | |
| function addMoment(c) { | |
| comments.push(c); | |
| renderMoments(); | |
| renderMarker(c); | |
| } | |
| function renderMoments() { | |
| const panel = document.getElementById("momentsPanel"); | |
| panel.innerHTML = ""; | |
| if (!comments.length) { | |
| const p = document.createElement("div"); | |
| p.className = "empty-hint"; | |
| p.textContent = "Pinned moments will show up here — click a timestamp to jump right to it."; | |
| panel.appendChild(p); | |
| return; | |
| } | |
| comments.slice().sort((a, b) => a.atTime - b.atTime).forEach(c => { | |
| const div = document.createElement("div"); | |
| div.className = "moment"; | |
| const stamp = document.createElement("span"); | |
| stamp.className = "stamp"; stamp.textContent = fmt(c.atTime); | |
| stamp.addEventListener("click", () => { | |
| const el = activeEl(); | |
| if (el.duration) { el.currentTime = c.atTime; broadcastState(); } | |
| }); | |
| const who = document.createElement("span"); | |
| who.className = "who"; who.textContent = " " + c.name + " — "; | |
| div.appendChild(stamp); | |
| div.appendChild(who); | |
| div.appendChild(document.createTextNode(c.text)); | |
| panel.appendChild(div); | |
| }); | |
| } | |
| function renderAllMarkers() { | |
| const layer = document.getElementById("markerLayer"); | |
| if (!layer) return; | |
| layer.innerHTML = ""; | |
| comments.forEach(renderMarker); | |
| } | |
| function renderMarker(c) { | |
| const el = activeEl(); | |
| if (!el.duration) return; | |
| const layer = document.getElementById("markerLayer"); | |
| const dot = document.createElement("div"); | |
| dot.className = "marker"; | |
| dot.style.left = (c.atTime / el.duration * 100) + "%"; | |
| dot.title = c.name + ": " + c.text; | |
| dot.addEventListener("click", (e) => { | |
| e.stopPropagation(); | |
| el.currentTime = c.atTime; broadcastState(); | |
| }); | |
| layer.appendChild(dot); | |
| } | |
| // ---------- Tabs ---------- | |
| document.querySelectorAll(".tab").forEach(tab => { | |
| tab.addEventListener("click", () => { | |
| document.querySelectorAll(".tab").forEach(t => t.classList.remove("active")); | |
| document.querySelectorAll(".panel").forEach(p => p.classList.remove("active")); | |
| tab.classList.add("active"); | |
| document.getElementById(tab.dataset.tab + "Panel").classList.add("active"); | |
| document.getElementById("chatComposer").style.display = tab.dataset.tab === "chat" ? "flex" : "none"; | |
| }); | |
| }); | |
| // ---------- Incoming messages ---------- | |
| function handleMessage(data) { | |
| switch (data.type) { | |
| case "STATE": | |
| applyState(data); | |
| break; | |
| case "REQUEST_STATE": | |
| if (currentUrl) setTimeout(broadcastState, Math.random() * 400); | |
| if (chatLog.length || comments.length) { | |
| setTimeout(() => send({ | |
| type: "HISTORY", | |
| chatLog: chatLog.slice(-150), | |
| comments: comments | |
| }), 200 + Math.random() * 400); | |
| } | |
| break; | |
| case "HISTORY": | |
| if (hasSyncedHistory) break; | |
| hasSyncedHistory = true; | |
| (data.chatLog || []).forEach(m => { | |
| chatLog.push(m); | |
| addChatMsg(escapeText(m.name), escapeText(m.text), m.ts, false); | |
| }); | |
| if (data.comments && data.comments.length) { | |
| comments = comments.concat(data.comments.map(c => ({ | |
| name: escapeText(c.name), text: escapeText(c.text), atTime: c.atTime, ts: c.ts | |
| }))); | |
| renderMoments(); | |
| whenMediaReady(renderAllMarkers); | |
| } | |
| break; | |
| case "REACTION": | |
| spawnEmoji(data.emoji); | |
| break; | |
| case "CHAT": | |
| addChatMsg(data.name, escapeText(data.text), data.ts, false); | |
| break; | |
| case "COMMENT": | |
| addMoment({ name: escapeText(data.name), text: escapeText(data.text), atTime: data.atTime, ts: data.ts }); | |
| break; | |
| case "PRESENCE": | |
| if (data.action === "join") { | |
| viewers.set(data.name, Date.now()); | |
| if (data.name !== myName) addSystemMsg(data.name + " joined the room"); | |
| } else { | |
| viewers.delete(data.name); | |
| addSystemMsg(data.name + " left"); | |
| } | |
| renderViewerCount(); | |
| break; | |
| } | |
| } | |
| // ---------- Invite link ---------- | |
| function refreshInviteLink() { | |
| const input = document.getElementById("inviteLinkInput"); | |
| if (input) input.value = location.href; | |
| } | |
| function copyInviteLink(btn) { | |
| refreshInviteLink(); | |
| const input = document.getElementById("inviteLinkInput"); | |
| const text = location.href; | |
| const showCopied = () => { | |
| const old = btn.textContent; | |
| btn.textContent = "Copied"; | |
| setTimeout(() => btn.textContent = old, 1500); | |
| }; | |
| if (navigator.clipboard && navigator.clipboard.writeText) { | |
| navigator.clipboard.writeText(text).then(showCopied).catch(() => fallbackCopy(input, showCopied)); | |
| } else { | |
| fallbackCopy(input, showCopied); | |
| } | |
| } | |
| function fallbackCopy(input, onDone) { | |
| try { | |
| input.focus(); | |
| input.select(); | |
| input.setSelectionRange(0, input.value.length); | |
| const ok = document.execCommand("copy"); | |
| if (ok) onDone(); | |
| } catch (e) { /* selection is still visible for the user to copy manually */ } | |
| } | |
| document.getElementById("inviteLinkInput").addEventListener("click", function () { | |
| this.focus(); | |
| this.select(); | |
| }); | |
| document.getElementById("copyLinkBtn").addEventListener("click", () => copyInviteLink(document.getElementById("copyLinkBtn"))); | |
| document.getElementById("copyLinkBtn2").addEventListener("click", () => copyInviteLink(document.getElementById("copyLinkBtn2"))); | |
| })(); | |
| </script> | |
| </body> | |
| </html> | |