/* Public playlist discovery runs in the visitor's browser using YouTube's IFrame Player API. No ChatGPT, paid service, API key or scheduled job. */ (() => { 'use strict'; const known = new Set([...document.querySelectorAll('[data-video-id]')].map(el => el.dataset.videoId)); const seen = new Set(known); const valid = id => typeof id === 'string' && /^[A-Za-z0-9_-]{11}$/.test(id); const metadata = new Map(); function publicTitle(id) { if (!metadata.has(id)) { const endpoint = new URL('https://www.youtube.com/oembed'); endpoint.searchParams.set('url','https://www.youtube.com/watch?v=' + id); endpoint.searchParams.set('format','json'); metadata.set(id, fetch(endpoint,{credentials:'omit',signal:AbortSignal.timeout(8000)}) .then(r => { if (!r.ok) throw Error('public metadata unavailable'); return r.json(); }) .then(data => { if (typeof data.title !== 'string' || !data.title.trim()) throw Error('title unavailable'); return data.title; })); } return metadata.get(id); } async function addVideo(id, container, label) { if (!valid(id) || seen.has(id)) return; seen.add(id); let title; try { title = await publicTitle(id); } catch (_) { return; } // Never publish cards for unavailable/private metadata. container.hidden = false; const card = document.createElement('article'); card.className = 'video-tape'; card.dataset.videoId = id; card.dataset.liveAddition = 'true'; const category = document.createElement('div'); category.className = 'eyebrow'; category.textContent = label; const heading = document.createElement('h3'); heading.textContent = title; const frame = document.createElement('iframe'); frame.className = 'teaching-player'; frame.width = '640'; frame.height = '360'; frame.src = 'https://www.youtube.com/embed/' + id; frame.title = title; frame.loading = 'lazy'; frame.allowFullscreen = true; frame.allow = 'accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share'; frame.referrerPolicy = 'strict-origin-when-cross-origin'; const link = document.createElement('a'); link.href = 'https://www.youtube.com/watch?v=' + id; link.target = '_blank'; link.rel = 'noopener noreferrer'; link.textContent = 'Watch on YouTube ↗'; card.append(category, heading, frame, link); container.querySelector('.video-tapes').append(card); } function connect() { if (!window.YT || !window.YT.Player) return; document.querySelectorAll('iframe[data-youtube-playlist]').forEach(frame => { const playlist = frame.dataset.youtubePlaylist; const target = document.getElementById(frame.dataset.additions); const status = document.getElementById(frame.dataset.status); if (!target || !status) return; const src = new URL(frame.src); src.searchParams.set('enablejsapi','1'); src.searchParams.set('origin',location.origin); frame.src = src.href; frame.loading = 'eager'; let attempts = 0; let timer; function update(player) { let ids; try { ids = player.getPlaylist(); } catch (_) { return false; } if (!Array.isArray(ids) || !ids.length) return false; ids.filter(valid).forEach(id => addVideo(id, target, frame.dataset.label)); status.textContent = 'Connected to YouTube · ' + ids.filter(valid).length + ' playlist videos. Public additions appear here when this page is opened or refreshed.'; frame.dataset.playlistLoaded = String(ids.filter(valid).length); if (!frame.dataset.metadataChecked) { frame.dataset.metadataChecked = 'pending'; publicTitle(ids.find(valid)).then(() => { frame.dataset.metadataChecked = 'ready'; }).catch(() => { frame.dataset.metadataChecked = 'unavailable'; status.textContent = 'The YouTube playlist is available. This browser could not retrieve public titles for new individual cards; new videos remain available in the playlist player.'; }); } clearInterval(timer); return true; } new window.YT.Player(frame.id, {events: { onReady(event) { if (update(event.target)) return; event.target.cuePlaylist({listType:'playlist', list:playlist, index:0}); timer = setInterval(() => { attempts += 1; if (update(event.target) || attempts >= 10) clearInterval(timer); },1000); }, onStateChange(event) { update(event.target); }, onError() { status.textContent = 'YouTube could not load this playlist right now. Use the direct playlist link, or try again later.'; } }}); }); } if (window.YT && window.YT.Player) connect(); else { const previous = window.onYouTubeIframeAPIReady; window.onYouTubeIframeAPIReady = () => { if (typeof previous === 'function') previous(); connect(); }; const script = document.createElement('script'); script.src = 'https://www.youtube.com/iframe_api'; script.async = true; document.head.append(script); } })();