一分钟搭建影视站,支持Docker等部署方式
Add viewing history feature with UI panel and local storage support
developer28a58512d47a
diff --git a/css/styles.css b/css/styles.cssindex 6501198..e746311 100644--- a/css/styles.css+++ b/css/styles.css@@ -598,3 +598,96 @@ body {font-weight: 500;margin-right: 0.35rem;}++/* 历史记录面板样式 */+.history-panel {+ box-shadow: 2px 0 10px rgba(0,0,0,0.5);+ transition: transform 0.3s ease-in-out;+}++.history-panel.show {+ transform: translateX(0);+}++.history-item {+ background: #1a1a1a;+ border-radius: 8px;+ border: 1px solid #333;+ overflow: hidden;+ transition: all 0.2s ease;+ padding: 12px;+}++.history-item:hover {+ transform: translateY(-2px);+ border-color: #444;+ box-shadow: 0 4px 8px rgba(0,0,0,0.2);+}++.history-info {+ padding: 0;+}++.history-title {+ font-weight: 500;+ font-size: 1rem;+ margin-bottom: 4px;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+}++.history-meta {+ color: #bbb;+ font-size: 0.8rem;+ display: flex;+ flex-wrap: wrap;+}++.history-episode {+ color: #3b82f6;+}++.history-source {+ color: #10b981;+}++.history-time {+ color: #888;+ font-size: 0.75rem;+ margin-top: 4px;+}++.history-separator {+ color: #666;+}++.history-thumbnail {+ width: 100%;+ height: 90px;+ background-color: #222;+ overflow: hidden;+}++.history-thumbnail img {+ width: 100%;+ height: 100%;+ object-fit: cover;+}++.history-info {+ padding: 10px;+}++.history-time {+ color: #888;+ font-size: 0.8rem;+ margin-top: 4px;+}++.history-title {+ font-weight: 500;+ white-space: nowrap;+ overflow: hidden;+ text-overflow: ellipsis;+}diff --git a/index.html b/index.htmlindex 3b6f9d6..b0578c9 100644--- a/index.html+++ b/index.html@@ -35,6 +35,12 @@</head><body class="page-bg text-white"><div class="fixed top-4 right-4 z-50 flex items-center space-x-4">+ <!-- 添加历史记录按钮 -->+ <button onclick="toggleHistory(event)" class="bg-[#222] hover:bg-[#333] border border-[#333] hover:border-white rounded-lg px-4 py-2 transition-colors" aria-label="观看历史">+ <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>+ </svg>+ </button><button onclick="toggleSettings(event)" class="bg-[#222] hover:bg-[#333] border border-[#333] hover:border-white rounded-lg px-4 py-2 transition-colors" aria-label="打开设置"><svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path>@@ -42,6 +48,23 @@</svg></button></div>++ <!-- 历史记录面板 -->+ <div id="historyPanel" class="history-panel fixed left-0 top-0 h-full w-80 bg-[#111] border-r border-[#333] p-6 z-40 overflow-y-auto transform -translate-x-full transition-transform duration-300" aria-label="观看历史" aria-hidden="true">+ <div class="flex justify-between items-center mb-6">+ <h3 class="text-xl font-bold gradient-text">观看历史</h3>+ <button onclick="toggleHistory()" class="text-gray-400 hover:text-white" aria-label="关闭历史">×</button>+ </div>+ <div id="historyList" class="space-y-4">+ <!-- 历史记录将在这里动态显示 -->+ <div class="text-center text-gray-500 py-8">暂无观看记录</div>+ </div>+ <div class="mt-6 text-center">+ <button onclick="clearViewingHistory()" class="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg text-sm transition-colors">+ 清空历史记录+ </button>+ </div>+ </div><!-- 设置面板 --><div id="settingsPanel" class="settings-panel fixed right-0 top-0 h-full w-80 bg-[#111] border-l border-[#333] p-6 z-40 overflow-y-auto" aria-label="设置面板" aria-hidden="true">diff --git a/js/app.js b/js/app.jsindex 910f191..8441e75 100644--- a/js/app.js+++ b/js/app.js@@ -850,19 +850,45 @@ async function showDetails(id, vod_name, sourceCode) {}}-// 更新播放视频函数,修改为在新标签页中打开播放页面+// 更新播放视频函数,修改为在新标签页中打开播放页面,并保存到历史记录function playVideo(url, vod_name, episodeIndex = 0) {if (!url) {showToast('无效的视频链接', 'error');return;}+ // 获取当前视频来源名称(从模态框标题中提取)+ let sourceName = '';+ const modalTitle = document.getElementById('modalTitle');+ if (modalTitle) {+ const sourceSpan = modalTitle.querySelector('span.text-gray-400');+ if (sourceSpan) {+ // 提取括号内的来源名称, 例如从 "(黑木耳)" 提取 "黑木耳"+ const sourceText = sourceSpan.textContent;+ const match = sourceText.match(/\(([^)]+)\)/);+ if (match && match[1]) {+ sourceName = match[1].trim();+ }+ }+ }+// 保存当前状态到localStorage,让播放页面可以获取localStorage.setItem('currentVideoTitle', currentVideoTitle);localStorage.setItem('currentEpisodeIndex', episodeIndex);localStorage.setItem('currentEpisodes', JSON.stringify(currentEpisodes));localStorage.setItem('episodesReversed', episodesReversed);+ // 保存到观看历史,添加sourceName+ if (typeof addToViewingHistory === 'function') {+ addToViewingHistory({+ title: vod_name || currentVideoTitle,+ url: url,+ episodeIndex: episodeIndex,+ sourceName: sourceName,+ timestamp: Date.now()+ });+ }+// 构建播放页面URL,传递必要参数const playerUrl = `player.html?url=${encodeURIComponent(url)}&title=${encodeURIComponent(vod_name)}&index=${episodeIndex}`;diff --git a/js/ui.js b/js/ui.jsindex ecf991f..1026099 100644--- a/js/ui.js+++ b/js/ui.js@@ -232,3 +232,193 @@ function clearSearchHistory() {showToast('清除搜索历史失败', 'error');}}++// 历史面板相关函数+function toggleHistory(e) {+ if (e) e.stopPropagation();++ const panel = document.getElementById('historyPanel');+ if (panel) {+ panel.classList.toggle('show');++ // 如果打开了历史记录面板,则加载历史数据+ if (panel.classList.contains('show')) {+ loadViewingHistory();+ }++ // 如果设置面板是打开的,则关闭它+ const settingsPanel = document.getElementById('settingsPanel');+ if (settingsPanel && settingsPanel.classList.contains('show')) {+ settingsPanel.classList.remove('show');+ }+ }+}++// 格式化时间戳为友好的日期时间格式+function formatTimestamp(timestamp) {+ const date = new Date(timestamp);+ const now = new Date();+ const diff = now - date;++ // 小于1小时,显示"X分钟前"+ if (diff < 3600000) {+ const minutes = Math.floor(diff / 60000);+ return minutes <= 0 ? '刚刚' : `${minutes}分钟前`;+ }++ // 小于24小时,显示"X小时前"+ if (diff < 86400000) {+ const hours = Math.floor(diff / 3600000);+ return `${hours}小时前`;+ }++ // 小于7天,显示"X天前"+ if (diff < 604800000) {+ const days = Math.floor(diff / 86400000);+ return `${days}天前`;+ }++ // 其他情况,显示完整日期+ const year = date.getFullYear();+ const month = (date.getMonth() + 1).toString().padStart(2, '0');+ const day = date.getDate().toString().padStart(2, '0');+ const hour = date.getHours().toString().padStart(2, '0');+ const minute = date.getMinutes().toString().padStart(2, '0');++ return `${year}-${month}-${day} ${hour}:${minute}`;+}++// 获取观看历史记录+function getViewingHistory() {+ try {+ const data = localStorage.getItem('viewingHistory');+ return data ? JSON.parse(data) : [];+ } catch (e) {+ console.error('获取观看历史失败:', e);+ return [];+ }+}++// 加载观看历史并渲染+function loadViewingHistory() {+ const historyList = document.getElementById('historyList');+ if (!historyList) return;++ const history = getViewingHistory();++ if (history.length === 0) {+ historyList.innerHTML = `<div class="text-center text-gray-500 py-8">暂无观看记录</div>`;+ return;+ }++ // 渲染历史记录+ historyList.innerHTML = history.map(item => {+ // 防止XSS+ const safeTitle = item.title+ .replace(/</g, '<')+ .replace(/>/g, '>')+ .replace(/"/g, '"');++ const safeSource = item.sourceName ?+ item.sourceName.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"') :+ '未知来源';++ const episodeText = item.episodeIndex !== undefined ?+ `第${item.episodeIndex + 1}集` : '';++ // 构建历史记录项HTML,移除缩略图,添加来源信息+ return `+ <div class="history-item cursor-pointer" onclick="playFromHistory('${item.url}', '${safeTitle}', ${item.episodeIndex || 0})">+ <div class="history-info">+ <div class="history-title">${safeTitle}</div>+ <div class="history-meta">+ <span class="history-episode">${episodeText}</span>+ ${episodeText ? '<span class="history-separator mx-1">·</span>' : ''}+ <span class="history-source">${safeSource}</span>+ </div>+ <div class="history-time">${formatTimestamp(item.timestamp)}</div>+ </div>+ </div>+ `;+ }).join('');+}++// 从历史记录播放+function playFromHistory(url, title, episodeIndex) {+ // 构造播放页面URL并跳转+ const playerUrl = `player.html?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&index=${episodeIndex}`;+ window.open(playerUrl, '_blank');+}++// 添加观看历史+function addToViewingHistory(videoInfo) {+ try {+ const history = getViewingHistory();++ // 检查是否已经存在相同的项目(基于URL)+ const existingIndex = history.findIndex(item => item.url === videoInfo.url);+ if (existingIndex !== -1) {+ // 存在则移除旧的记录+ history.splice(existingIndex, 1);+ }++ // 添加新记录到最前面+ history.unshift({+ ...videoInfo,+ timestamp: Date.now()+ });++ // 限制历史记录数量为50条+ const maxHistoryItems = 50;+ if (history.length > maxHistoryItems) {+ history.splice(maxHistoryItems);+ }++ // 保存到本地存储+ localStorage.setItem('viewingHistory', JSON.stringify(history));+ } catch (e) {+ console.error('保存观看历史失败:', e);+ }+}++// 清空观看历史+function clearViewingHistory() {+ try {+ localStorage.removeItem('viewingHistory');+ loadViewingHistory(); // 重新加载空的历史记录+ showToast('观看历史已清空', 'success');+ } catch (e) {+ console.error('清除观看历史失败:', e);+ showToast('清除观看历史失败', 'error');+ }+}++// 更新toggleSettings函数以处理历史面板互动+const originalToggleSettings = toggleSettings;+toggleSettings = function(e) {+ if (e) e.stopPropagation();++ // 原始设置面板切换逻辑+ originalToggleSettings(e);++ // 如果历史记录面板是打开的,则关闭它+ const historyPanel = document.getElementById('historyPanel');+ if (historyPanel && historyPanel.classList.contains('show')) {+ historyPanel.classList.remove('show');+ }+};++// 点击外部关闭历史面板+document.addEventListener('DOMContentLoaded', function() {+ document.addEventListener('click', function(e) {+ const historyPanel = document.getElementById('historyPanel');+ const historyButton = document.querySelector('button[onclick="toggleHistory(event)"]');++ if (historyPanel && historyButton &&+ !historyPanel.contains(e.target) &&+ !historyButton.contains(e.target) &&+ historyPanel.classList.contains('show')) {+ historyPanel.classList.remove('show');+ }+ });+});diff --git a/player.html b/player.htmlindex d57732e..0217f7b 100644--- a/player.html+++ b/player.html@@ -546,6 +546,9 @@// 视频加载完成后重新设置进度条点击监听setupProgressBarPreciseClicks();++ // 视频加载成功后,在稍微延迟后将其添加到观看历史+ setTimeout(saveToHistory, 3000);});dp.on('error', function() {@@ -942,6 +945,48 @@e.stopPropagation();dp.seek(clickTime);}++ // 在播放器初始化后添加视频到历史记录+ function saveToHistory() {+ // 尝试从URL中获取参数+ const urlParams = new URLSearchParams(window.location.search);+ const sourceName = urlParams.get('source') || '';++ // 构建要保存的视频信息对象+ const videoInfo = {+ title: currentVideoTitle,+ url: window.location.href, // 保存完整URL以便能直接打开+ episodeIndex: currentEpisodeIndex,+ sourceName: sourceName,+ timestamp: Date.now()+ };++ // 如果外部定义了addToViewingHistory函数,则调用它+ if (typeof addToViewingHistory === 'function') {+ addToViewingHistory(videoInfo);+ } else {+ // 否则直接使用本地实现+ try {+ const history = JSON.parse(localStorage.getItem('viewingHistory') || '[]');++ // 检查是否已经存在相同的项目+ const existingIndex = history.findIndex(item => item.url === videoInfo.url);+ if (existingIndex !== -1) {+ history.splice(existingIndex, 1);+ }++ // 添加新记录到最前面+ history.unshift(videoInfo);++ // 限制历史记录数量为50条+ if (history.length > 50) history.splice(50);++ localStorage.setItem('viewingHistory', JSON.stringify(history));+ } catch (e) {+ console.error('保存观看历史失败:', e);+ }+ }+ }</script></body></html>