Peach: Local-first video metadata scraper and media manager.
perf(web): video.js 改成开播时才加载
longmeidao43fbe1853ac5
diff --git a/tests/test_dependency_policy.py b/tests/test_dependency_policy.pyindex 74bb28cf9..80e4d54ca 100644--- a/tests/test_dependency_policy.py+++ b/tests/test_dependency_policy.py@@ -80,7 +80,10 @@ class DependencyPolicyTests(unittest.TestCase):index = (ROOT / "web" / "index.html").read_text(encoding="utf-8")app = (ROOT / "web" / "app.js").read_text(encoding="utf-8")- self.assertIn(f'/vendor/videojs/{versions["video.js"]}/', index)+ # 首屏只剩 video.js 的样式表,脚本按需加载、版本钉在 app.js 的加载器里,+ # 所以两侧都要核。只核 index 的话,加载器里写错版本没人会拦。+ self.assertIn(f'/vendor/videojs/{versions["video.js"]}/video-js.min.css', index)+ self.assertIn(f'/vendor/videojs/{versions["video.js"]}/video.min.js', app)self.assertIn(f"Lucide static {versions['lucide-static']}", index)self.assertIn(f"Health Icons {versions['healthicons']}", index)self.assertIn(f"/vendor/swiper/{versions['swiper']}/", app)diff --git a/tests/test_follow_web.py b/tests/test_follow_web.pyindex 597353b92..52f3e55af 100644--- a/tests/test_follow_web.py+++ b/tests/test_follow_web.py@@ -1837,7 +1837,7 @@ class FollowWebSourceTests(unittest.TestCase):def test_follow_video_uses_the_shared_videojs_player_and_quality_control(self):self.assertPageContains('class="video-js vjs-big-play-centered" controls playsinline preload="metadata"')self.assertPageContains("if(followVideo){")- self.assertPageContains("const followPlayer=mountDetailPlayer(item,followVideo,false,{")+ self.assertPageContains("const followPlayer=await mountDetailPlayer(item,followVideo,false,{")self.assertPageContains("source:{src,type:selectedMedia?.media_type||item.media_type||'video/mp4'}")# 第四个参数是来源自己给的清晰度表:rule34video 把每档写成独立 mp4 字段,# videojs 的 qualityLevels 只认 HLS/DASH 的自适应轨道,看不到它们。@@ -1850,7 +1850,7 @@ class FollowWebSourceTests(unittest.TestCase):"function renderFollow", 1)[0]self.assertNotIn("await api(`/follow-qualities", detail,"清晰度回源不能挡住默认视频挂载")- self.assertLess(detail.index("const followPlayer=mountDetailPlayer"),+ self.assertLess(detail.index("const followPlayer=await mountDetailPlayer"),detail.index("wireFollowTelemetry"))self.assertPageContains('aria-label="播放器设置"')self.assertPageContains("data-player-quality-badge")diff --git a/tests/test_web_ui.py b/tests/test_web_ui.pyindex f82883dc1..3100612cb 100644--- a/tests/test_web_ui.py+++ b/tests/test_web_ui.py@@ -1084,14 +1084,31 @@ class WebUiSourceTests(unittest.TestCase):# 会在同一个 flex 容器里各占一半。点击入口后再取消 hidden、移除入口并自动播放。self.assertPageContains(".vwrap>video[hidden]{display:none}")self.assertPageContains(".gate{aspect-ratio:16/9;width:100%")- self.assertPageContains(- "else if(g)g.onclick=()=>{vv.hidden=false;g.remove();const mounted=mountDetailPlayer(it,vv,true)"+ # 挂播放器现在要等 video.js 到位,入口回调因此是 async。+ self.assertCode(+ "else if(g)g.onclick=async()=>{vv.hidden=false;g.remove();"+ "const mounted=await mountDetailPlayer(it,vv,true)")def test_detail_uses_pinned_videojs_and_authoritative_duration(self):self.assertPageContains('/vendor/videojs/8.24.0/video.min.js')self.assertPageContains('/vendor/videojs/8.24.0/video-js.min.css')+ def test_the_player_script_is_fetched_on_demand_instead_of_in_the_first_paint(self):+ """video.js 676KB,只有开始看片才用得上,和 Swiper 同一口径不进首屏。++ 语言包必须串在主脚本之后:`videojs.addLanguage` 要求先有 videojs,+ 并行加载会随机丢掉中文界面。+ """+ self.assertCode("const ensureVideojs=()=>{")+ self.assertCode(+ "videojsLoader=loadScript('/vendor/videojs/8.24.0/video.min.js')"+ ".then(()=>loadScript('/vendor/videojs/8.24.0/lang/zh-CN.js'))")+ self.assertPageLacks('<script src="/vendor/videojs',+ "播放器脚本才用得上,不进首屏")+ # 样式表留在首屏:它是 .video-js 的版式来源,等到点开才拉会先闪一帧裸 video。+ self.assertPageContains('<link rel="stylesheet" href="/vendor/videojs/8.24.0/video-js.min.css">')+def test_detail_player_controls_use_two_rows_and_offer_real_quality_levels(self):self.assertPageContains(".vwrap .video-js .vjs-big-play-button{left:50%;top:50%;width:56px;height:56px")self.assertPageContains("border-top:.72em solid transparent;border-bottom:.72em solid transparent;border-left:1.05em solid #fff")diff --git a/web/app.js b/web/app.jsindex 7097ce655..822e62dda 100644--- a/web/app.js+++ b/web/app.js@@ -946,11 +946,31 @@ function mountPlayerCenterControls(player){root.addEventListener('animationend',hideFeedback);player.on(['play','pause','ended'],onState);player.on(['waiting','seeking'],hideFeedback);player.on('dispose',()=>{clearTimeout(gestureTimer);playerRoot.removeEventListener('pointerdown',arm,true);playerRoot.removeEventListener('keydown',onKey,true)});sync();}-function mountDetailPlayer(it,video,autoplay,options={}){+/* 播放器按需加载,和灯箱的 Swiper 同一个理由:video.js 676KB,只有真的开始看片才+ 用得上,进首屏就是每次开页都白下一遍。灯箱那边还要等一张样式表,所以它保留自己的+ Promise.all;这里只有脚本,用下面这个最小加载器。主脚本与语言包有依赖+ ——`videojs.addLanguage` 得先有 videojs——必须串行,不能 Promise.all。 */+const loadScript=src=>new Promise((resolve,reject)=>{+ const script=document.createElement('script');+ script.src=src;script.onload=resolve;+ script.onerror=()=>reject(new Error(`script unavailable: ${src}`));+ document.head.appendChild(script)});+let videojsLoader=null;+const ensureVideojs=()=>{+ if(globalThis.videojs)return Promise.resolve(globalThis.videojs);+ return videojsLoader||(videojsLoader=loadScript('/vendor/videojs/8.24.0/video.min.js')+ .then(()=>loadScript('/vendor/videojs/8.24.0/lang/zh-CN.js'))+ .then(()=>globalThis.videojs)+ /* 失败要把 loader 清空,否则一次网络抖动之后这一整页都再也挂不上播放器了。 */+ .catch(error=>{videojsLoader=null;throw error}));+};+async function mountDetailPlayer(it,video,autoplay,options={}){if(detailPlayer)return detailPlayer;const statsButton=$('#playerStatsBtn'),statsPanel=$('#playerStats');const source=()=>options.source?Promise.resolve(options.source):detailStreamSource(it);- if(!globalThis.videojs){+ /* 拉不到就退回原生 video,和以前「页面里没有 videojs」是同一个兜底出口。 */+ try{await ensureVideojs()}+ catch(_e){video.controls=true;source().then(next=>{video.src=next.src;if(autoplay)video.play().catch(()=>{})}).catch(()=>{});return null;@@ -3327,7 +3347,7 @@ async function openFollowDetail(id,push=true,mediaIndex=null,preserveReturn=fals一点,详情里就会先留下一个没有 src 的空视频框。 */const qualitiesPromise=api(`/follow-qualities?id=${encodeURIComponent(item.id)}`).then(answer=>answer?.qualities?.length?answer.qualities:null).catch(()=>null);- const followPlayer=mountDetailPlayer(item,followVideo,false,{+ const followPlayer=await mountDetailPlayer(item,followVideo,false,{source:{src,type:selectedMedia?.media_type||item.media_type||'video/mp4'},checkSourceStatus:false,qualitiesPromise@@ -6236,8 +6256,8 @@ async function openItem(id,push=true,queueContext=null,anchor=null){followAuthor='';followProvider='';followTags=new Set();followMediaView='videos';followFilter='saved';route(followViewPath());openFollow(false)};}- else if(g)g.onclick=()=>{vv.hidden=false;g.remove();const mounted=mountDetailPlayer(it,vv,true);stopAmbient=mountPlayerAmbient(vv);mounted?.one?.('dispose',stopAmbient)};- else{const mounted=mountDetailPlayer(it,vv,true);stopAmbient=mountPlayerAmbient(vv);mounted?.one?.('dispose',stopAmbient)}+ else if(g)g.onclick=async()=>{vv.hidden=false;g.remove();const mounted=await mountDetailPlayer(it,vv,true);stopAmbient=mountPlayerAmbient(vv);mounted?.one?.('dispose',stopAmbient)};+ else{const mounted=await mountDetailPlayer(it,vv,true);stopAmbient=mountPlayerAmbient(vv);mounted?.one?.('dispose',stopAmbient)}vv.addEventListener('emptied',()=>stopAmbient(),{once:true});buildBars();scrollItemDetailIntoView();diff --git a/web/index.html b/web/index.htmlindex 59c4577b6..51c7fc5bb 100644--- a/web/index.html+++ b/web/index.html@@ -201,8 +201,8 @@<div class="toasts" id="toasts" role="status" aria-live="polite"></div>-<script src="/vendor/videojs/8.24.0/video.min.js"></script>-<script src="/vendor/videojs/8.24.0/lang/zh-CN.js"></script>+<!-- video.js(676KB)首屏用不上,与 Swiper 同一口径:由 app.js 的 ensureVideojs()+ 在首次挂播放器时才拉。版本钉在那个加载器里。 --><script type="module" src="/app.js"></script></body></html>