一分钟搭建影视站,支持Docker等部署方式
fix(player): 保留视频ID和返回URL参数,优化嵌套播放链接处理
developerf6c85b02bdfb
diff --git a/js/app.js b/js/app.jsindex 987c80e..d09d2c7 100644--- a/js/app.js+++ b/js/app.js@@ -1051,8 +1051,19 @@ function playVideo(url, vod_name, sourceCode, episodeIndex = 0) {// 获取当前路径作为返回页面let currentPath = window.location.href;+ // 如果有ID参数,也传递过去+ let videoId = '';+ try {+ // 尝试从URL中提取id参数(如果存在)+ const urlObj = new URL(url);+ videoId = urlObj.searchParams.get('id') || '';+ } catch (e) {+ // 忽略错误+ console.log('解析视频URL获取ID失败:', e);+ }+// 构建播放页面URL,使用watch.html作为中间跳转页- let watchUrl = `watch.html?id=${currentEpisodes[episodeIndex].id || ''}&source=${sourceCode || ''}&url=${encodeURIComponent(url)}&index=${episodeIndex}&title=${encodeURIComponent(vod_name || '')}`;+ let watchUrl = `watch.html?id=${videoId}&source=${sourceCode || ''}&url=${encodeURIComponent(url)}&index=${episodeIndex}&title=${encodeURIComponent(vod_name || '')}`;// 添加返回URL参数watchUrl += `&back=${encodeURIComponent(currentPath)}`;diff --git a/js/player.js b/js/player.jsindex b8f8dbd..8c93db4 100644--- a/js/player.js+++ b/js/player.js@@ -893,28 +893,25 @@ function playEpisode(index) {currentEpisodeIndex = index;videoHasEnded = false; // 重置视频结束标志- // 获取当前URL参数,保留source参数- const urlParams = new URL(window.location.href);- const sourceName = urlParams.searchParams.get('source') || ''; // Use searchParams- const sourceCode = urlParams.searchParams.get('source_code') || ''; // Use searchParams-- // 更新URL,不刷新页面,保留source参数- const newUrl = new URL(window.location.href);+ // 获取当前URL的所有参数+ const currentUrl = new URL(window.location.href);+ const urlParams = currentUrl.searchParams;+ const sourceName = urlParams.get('source') || '';+ const sourceCode = urlParams.get('source_code') || '';+ const videoId = urlParams.get('id') || '';+ const returnUrl = urlParams.get('returnUrl') || '';++ // 构建新的URL,保持查询参数但更新index和url+ const newUrl = new URL(window.location.origin + window.location.pathname);+ // 保留所有原始参数+ for(const [key, value] of urlParams.entries()) {+ newUrl.searchParams.set(key, value);+ }+ // 更新需要变更的参数newUrl.searchParams.set('index', index);newUrl.searchParams.set('url', url);- if (sourceName) {- newUrl.searchParams.set('source', sourceName);- }- if (sourceCode) {- newUrl.searchParams.set('source_code', sourceCode);- }-- // 保留referrer参数,如果存在- const referrer = urlParams.searchParams.get('referrer'); // Use searchParams- if (referrer) {- newUrl.searchParams.set('referrer', referrer);- }+ // 使用replaceState更新URL,这样不会增加浏览历史记录window.history.replaceState({}, '', newUrl);// 更新播放器diff --git a/js/ui.js b/js/ui.jsindex bab6b5d..4429ddd 100644--- a/js/ui.js+++ b/js/ui.js@@ -531,24 +531,46 @@ function playFromHistory(url, title, episodeIndex, playbackPosition = 0) {}localStorage.setItem('lastPageUrl', currentPath);- // 构造带播放进度参数的URL- const positionParam = `&position=${Math.floor(playbackPosition || 0)}`;+ // 构造播放器URL+ let playerUrl;- if (url.includes('?')) {+ // 检查URL是否是嵌套的player.html链接+ if (url.includes('player.html') || url.includes('watch.html')) {+ console.log('检测到嵌套播放链接,解析真实URL');+ try {+ const nestedUrl = new URL(url, window.location.origin);+ const nestedParams = nestedUrl.searchParams;+ const realVideoUrl = nestedParams.get('url') || url;++ // 构造更干净的播放链接+ playerUrl = `player.html?url=${encodeURIComponent(realVideoUrl)}&title=${encodeURIComponent(title)}&index=${episodeIndex}&position=${Math.floor(playbackPosition || 0)}&returnUrl=${encodeURIComponent(currentPath)}`;++ // 如果有source和source_code,也添加+ const source = nestedParams.get('source');+ const sourceCode = nestedParams.get('source_code');+ if (source) playerUrl += `&source=${encodeURIComponent(source)}`;+ if (sourceCode) playerUrl += `&source_code=${encodeURIComponent(sourceCode)}`;+ } catch (e) {+ console.error('解析嵌套URL出错:', e);+ // fallback到简单URL+ playerUrl = `player.html?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&index=${episodeIndex}&position=${Math.floor(playbackPosition || 0)}&returnUrl=${encodeURIComponent(currentPath)}`;+ }+ } else if (url.includes('?')) {// URL已有参数,添加索引和位置参数- const playUrl = new URL(url);+ const playUrl = new URL(url, window.location.origin);if (!playUrl.searchParams.has('index') && episodeIndex > 0) {playUrl.searchParams.set('index', episodeIndex);}playUrl.searchParams.set('position', Math.floor(playbackPosition || 0).toString());// 添加返回URLplayUrl.searchParams.set('returnUrl', encodeURIComponent(currentPath));- showVideoPlayer(playUrl.toString());+ playerUrl = playUrl.toString();} else {// 原始URL,构造player页面链接- const playerUrl = `player.html?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&index=${episodeIndex}&position=${Math.floor(playbackPosition || 0)}&returnUrl=${encodeURIComponent(currentPath)}`;- showVideoPlayer(playerUrl);+ playerUrl = `player.html?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&index=${episodeIndex}&position=${Math.floor(playbackPosition || 0)}&returnUrl=${encodeURIComponent(currentPath)}`;}++ showVideoPlayer(playerUrl);} catch (e) {console.error('从历史记录播放失败:', e);// 回退到原始简单URLdiff --git a/js/watch.js b/js/watch.jsindex c990dab..08abfdb 100644--- a/js/watch.js+++ b/js/watch.js@@ -31,7 +31,7 @@ window.onload = function() {currentStatus++;}, 1000);- // 如果有查询参数,添加到player.html的URL+ // 确保保留所有原始参数,但可能会覆盖一些参数if (currentParams.toString()) {playerUrl += "?" + currentParams.toString();}@@ -59,8 +59,12 @@ window.onload = function() {}// 将返回URL添加到player.html的参数中- const separator = playerUrl.includes('?') ? '&' : '?';- playerUrl += separator + 'returnUrl=' + encodeURIComponent(returnUrl);+ // 检查playerUrl是否已有returnUrl参数+ const playerUrlObj = new URL(playerUrl, window.location.origin);+ if (!playerUrlObj.searchParams.has('returnUrl')) {+ const separator = playerUrl.includes('?') ? '&' : '?';+ playerUrl += separator + 'returnUrl=' + encodeURIComponent(returnUrl);+ }// 同时保存在localStorage中,作为备用localStorage.setItem('lastPageUrl', returnUrl);@@ -71,6 +75,13 @@ window.onload = function() {localStorage.setItem('searchPageUrl', returnUrl);}+ // 确保videoId和sourceCode等重要参数被保留+ // 这是调试代码,可以帮助识别问题,可选择保留或删除+ console.log('传递给player.html的参数:');+ (new URL(playerUrl, window.location.origin)).searchParams.forEach((value, key) => {+ console.log(`- ${key}: ${value}`);+ });+// 更新手动重定向链接if (manualRedirect) {manualRedirect.href = playerUrl;