bep40 commited on
Commit
3792640
·
verified ·
1 Parent(s): 2c16feb

Add YouTube Live block pinned to top of home page

Browse files
Files changed (1) hide show
  1. static/yt_live.js +73 -0
static/yt_live.js ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // === VNEWS — YouTube Live pinned to TOP of home page ===
2
+ // Embeds a live YouTube stream as the very first block of #view-home.
3
+ // Loaded AFTER app_v2.js so it can wrap the global loadHome().
4
+ (function(){
5
+ const LIVE_ID = 'r9NDoNv2a-Y'; // youtube.com/live/r9NDoNv2a-Y
6
+ const LIVE_TITLE = 'Trực tiếp';
7
+
8
+ // Inject styles once.
9
+ if(!document.getElementById('yt-live-css')){
10
+ const s=document.createElement('style');s.id='yt-live-css';
11
+ s.textContent=`
12
+ .yt-live-wrap{margin:6px 4px;background:#111;border:1px solid #3a0d0d;border-radius:10px;overflow:hidden;box-shadow:0 0 0 1px rgba(231,76,60,.25)}
13
+ .yt-live-head{display:flex;align-items:center;gap:8px;padding:8px 10px;background:linear-gradient(90deg,#2a0808,#1a1a1a)}
14
+ .yt-live-dot{width:9px;height:9px;border-radius:50%;background:#e74c3c;box-shadow:0 0 6px #e74c3c;animation:yt-live-pulse 1.3s infinite}
15
+ @keyframes yt-live-pulse{0%,100%{opacity:1}50%{opacity:.3}}
16
+ .yt-live-badge{font-size:11px;font-weight:800;color:#e74c3c;letter-spacing:.5px}
17
+ .yt-live-title{font-size:12px;color:#eee;font-weight:700;flex:1;min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
18
+ .yt-live-frame{position:relative;width:100%;aspect-ratio:16/9;background:#000}
19
+ .yt-live-frame iframe{position:absolute;inset:0;width:100%;height:100%;border:0}
20
+ `;
21
+ document.head.appendChild(s);
22
+ }
23
+
24
+ function buildLive(){
25
+ const wrap=document.createElement('div');
26
+ wrap.className='yt-live-wrap';
27
+ wrap.id='yt-live-block';
28
+ const src='https://www.youtube.com/embed/'+LIVE_ID
29
+ +'?autoplay=1&mute=1&playsinline=1&rel=0&modestbranding=1';
30
+ wrap.innerHTML=
31
+ '<div class="yt-live-head">'
32
+ +'<span class="yt-live-dot"></span>'
33
+ +'<span class="yt-live-badge">● LIVE</span>'
34
+ +'<span class="yt-live-title">'+LIVE_TITLE+'</span>'
35
+ +'</div>'
36
+ +'<div class="yt-live-frame">'
37
+ +'<iframe src="'+src+'" allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture" allowfullscreen></iframe>'
38
+ +'</div>';
39
+ return wrap;
40
+ }
41
+
42
+ function pinLive(){
43
+ const home=document.getElementById('view-home');
44
+ if(!home)return;
45
+ // Already present? do nothing (avoid reloading the stream).
46
+ if(document.getElementById('yt-live-block'))return;
47
+ home.insertBefore(buildLive(), home.firstChild);
48
+ }
49
+
50
+ // Wrap the existing global loadHome so the live block is (re)pinned after each render.
51
+ function install(){
52
+ if(typeof window.loadHome!=='function'){return false;}
53
+ if(window.loadHome.__ytLiveWrapped)return true;
54
+ const orig=window.loadHome;
55
+ const wrapped=async function(){
56
+ const r=await orig.apply(this,arguments);
57
+ try{pinLive();}catch(e){}
58
+ return r;
59
+ };
60
+ wrapped.__ytLiveWrapped=true;
61
+ window.loadHome=wrapped;
62
+ return true;
63
+ }
64
+
65
+ // loadHome may already be defined (app_v2.js loaded before us). Install now; retry if not yet ready.
66
+ if(!install()){
67
+ let tries=0;
68
+ const t=setInterval(()=>{ if(install()||++tries>40)clearInterval(t); },100);
69
+ }
70
+ // In case the homepage was already rendered before this script ran, pin immediately too.
71
+ if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',()=>setTimeout(pinLive,300));
72
+ else setTimeout(pinLive,300);
73
+ })();