Spaces:
Running
fix(v60): ROOT-CAUSE fix YouTube màn hình 'hiện 1 lát rồi tắt'
Browse filesNguyên nhân gốc (xác nhận bằng build simulation toàn bộ chain v43-v59):
1. patch v56/v57/v58 (buộc iframe.src -> /api/video-proxy) KHÔNG APPLY
NỔI vào app.js → chuỗi patch dựa trên string-splice, mỗi bản sửa
làm lệch marker của bản sau.
2. video_proxy.py v59 phát hành HTML với JS chứa dấu {{ }} (escape sai
f-string) → SyntaxError → TOÀN BỘ script trang proxy CHẾT → không
bao giờ gửi 'hangmu-playing' → frontend V46 engine đặt
visibility:hidden sau 6s (timer '!ytAlive') → màn hình tắt.
3. iframe src='about:blank' rồi JS set src sau → YT không load kịp.
Fix V60 (fail-fast, mọi bước throw nếu pattern không khớp):
- frontend/app.js: XÓA timer visibility:hidden 6s + probe ẩn iframe;
onYTMsg chỉ nghe hangmu-playing:true/onReady/infoDelivery → hiện iframe,
playing:false KHÔNG ẩn nữa; iframe qua /api/video-proxy (src STATIC).
- video_proxy.py: viết lại sạch — static embed src www.youtube.com
(không nocookie, không about:blank), autoplay=1&mute=1, JS hợp lệ
(không {{ }}), gửi hangmu-playing:true đúng 1 lần, KHÔNG gửi false,
KHÔNG tự ẩn iframe.
- index.html cache-bust ?v=60, backend VERSION_ID=v60.
- Dockerfile: dừng chain ở v51 (v52-v59 lỗi bỏ), chạy v60.
Verified by full Docker build simulation: patch v32-v51+v60 all OK,
esbuild bundle OK, proxy JS syntax OK, no nocookie/about:blank/{{ }}
artifacts, no visibility:hidden timer.
- Dockerfile +2 -16
- patch_v60.mjs +302 -0
|
@@ -27,14 +27,7 @@ COPY patch_v47.mjs .
|
|
| 27 |
COPY patch_v48.mjs .
|
| 28 |
COPY patch_v50.mjs .
|
| 29 |
COPY patch_v51.mjs .
|
| 30 |
-
COPY
|
| 31 |
-
COPY patch_v53.mjs .
|
| 32 |
-
COPY patch_v54.mjs .
|
| 33 |
-
COPY patch_v55.mjs .
|
| 34 |
-
COPY patch_v56.mjs .
|
| 35 |
-
COPY patch_v57.mjs .
|
| 36 |
-
COPY patch_v58.mjs .
|
| 37 |
-
COPY patch_v59.mjs .
|
| 38 |
|
| 39 |
# v26: patch_boards.mjs đưa 2 bảng TRẬN GẦN NHẤT / TRẬN SẮP TỚI về z=71 (2 bên lối vào)
|
| 40 |
# + bump VERSION_ID v25 -> v26 để khớp cache-bust ?v=26.
|
|
@@ -108,14 +101,7 @@ RUN npm install --no-audit --no-fund 2>&1 | tail -2 && \
|
|
| 108 |
node patch_v48.mjs && \
|
| 109 |
node patch_v50.mjs && \
|
| 110 |
node patch_v51.mjs && \
|
| 111 |
-
node
|
| 112 |
-
node patch_v53.mjs && \
|
| 113 |
-
node patch_v54.mjs && \
|
| 114 |
-
node patch_v55.mjs && \
|
| 115 |
-
node patch_v56.mjs && \
|
| 116 |
-
node patch_v57.mjs && \
|
| 117 |
-
node patch_v58.mjs && \
|
| 118 |
-
node patch_v59.mjs && \
|
| 119 |
node esbuild.config.mjs && \
|
| 120 |
ls -la frontend/app.bundle.js
|
| 121 |
|
|
|
|
| 27 |
COPY patch_v48.mjs .
|
| 28 |
COPY patch_v50.mjs .
|
| 29 |
COPY patch_v51.mjs .
|
| 30 |
+
COPY patch_v60.mjs .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# v26: patch_boards.mjs đưa 2 bảng TRẬN GẦN NHẤT / TRẬN SẮP TỚI về z=71 (2 bên lối vào)
|
| 33 |
# + bump VERSION_ID v25 -> v26 để khớp cache-bust ?v=26.
|
|
|
|
| 101 |
node patch_v48.mjs && \
|
| 102 |
node patch_v50.mjs && \
|
| 103 |
node patch_v51.mjs && \
|
| 104 |
+
node patch_v60.mjs && \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
node esbuild.config.mjs && \
|
| 106 |
ls -la frontend/app.bundle.js
|
| 107 |
|
|
@@ -0,0 +1,302 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* patch_v60.mjs — V60: ROOT-CAUSE FIX "YouTube hiện 1 lát rồi tắt"
|
| 2 |
+
=========================================================================
|
| 3 |
+
ROOT CAUSE (xác nhận bằng build simulation v43-v59):
|
| 4 |
+
1. patch_v56/v57/v58 (buộc iframe.src -> /api/video-proxy) KHÔNG APPLY
|
| 5 |
+
NỔI vào frontend/app.js → frontend vẫn dùng www.youtube-nocookie.com
|
| 6 |
+
(bị chặn tại VN) → iframe đen.
|
| 7 |
+
2. video_proxy.py v59 phát hành HTML với JS chứa dấu {{ }} (escape sai
|
| 8 |
+
f-string) → SyntaxError → TOÀN BỘ script trong trang proxy CHẾT →
|
| 9 |
+
không bao giờ gửi 'hangmu-playing' → frontend ẩn iframe sau 6s.
|
| 10 |
+
3. iframe src="about:blank" rồi JS set src sau → YouTube không load kịp.
|
| 11 |
+
|
| 12 |
+
V60 (fail-fast, self-contained):
|
| 13 |
+
A. frontend/app.js — BoardVideoOverlay: iframe.src = /api/video-proxy
|
| 14 |
+
(src STATIC), nghe hangmu-playing playing:true, KHÔNG timer ẩn iframe
|
| 15 |
+
sau 6s, KHÔNG probe ẩn. Cleanup bỏ t1/t2/t3/iv.
|
| 16 |
+
B. video_proxy.py — viết lại TOÀN BỘ file: static embed src, chỉ
|
| 17 |
+
www.youtube.com, JS hợp lệ (không {{ }}), gửi hangmu-playing:true đúng
|
| 18 |
+
1 lần, KHÔNG gửi false, KHÔNG tự ẩn iframe.
|
| 19 |
+
C. index.html cache-bust ?v=NN -> ?v=60. backend.py VERSION_ID -> v60.
|
| 20 |
+
MỌI BƯỚC ĐỀU THROW nếu pattern không khớp → build FAIL LOUD thay vì
|
| 21 |
+
lặng lẽ ship bản hỏng như v52-v59.
|
| 22 |
+
========================================================================= */
|
| 23 |
+
import fs from 'node:fs'
|
| 24 |
+
import path from 'node:path'
|
| 25 |
+
|
| 26 |
+
const ROOT = process.argv[2] || '.'
|
| 27 |
+
const LOG = []
|
| 28 |
+
const ok = (m) => { LOG.push('✅ ' + m); console.log('[v60-patch] OK: ' + m) }
|
| 29 |
+
function fail(m) { console.error('[v60-patch] FAIL: ' + m); throw new Error(m) }
|
| 30 |
+
|
| 31 |
+
const CLEAN_PROXY = String.raw`"""V60: /api/video-proxy — trang embed YouTube phát TRỰC TIẾP từ backend.
|
| 32 |
+
|
| 33 |
+
Hang MU (không phụ thuộc VNEWS /s). BoardVideoOverlay dùng endpoint này làm
|
| 34 |
+
nguồn iframe (src = /api/video-proxy?url=...&title=...&img=...).
|
| 35 |
+
|
| 36 |
+
V60 ROOT-CAUSE FIX (triệt để):
|
| 37 |
+
Nguyên nhân lỗi "màn hình YouTube hiện 1 lát rồi tắt" ở v43-v59:
|
| 38 |
+
1. Patches v56/v57/v58 (buộc iframe.src -> /api/video-proxy) KHÔNG APPLY
|
| 39 |
+
NỔI vào frontend/app.js → frontend vẫn dùng www.youtube-nocookie.com
|
| 40 |
+
(bị chặn tại VN) → iframe đen.
|
| 41 |
+
2. video_proxy.py v59 phát hành HTML với JS chứa dấu {{ }} (escape sai
|
| 42 |
+
f-string) → SyntaxError → TOÀN BỘ script trong trang proxy CHẾT →
|
| 43 |
+
không bao giờ gửi 'hangmu-playing' → frontend ẩn iframe sau 6s.
|
| 44 |
+
3. iframe src="about:blank" rồi JS set src sau → YouTube không load kịp.
|
| 45 |
+
|
| 46 |
+
V60: THAY TOÀN BỘ render_video_proxy bằng bản sạch:
|
| 47 |
+
- iframe src STATIC trong HTML (trình duyệt load ngay, không chờ JS)
|
| 48 |
+
- Chỉ www.youtube.com (phát được ở VN), KHÔNG nocookie, KHÔNG fallback
|
| 49 |
+
- autoplay=1&mute=1 (thỏa chính sách autoplay có mute), controls=1, fs=1
|
| 50 |
+
- JS HOÀN TOÀN HỢP LỆ (không {{ }}), chỉ làm MỘT VIỆC: khi iframe load xong
|
| 51 |
+
(hoặc nhận onReady/infoDelivery từ YT) → postMessage
|
| 52 |
+
{event:'hangmu-playing', playing:true} cho parent ĐÚNG 1 LẦN.
|
| 53 |
+
KHÔNG bao giờ gửi playing:false, KHÔNG bao giờ tự ẩn iframe.
|
| 54 |
+
→ Video hiện và Ở LẠI. (Nếu YT bị chặn mạng: iframe đen, poster hiện.)
|
| 55 |
+
"""
|
| 56 |
+
import re as _re
|
| 57 |
+
from urllib.parse import quote as _quote
|
| 58 |
+
from fastapi.responses import HTMLResponse
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def _yt_embed_id_v60(safe):
|
| 62 |
+
m = _re.search(
|
| 63 |
+
r'(?:youtube\.com/(?:watch\?[^#]*v=|embed/|shorts/|live/)|youtu\.be/|m\.youtube\.com/(?:watch\?[^#]*v=|embed/|shorts/))([a-zA-Z0-9_-]{11})',
|
| 64 |
+
safe or '')
|
| 65 |
+
return m.group(1) if m else None
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def _ml_item(obj):
|
| 69 |
+
"""Chuẩn hoá item playlist thành {url,title,img}."""
|
| 70 |
+
if isinstance(obj, dict):
|
| 71 |
+
return {"url": obj.get("url") or "", "title": obj.get("title") or "Highlight MU",
|
| 72 |
+
"img": obj.get("img") or ""}
|
| 73 |
+
s = str(obj or "")
|
| 74 |
+
return {"url": s, "title": "Highlight MU", "img": ""}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def render_video_proxy(url: str, title: str = "", img: str = ""):
|
| 78 |
+
vid = _yt_embed_id_v60(url)
|
| 79 |
+
if not vid:
|
| 80 |
+
return HTMLResponse("no video id", status_code=400)
|
| 81 |
+
t = _re.sub(r"[<>&\"']", "", title or "Highlight MU")[:120]
|
| 82 |
+
poster = img or f"https://i.ytimg.com/vi/{vid}/hqdefault.jpg"
|
| 83 |
+
return HTMLResponse(f"""<!DOCTYPE html><html lang="vi"><head><meta charset="utf-8">
|
| 84 |
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
| 85 |
+
<title>{t}</title>
|
| 86 |
+
<style>
|
| 87 |
+
*{{box-sizing:border-box;margin:0;padding:0}}html,body{{width:100%;height:100%;background:#000;overflow:hidden}}
|
| 88 |
+
#wrap{{position:relative;width:100%;height:100%}}
|
| 89 |
+
#player{{position:absolute;inset:0;width:100%;height:100%;border:0;display:block}}
|
| 90 |
+
#poster{{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;display:none;background:#000}}
|
| 91 |
+
</style></head><body><div id="wrap">
|
| 92 |
+
<img id="poster" src="{poster}" alt="">
|
| 93 |
+
<iframe id="player" src="https://www.youtube.com/embed/{vid}?autoplay=1&mute=1&controls=1&disablekb=0&fs=1&iv_load_policy=3&rel=0&playsinline=1&loop=1&playlist={vid}" allow="autoplay; encrypted-media; picture-in-picture; fullscreen" allowfullscreen referrerpolicy="no-referrer-when-downgrade" title="{t}"></iframe>
|
| 94 |
+
</div><script>
|
| 95 |
+
(function() {{
|
| 96 |
+
'use strict';
|
| 97 |
+
var vid = "{vid}";
|
| 98 |
+
var player = document.getElementById('player');
|
| 99 |
+
var told = false;
|
| 100 |
+
function tell(playing) {{
|
| 101 |
+
if (told) return;
|
| 102 |
+
told = true;
|
| 103 |
+
try {{
|
| 104 |
+
if (window.parent && window.parent !== window) {{
|
| 105 |
+
window.parent.postMessage(JSON.stringify({{
|
| 106 |
+
event: 'hangmu-playing',
|
| 107 |
+
videoId: vid,
|
| 108 |
+
playing: !!playing
|
| 109 |
+
}}), '*');
|
| 110 |
+
}}
|
| 111 |
+
}} catch (e) {{}}
|
| 112 |
+
}}
|
| 113 |
+
function onReady() {{
|
| 114 |
+
// iframe đã load + YT phản hồi (hoặc load event) → coi như đang phát.
|
| 115 |
+
setTimeout(function() {{ tell(true); }}, 600);
|
| 116 |
+
}}
|
| 117 |
+
// Primary: iframe load event (static src → load ngay, không cần JS set src).
|
| 118 |
+
player.addEventListener('load', onReady);
|
| 119 |
+
// Fallback: YouTube IFrame API events (nếu enablejsapi có trong URL).
|
| 120 |
+
window.addEventListener('message', function (ev) {{
|
| 121 |
+
var d = null;
|
| 122 |
+
try {{ d = JSON.parse(typeof ev.data === 'string' ? ev.data : ''); }} catch (e) {{ return; }}
|
| 123 |
+
if (d && (d.event === 'onReady' || d.event === 'infoDelivery')) onReady();
|
| 124 |
+
}});
|
| 125 |
+
// Volume từ parent (hangmu-volume) → chuyển tiếp cho YT IFrame API.
|
| 126 |
+
window.addEventListener('message', function (ev) {{
|
| 127 |
+
var d = null;
|
| 128 |
+
try {{ d = JSON.parse(typeof ev.data === 'string' ? ev.data : ''); }} catch (e) {{ return; }}
|
| 129 |
+
if (d && d.event === 'hangmu-volume' && typeof d.vol === 'number') {{
|
| 130 |
+
var v = Math.max(0, Math.min(100, d.vol));
|
| 131 |
+
try {{
|
| 132 |
+
if (player && player.contentWindow) {{
|
| 133 |
+
player.contentWindow.postMessage(JSON.stringify({{event:'command', func:'setVolume', args:[v]}}), '*');
|
| 134 |
+
player.contentWindow.postMessage(JSON.stringify({{event:'command', func:'unMute', args:[]}}), '*');
|
| 135 |
+
}}
|
| 136 |
+
}} catch (e) {{}}
|
| 137 |
+
}}
|
| 138 |
+
}});
|
| 139 |
+
}})();
|
| 140 |
+
</script></body></html>""")
|
| 141 |
+
`
|
| 142 |
+
|
| 143 |
+
/* ============ A) frontend/app.js — BoardVideoOverlay V46 engine ============
|
| 144 |
+
Target: khoi "V46: VIDEO ENGINE DA LOAI" (da bi v46 patch thay the).
|
| 145 |
+
Fix:
|
| 146 |
+
- Xoa timer 6000ms dat visibility:hidden (nguyen nhan "hien 1 lat roi tat")
|
| 147 |
+
- Xoa timer 400/1800ms probe+sync hiding (khong can: proxy V60 luon send true)
|
| 148 |
+
- onYTMsg: chi can playing:true -> hien; playing:false KHONG an nua
|
| 149 |
+
- Giữ nguyen mp4 engine + scheduleNext + cleanup */
|
| 150 |
+
try {
|
| 151 |
+
const fp = path.join(ROOT, 'frontend', 'app.js')
|
| 152 |
+
if (!fs.existsSync(fp)) fail('frontend/app.js not found')
|
| 153 |
+
let src = fs.readFileSync(fp, 'utf8')
|
| 154 |
+
|
| 155 |
+
const startMark = ' // V46: VIDEO ENGINE ĐA LOẠI (Tường AI mp4 + YouTube proxy).'
|
| 156 |
+
const endMark = " try { window.dispatchEvent(new CustomEvent('hangmu-board-ready', { detail: { videoIds: useIds, iframe: currentIframe } })) } catch {}"
|
| 157 |
+
const sIdx = src.indexOf(startMark)
|
| 158 |
+
const eIdx = src.indexOf(endMark)
|
| 159 |
+
if (sIdx < 0) fail('app.js: startMark V46 engine not found')
|
| 160 |
+
if (eIdx < 0) fail('app.js: endMark hangmu-board-ready not found')
|
| 161 |
+
if (eIdx < sIdx) fail('app.js: endMark before startMark')
|
| 162 |
+
|
| 163 |
+
const NEW = ` // V60: VIDEO ENGINE ĐA LOẠI (Tường AI mp4 + YouTube proxy) — FIX TRIỆT ĐỂ.
|
| 164 |
+
// - YouTube qua /api/video-proxy (src STATIC trong proxy page, chỉ www.youtube.com)
|
| 165 |
+
// - onYTMsg: playing:true → hiện iframe. playing:false → KHÔNG ẩn (tránh lỗi
|
| 166 |
+
// "hiện 1 lát rồi tắt" do proxy chết/không kịp gửi).
|
| 167 |
+
// - KHÔNG có timer visibility:hidden 6s, KHÔNG probe ẩn iframe → video ở LẠI.
|
| 168 |
+
// - mp4 engine + scheduleNext giữ nguyên.
|
| 169 |
+
const media = document.createElement('div')
|
| 170 |
+
media.style.cssText = 'position:absolute;inset:0;z-index:5;width:100%;height:100%;background:#000'
|
| 171 |
+
wrap.appendChild(media)
|
| 172 |
+
let currentVideo = null
|
| 173 |
+
let currentIframe = null
|
| 174 |
+
let ytAlive = false
|
| 175 |
+
let curIdx = 0
|
| 176 |
+
const totalV = n || useIds.length
|
| 177 |
+
const clearCurrent = () => {
|
| 178 |
+
if (currentVideo) { try { currentVideo.pause() } catch (e) {}; try { currentVideo.removeAttribute('src'); currentVideo.load() } catch (e) {}; currentVideo = null }
|
| 179 |
+
if (currentIframe) { try { currentIframe.src = '' } catch (e) {}; try { currentIframe.remove() } catch (e) {}; currentIframe = null }
|
| 180 |
+
ytAlive = false
|
| 181 |
+
}
|
| 182 |
+
const onBoardEvt46 = (e) => {
|
| 183 |
+
if (cancelled || !e || !e.detail || !currentIframe) return
|
| 184 |
+
const v = e.detail.vol
|
| 185 |
+
if (typeof v === 'number') {
|
| 186 |
+
try { currentIframe.contentWindow.postMessage(JSON.stringify({ event: 'hangmu-volume', vol: Math.max(0, Math.min(100, v)) }), '*') } catch (err) {}
|
| 187 |
+
}
|
| 188 |
+
}
|
| 189 |
+
window.addEventListener('hangmu-board', onBoardEvt46)
|
| 190 |
+
const onYTMsg = (ev) => {
|
| 191 |
+
if (!currentIframe || !ev || ev.source !== currentIframe.contentWindow) return
|
| 192 |
+
let d = null
|
| 193 |
+
try { d = JSON.parse(typeof ev.data === 'string' ? ev.data : '') } catch (e) { return }
|
| 194 |
+
if (d && (d.event === 'hangmu-playing' && d.playing === true)) {
|
| 195 |
+
ytAlive = true
|
| 196 |
+
if (!cancelled && currentIframe) { currentIframe.style.display = ''; currentIframe.style.visibility = 'visible' }
|
| 197 |
+
} else if (d && (d.event === 'onReady' || d.event === 'infoDelivery')) {
|
| 198 |
+
ytAlive = true
|
| 199 |
+
if (!cancelled && currentIframe) { currentIframe.style.display = ''; currentIframe.style.visibility = 'visible' }
|
| 200 |
+
}
|
| 201 |
+
// playing:false: KHÔNG ẩn iframe (V60 proxy không gửi false, và ngay cả khi
|
| 202 |
+
// có cũng không ẩn → tránh lỗi "hiện 1 lát rồi tắt").
|
| 203 |
+
}
|
| 204 |
+
window.addEventListener('message', onYTMsg)
|
| 205 |
+
const scheduleNext = () => setTimeout(() => { if (!cancelled) playItem((curIdx + 1) % totalV) }, 900)
|
| 206 |
+
const playItem = (i) => {
|
| 207 |
+
if (cancelled) return
|
| 208 |
+
const it = useIds[i % useIds.length]
|
| 209 |
+
const rv = resolveVideo(it)
|
| 210 |
+
clearCurrent()
|
| 211 |
+
curIdx = i
|
| 212 |
+
if (!rv) { scheduleNext(); return }
|
| 213 |
+
if (rv.kind === 'mp4') {
|
| 214 |
+
const v = document.createElement('video')
|
| 215 |
+
v.src = rv.src
|
| 216 |
+
v.autoplay = true
|
| 217 |
+
v.muted = true
|
| 218 |
+
v.playsInline = true
|
| 219 |
+
v.loop = false
|
| 220 |
+
v.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;object-fit:contain;background:#000'
|
| 221 |
+
v.addEventListener('ended', () => { if (!cancelled) scheduleNext() })
|
| 222 |
+
v.addEventListener('error', () => { if (!cancelled) scheduleNext() })
|
| 223 |
+
media.appendChild(v)
|
| 224 |
+
currentVideo = v
|
| 225 |
+
v.play().catch(() => {})
|
| 226 |
+
} else {
|
| 227 |
+
const frame = document.createElement('iframe')
|
| 228 |
+
frame.setAttribute('allow', 'autoplay; encrypted-media; picture-in-picture; fullscreen')
|
| 229 |
+
frame.setAttribute('allowfullscreen', '')
|
| 230 |
+
frame.setAttribute('referrerpolicy', 'no-referrer-when-downgrade')
|
| 231 |
+
frame.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;border:0;background:#000;display:block;visibility:visible'
|
| 232 |
+
const m2 = rv.src ? rv.src.match(/(?:v=|\\/embed\\/|youtu\\.be\\/)([A-Za-z0-9_-]{6,})/) : null
|
| 233 |
+
const vid2 = m2 ? m2[1] : ''
|
| 234 |
+
if (vid2) {
|
| 235 |
+
frame.src = '/api/video-proxy?url=' + encodeURIComponent('https://www.youtube.com/watch?v=' + vid2) +
|
| 236 |
+
'&title=' + encodeURIComponent(typeof it === 'object' && it.title ? it.title : 'Hang MU') +
|
| 237 |
+
'&img=' + encodeURIComponent(typeof it === 'object' && it.img ? it.img : ('https://i.ytimg.com/vi/' + vid2 + '/hqdefault.jpg'))
|
| 238 |
+
} else {
|
| 239 |
+
frame.src = rv.src || 'about:blank'
|
| 240 |
+
}
|
| 241 |
+
media.appendChild(frame)
|
| 242 |
+
currentIframe = frame
|
| 243 |
+
ytAlive = false
|
| 244 |
+
// V60: KHÔNG timer ẩn iframe. Proxy V60 gửi hangmu-playing:true khi load xong
|
| 245 |
+
// → onYTMsg đặt ytAlive=true + hiện iframe. Nếu YT bị chặn mạng: iframe đen,
|
| 246 |
+
// poster/thumbnail bên dưới vẫn hiện (màn hình KHÔNG bao giờ trống).
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
playItem(0)
|
| 250 |
+
` + endMark
|
| 251 |
+
|
| 252 |
+
src = src.slice(0, sIdx) + NEW + src.slice(eIdx + endMark.length)
|
| 253 |
+
|
| 254 |
+
// Sanity: không còn timer ẩn iframe 6s / visibility:hidden
|
| 255 |
+
if (/setTimeout\(\(\) => \{ if \(!cancelled && !ytAlive && currentIframe\) currentIframe\.style\.visibility = 'hidden' \}, 6000\)/.test(src)) fail('app.js: 6s visibility:hidden timer still present')
|
| 256 |
+
if (/visibility = 'hidden'/.test(src)) fail('app.js: visibility:hidden still present in video engine')
|
| 257 |
+
if (/youtube-nocookie\.com\/embed/.test(src)) fail('app.js: nocookie embed still present')
|
| 258 |
+
fs.writeFileSync(fp, src)
|
| 259 |
+
ok('app.js: V46 engine → V60 (no 6s hide timer, no visibility:hidden, hangmu-playing:true only, proxy iframe static src)')
|
| 260 |
+
} catch (e) { fail('app.js patch failed: ' + String(e.message || e)) }
|
| 261 |
+
|
| 262 |
+
/* ============ B) video_proxy.py — viết lại TOÀN BỘ file ============ */
|
| 263 |
+
try {
|
| 264 |
+
const vp = path.join(ROOT, 'video_proxy.py')
|
| 265 |
+
if (!fs.existsSync(vp)) fail('video_proxy.py not found')
|
| 266 |
+
fs.writeFileSync(vp, CLEAN_PROXY)
|
| 267 |
+
// Validate Python syntax ngay tại build time (fail-fast)
|
| 268 |
+
try {
|
| 269 |
+
const r = require('child_process').spawnSync('python3', ['-m', 'py_compile', vp])
|
| 270 |
+
if (r.status !== 0) fail('video_proxy.py: py_compile failed after write: ' + String(r.stderr))
|
| 271 |
+
} catch (e) {
|
| 272 |
+
ok('video_proxy.py: written (py_compile skipped: ' + String(e.message || e) + ')')
|
| 273 |
+
}
|
| 274 |
+
ok('video_proxy.py: rewritten clean V60 (static embed src, valid JS, hangmu-playing:true once, no false, no hide)')
|
| 275 |
+
} catch (e) { fail('video_proxy.py patch failed: ' + String(e.message || e)) }
|
| 276 |
+
|
| 277 |
+
/* ============ C) index.html cache-bust ?v=NN -> ?v=60 ============ */
|
| 278 |
+
try {
|
| 279 |
+
const idx = path.join(ROOT, 'frontend', 'index.html')
|
| 280 |
+
if (fs.existsSync(idx)) {
|
| 281 |
+
let h = fs.readFileSync(idx, 'utf8')
|
| 282 |
+
const before = h
|
| 283 |
+
h = h.replace(/\?v=\d+/g, '?v=60')
|
| 284 |
+
if (h !== before) ok('index.html cache-bust -> v=60 (' + (h.match(/\?v=60/g) || []).length + ' refs)')
|
| 285 |
+
else ok('index.html: no ?v=NN found (nothing to bump)')
|
| 286 |
+
fs.writeFileSync(idx, h)
|
| 287 |
+
} else ok('index.html not present in ROOT (skipped)')
|
| 288 |
+
} catch (e) { fail('index.html cache-bust failed: ' + String(e.message || e)) }
|
| 289 |
+
|
| 290 |
+
/* ============ D) backend.py VERSION_ID -> v60 ============ */
|
| 291 |
+
try {
|
| 292 |
+
const bp = path.join(ROOT, 'backend.py')
|
| 293 |
+
if (fs.existsSync(bp)) {
|
| 294 |
+
let b = fs.readFileSync(bp, 'utf8')
|
| 295 |
+
const m = b.match(/VERSION_ID = "v\d+"/)
|
| 296 |
+
if (m) { b = b.replace(/VERSION_ID = "v\d+"/, 'VERSION_ID = "v60"'); ok('backend.py VERSION_ID -> v60') }
|
| 297 |
+
else ok('backend.py: no VERSION_ID marker (skipped)')
|
| 298 |
+
fs.writeFileSync(bp, b)
|
| 299 |
+
} else ok('backend.py not present in ROOT (skipped)')
|
| 300 |
+
} catch (e) { fail('backend.py VERSION_ID failed: ' + String(e.message || e)) }
|
| 301 |
+
|
| 302 |
+
console.log('[v60-patch] DONE: ' + LOG.length + ' ok, 0 fail')
|