Spaces:
Running
Running
Fix VTV: prevent double-load + add EPG support
Browse files- static/yt_live.js +82 -17
static/yt_live.js
CHANGED
|
@@ -20,13 +20,13 @@
|
|
| 20 |
{id:'vtvprime', name:'VTVPrime', badge:'Prime'},
|
| 21 |
];
|
| 22 |
|
| 23 |
-
// fptplay URLs that need proxying (can't play directly due to hotlink protection)
|
| 24 |
const NEEDS_PROXY = /fptplay\.net/;
|
| 25 |
-
|
| 26 |
const STREAMS = {};
|
| 27 |
let _currentCh = null;
|
| 28 |
let _hls = null;
|
| 29 |
let _loading = false;
|
|
|
|
|
|
|
| 30 |
|
| 31 |
const s = document.createElement('style');
|
| 32 |
s.textContent = `
|
|
@@ -48,6 +48,18 @@
|
|
| 48 |
.vtv-load{display:flex;align-items:center;justify-content:center;height:180px;color:#00ccff;font-size:12px;flex-direction:column;gap:8px}
|
| 49 |
.vtv-spinner{width:24px;height:24px;border:2px solid #333;border-top-color:#00ccff;border-radius:50%;animation:vtvspin .8s linear infinite}
|
| 50 |
@keyframes vtvspin{to{transform:rotate(360deg)}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
`;
|
| 52 |
document.head.appendChild(s);
|
| 53 |
|
|
@@ -56,7 +68,6 @@
|
|
| 56 |
_loading = true;
|
| 57 |
const loadEl = document.getElementById('vtv-load');
|
| 58 |
if(loadEl) loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang tải danh sách kênh...';
|
| 59 |
-
|
| 60 |
try {
|
| 61 |
const r = await fetch('/api/vtv/streams', {signal: AbortSignal.timeout(10000)});
|
| 62 |
if(r.ok){
|
|
@@ -65,7 +76,6 @@
|
|
| 65 |
const info = data[ch.id];
|
| 66 |
if(info && info.stream_url){
|
| 67 |
let url = info.stream_url;
|
| 68 |
-
// If it's a fptplay URL, route through our proxy
|
| 69 |
if(NEEDS_PROXY.test(url)){
|
| 70 |
url = '/api/proxy/m3u8/vtv?url=' + encodeURIComponent(url);
|
| 71 |
}
|
|
@@ -78,8 +88,6 @@
|
|
| 78 |
} catch(e) {
|
| 79 |
console.warn('VTV API error:', e);
|
| 80 |
}
|
| 81 |
-
|
| 82 |
-
// Update UI tabs
|
| 83 |
CHANNELS.forEach(ch => {
|
| 84 |
const tab = document.getElementById('vtvt-'+ch.id);
|
| 85 |
if(tab){
|
|
@@ -93,6 +101,7 @@
|
|
| 93 |
}
|
| 94 |
});
|
| 95 |
_loading = false;
|
|
|
|
| 96 |
}
|
| 97 |
|
| 98 |
function buildBlock(){
|
|
@@ -110,22 +119,73 @@
|
|
| 110 |
'<div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div>' +
|
| 111 |
'<video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video>' +
|
| 112 |
'<div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="window._vtvRetry()">Thử lại</button></div>' +
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
'</div>';
|
| 114 |
return w;
|
| 115 |
}
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
function pinBlock(){
|
| 118 |
const h = document.getElementById('view-home');
|
| 119 |
-
if(!h
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
}
|
| 127 |
-
}
|
| 128 |
-
}
|
| 129 |
}
|
| 130 |
|
| 131 |
window._vtvRetry = function(){
|
|
@@ -160,6 +220,7 @@
|
|
| 160 |
return;
|
| 161 |
}
|
| 162 |
_tryPlay(video, urls, 0, ch.name, loadEl, errEl, errMsg);
|
|
|
|
| 163 |
};
|
| 164 |
|
| 165 |
function _tryPlay(video, urls, idx, name, loadEl, errEl, errMsg){
|
|
@@ -232,15 +293,19 @@
|
|
| 232 |
}
|
| 233 |
}
|
| 234 |
|
|
|
|
|
|
|
| 235 |
const orig = window.loadHome;
|
| 236 |
if(orig && !orig.__vtvWrapped){
|
| 237 |
window.loadHome = async function(){
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
const r = await orig.apply(this, arguments);
|
| 239 |
try{ pinBlock(); }catch(e){}
|
| 240 |
return r;
|
| 241 |
};
|
| 242 |
window.loadHome.__vtvWrapped = true;
|
| 243 |
}
|
| 244 |
-
if(document.readyState === 'loading') document.addEventListener('DOMContentLoaded', () => setTimeout(pinBlock, 500));
|
| 245 |
-
else setTimeout(pinBlock, 500);
|
| 246 |
})();
|
|
|
|
| 20 |
{id:'vtvprime', name:'VTVPrime', badge:'Prime'},
|
| 21 |
];
|
| 22 |
|
|
|
|
| 23 |
const NEEDS_PROXY = /fptplay\.net/;
|
|
|
|
| 24 |
const STREAMS = {};
|
| 25 |
let _currentCh = null;
|
| 26 |
let _hls = null;
|
| 27 |
let _loading = false;
|
| 28 |
+
let _blockInserted = false;
|
| 29 |
+
let _streamsLoaded = false;
|
| 30 |
|
| 31 |
const s = document.createElement('style');
|
| 32 |
s.textContent = `
|
|
|
|
| 48 |
.vtv-load{display:flex;align-items:center;justify-content:center;height:180px;color:#00ccff;font-size:12px;flex-direction:column;gap:8px}
|
| 49 |
.vtv-spinner{width:24px;height:24px;border:2px solid #333;border-top-color:#00ccff;border-radius:50%;animation:vtvspin .8s linear infinite}
|
| 50 |
@keyframes vtvspin{to{transform:rotate(360deg)}}
|
| 51 |
+
.vtv-epg{margin:0;padding:6px 10px;background:#0a1628;border-top:1px solid #1a2a3a}
|
| 52 |
+
.vtv-epg-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:4px}
|
| 53 |
+
.vtv-epg-title{font-size:10px;font-weight:700;color:#00ccff}
|
| 54 |
+
.vtv-epg-toggle{background:none;border:1px solid #2a3a4a;color:#8ab4d8;font-size:9px;padding:2px 8px;border-radius:6px;cursor:pointer}
|
| 55 |
+
.vtv-epg-list{display:flex;gap:4px;overflow-x:auto;scrollbar-width:none;padding-bottom:4px}
|
| 56 |
+
.vtv-epg-list::-webkit-scrollbar{display:none}
|
| 57 |
+
.vtv-epg-item{flex:0 0 auto;padding:3px 6px;background:#1a2a3a;border-radius:4px;font-size:8px;color:#8ab4d8;white-space:nowrap}
|
| 58 |
+
.vtv-epg-item.now{background:#0066cc;color:#fff;font-weight:700}
|
| 59 |
+
.vtv-epg-item .epg-t{font-size:7px;color:#6a8aaa}
|
| 60 |
+
.vtv-epg-item.now .epg-t{color:#aaccee}
|
| 61 |
+
.vtv-epg-item .epg-n{color:#ccc;font-size:8px}
|
| 62 |
+
.vtv-epg-item.now .epg-n{color:#fff}
|
| 63 |
`;
|
| 64 |
document.head.appendChild(s);
|
| 65 |
|
|
|
|
| 68 |
_loading = true;
|
| 69 |
const loadEl = document.getElementById('vtv-load');
|
| 70 |
if(loadEl) loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang tải danh sách kênh...';
|
|
|
|
| 71 |
try {
|
| 72 |
const r = await fetch('/api/vtv/streams', {signal: AbortSignal.timeout(10000)});
|
| 73 |
if(r.ok){
|
|
|
|
| 76 |
const info = data[ch.id];
|
| 77 |
if(info && info.stream_url){
|
| 78 |
let url = info.stream_url;
|
|
|
|
| 79 |
if(NEEDS_PROXY.test(url)){
|
| 80 |
url = '/api/proxy/m3u8/vtv?url=' + encodeURIComponent(url);
|
| 81 |
}
|
|
|
|
| 88 |
} catch(e) {
|
| 89 |
console.warn('VTV API error:', e);
|
| 90 |
}
|
|
|
|
|
|
|
| 91 |
CHANNELS.forEach(ch => {
|
| 92 |
const tab = document.getElementById('vtvt-'+ch.id);
|
| 93 |
if(tab){
|
|
|
|
| 101 |
}
|
| 102 |
});
|
| 103 |
_loading = false;
|
| 104 |
+
_streamsLoaded = true;
|
| 105 |
}
|
| 106 |
|
| 107 |
function buildBlock(){
|
|
|
|
| 119 |
'<div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div>' +
|
| 120 |
'<video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video>' +
|
| 121 |
'<div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="window._vtvRetry()">Thử lại</button></div>' +
|
| 122 |
+
'</div>' +
|
| 123 |
+
'<div class="vtv-epg" id="vtv-epg">' +
|
| 124 |
+
'<div class="vtv-epg-header"><span class="vtv-epg-title">📋 Lịch phát sóng</span><button class="vtv-epg-toggle" id="vtv-epg-toggle" onclick="window._vtvToggleEpg()">Ẩn</button></div>' +
|
| 125 |
+
'<div class="vtv-epg-list" id="vtv-epg-list"><span style="color:#666;font-size:9px;padding:4px">Đang tải EPG...</span></div>' +
|
| 126 |
'</div>';
|
| 127 |
return w;
|
| 128 |
}
|
| 129 |
|
| 130 |
+
async function loadEpg(chId){
|
| 131 |
+
const epgList = document.getElementById('vtv-epg-list');
|
| 132 |
+
if(!epgList) return;
|
| 133 |
+
try {
|
| 134 |
+
const r = await fetch('/api/vtv/epg/' + chId, {signal: AbortSignal.timeout(8000)});
|
| 135 |
+
if(!r.ok) throw new Error('EPG fetch failed');
|
| 136 |
+
const data = await r.json();
|
| 137 |
+
const programs = data.programs || [];
|
| 138 |
+
if(programs.length === 0){
|
| 139 |
+
epgList.innerHTML = '<span style="color:#666;font-size:9px;padding:4px">Chưa có lịch phát sóng</span>';
|
| 140 |
+
return;
|
| 141 |
+
}
|
| 142 |
+
let h = '';
|
| 143 |
+
programs.forEach(p => {
|
| 144 |
+
const isNow = p.now ? ' now' : '';
|
| 145 |
+
h += '<div class="vtv-epg-item' + isNow + '" title="' + (p.title || '') + '">';
|
| 146 |
+
h += '<span class="epg-t">' + (p.time || '') + '</span> ';
|
| 147 |
+
h += '<span class="epg-n">' + (p.title || '') + '</span>';
|
| 148 |
+
h += '</div>';
|
| 149 |
+
});
|
| 150 |
+
epgList.innerHTML = h;
|
| 151 |
+
} catch(e) {
|
| 152 |
+
if(epgList) epgList.innerHTML = '<span style="color:#666;font-size:9px;padding:4px">Không tải được EPG</span>';
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
window._vtvToggleEpg = function(){
|
| 157 |
+
const list = document.getElementById('vtv-epg-list');
|
| 158 |
+
const btn = document.getElementById('vtv-epg-toggle');
|
| 159 |
+
if(!list || !btn) return;
|
| 160 |
+
if(list.style.display === 'none'){
|
| 161 |
+
list.style.display = 'flex';
|
| 162 |
+
btn.textContent = 'Ẩn';
|
| 163 |
+
} else {
|
| 164 |
+
list.style.display = 'none';
|
| 165 |
+
btn.textContent = 'Hiện';
|
| 166 |
+
}
|
| 167 |
+
};
|
| 168 |
+
|
| 169 |
function pinBlock(){
|
| 170 |
const h = document.getElementById('view-home');
|
| 171 |
+
if(!h) return;
|
| 172 |
+
if(document.getElementById('vtv-block')){
|
| 173 |
+
_blockInserted = true;
|
| 174 |
+
return;
|
| 175 |
+
}
|
| 176 |
+
const block = buildBlock();
|
| 177 |
+
h.insertBefore(block, h.firstChild);
|
| 178 |
+
_blockInserted = true;
|
| 179 |
+
if(!_streamsLoaded){
|
| 180 |
+
loadAllStreams().then(() => {
|
| 181 |
+
for(const ch of CHANNELS){
|
| 182 |
+
if(STREAMS[ch.id] && STREAMS[ch.id].length > 0){
|
| 183 |
+
setTimeout(() => window._vtvPlay(ch.id), 300);
|
| 184 |
+
break;
|
| 185 |
+
}
|
| 186 |
}
|
| 187 |
+
});
|
| 188 |
+
}
|
| 189 |
}
|
| 190 |
|
| 191 |
window._vtvRetry = function(){
|
|
|
|
| 220 |
return;
|
| 221 |
}
|
| 222 |
_tryPlay(video, urls, 0, ch.name, loadEl, errEl, errMsg);
|
| 223 |
+
loadEpg(chId);
|
| 224 |
};
|
| 225 |
|
| 226 |
function _tryPlay(video, urls, idx, name, loadEl, errEl, errMsg){
|
|
|
|
| 293 |
}
|
| 294 |
}
|
| 295 |
|
| 296 |
+
// FIX: Only wrap loadHome, no DOMContentLoaded path.
|
| 297 |
+
// This ensures pinBlock runs exactly once after loadHome completes.
|
| 298 |
const orig = window.loadHome;
|
| 299 |
if(orig && !orig.__vtvWrapped){
|
| 300 |
window.loadHome = async function(){
|
| 301 |
+
// Remove old VTV block before loadHome replaces innerHTML
|
| 302 |
+
const oldBlock = document.getElementById('vtv-block');
|
| 303 |
+
if(oldBlock) oldBlock.remove();
|
| 304 |
+
_blockInserted = false;
|
| 305 |
const r = await orig.apply(this, arguments);
|
| 306 |
try{ pinBlock(); }catch(e){}
|
| 307 |
return r;
|
| 308 |
};
|
| 309 |
window.loadHome.__vtvWrapped = true;
|
| 310 |
}
|
|
|
|
|
|
|
| 311 |
})();
|