一分钟搭建影视站,支持Docker等部署方式
feat: add URL search parameter handling and improve navigation between pages
developer2a6a7ed2396a
diff --git a/index.html b/index.htmlindex 16b1ba0..fe9887d 100644--- a/index.html+++ b/index.html@@ -449,5 +449,52 @@}});</script>++ <!-- 添加URL搜索参数处理脚本 -->+ <script>+ // 在页面加载完成后检查URL中的搜索参数+ document.addEventListener('DOMContentLoaded', function() {+ // 首先检查页面路径中的搜索参数 (格式: /s=keyword)+ const path = window.location.pathname;+ const searchPrefix = '/s=';++ if (path.startsWith(searchPrefix)) {+ // 提取搜索关键词+ const keyword = decodeURIComponent(path.substring(searchPrefix.length));+ if (keyword) {+ // 设置搜索框的值+ document.getElementById('searchInput').value = keyword;+ // 执行搜索+ setTimeout(() => {+ // 使用setTimeout确保其他DOM加载和初始化完成+ search();+ // 更新浏览器历史,不改变URL (保持搜索参数在地址栏)+ try {+ window.history.replaceState(+ { search: keyword },+ `搜索: ${keyword} - LibreTV`,+ window.location.href+ );+ } catch (e) {+ console.error('更新浏览器历史失败:', e);+ }+ }, 300);+ }+ }++ // 也检查查询字符串中的搜索参数 (格式: ?s=keyword)+ const urlParams = new URLSearchParams(window.location.search);+ const searchQuery = urlParams.get('s');++ if (searchQuery) {+ // 设置搜索框的值+ document.getElementById('searchInput').value = searchQuery;+ // 执行搜索+ setTimeout(() => {+ search();+ }, 300);+ }+ });+ </script></body></html>diff --git a/js/app.js b/js/app.jsindex 75a90ed..6452d41 100644--- a/js/app.js+++ b/js/app.js@@ -595,6 +595,23 @@ async function search() {return;}+ // 更新URL以包含搜索查询,方便分享和返回+ try {+ // 使用URI编码确保特殊字符能够正确显示+ const encodedQuery = encodeURIComponent(query);+ // 使用HTML5 History API更新URL,不刷新页面+ window.history.pushState(+ { search: query },+ `搜索: ${query} - LibreTV`,+ `/s=${encodedQuery}`+ );+ // 更新页面标题+ document.title = `搜索: ${query} - LibreTV`;+ } catch (e) {+ console.error('更新浏览器历史失败:', e);+ // 如果更新URL失败,继续执行搜索+ }+showLoading();try {diff --git a/player.html b/player.htmlindex 2ea0d7e..33a1dd2 100644--- a/player.html+++ b/player.html@@ -386,6 +386,33 @@// PASSWORD 变量将在这里被服务器端注入window.__ENV__.PASSWORD = "{{PASSWORD}}";+ // 改进返回上一页功能+ function goBack() {+ // 检查是否是从搜索页面进入的播放器+ const referrer = document.referrer;++ // 检查 referrer 是否包含搜索参数+ if (referrer && (referrer.includes('/s=') || referrer.includes('?s='))) {+ // 如果是从搜索页面来的,返回到搜索页面+ window.location.href = referrer;+ return;+ }++ // 如果是在iframe中打开的,尝试关闭iframe+ if (window.self !== window.top) {+ try {+ // 尝试调用父窗口的关闭播放器函数+ window.parent.closeVideoPlayer && window.parent.closeVideoPlayer();+ return;+ } catch (e) {+ console.error('调用父窗口closeVideoPlayer失败:', e);+ }+ }++ // 默认行为:返回上一页+ window.history.back();+ }+// =================================// ============== PLAYER ==========// =================================diff --git a/server.mjs b/server.mjsindex 9c8652a..705595b 100644--- a/server.mjs+++ b/server.mjs@@ -16,17 +16,22 @@ const password = process.env.PASSWORD || '';// 启用 CORSapp.use(cors());-app.get(['/', '/index.html', '/player.html'], async (req, res) => {+app.get(['/', '/index.html', '/player.html', '/s=*'], async (req, res) => {try {let content;- switch (req.path) {- case '/':- case '/index.html':- content = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8');- break;- case '/player.html':- content = fs.readFileSync(path.join(__dirname, 'player.html'), 'utf8');- break;+ // 处理搜索路径+ if (req.path.startsWith('/s=')) {+ content = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8');+ } else {+ switch (req.path) {+ case '/':+ case '/index.html':+ content = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8');+ break;+ case '/player.html':+ content = fs.readFileSync(path.join(__dirname, 'player.html'), 'utf8');+ break;+ }}if (password !== '') {const sha256 = await sha256Hash(password);diff --git a/watch.html b/watch.htmlindex 39e3984..73880a7 100644--- a/watch.html+++ b/watch.html@@ -5,6 +5,34 @@<meta http-equiv="refresh" content="0; url=player.html"><title>重定向到播放器</title><link rel="manifest" href="manifest.json">+ <script>+ // 获取当前URL的参数,并将它们传递给player.html+ window.onload = function() {+ // 获取当前URL的查询参数+ const currentParams = new URLSearchParams(window.location.search);++ // 创建player.html的URL+ let playerUrl = "player.html";++ // 如果有查询参数,添加到player.html的URL+ if (currentParams.toString()) {+ playerUrl += "?" + currentParams.toString();+ }++ // 获取来源URL (如果存在)+ const referrer = document.referrer;++ // 如果有referrer并且包含搜索参数,保存它以便返回+ if (referrer && (referrer.includes('/s=') || referrer.includes('?s='))) {+ // 如果已经有参数,添加&,否则添加?+ const separator = playerUrl.includes('?') ? '&' : '?';+ playerUrl += separator + 'referrer=' + encodeURIComponent(referrer);+ }++ // 重定向到player.html+ window.location.replace(playerUrl);+ };+ </script></head><body><p>如果您没有被自动重定向,请<a href="player.html">点击这里</a>前往播放页面。</p>