Skip to content

一分钟搭建影视站,支持Docker等部署方式

Enhance playback history feature with progress tracking and restore hint

developer8e0d510af336
diff --git a/css/styles.css b/css/styles.css
index afbb7b6..e9eb0b3 100644
--- a/css/styles.css
+++ b/css/styles.css
@@ -625,7 +625,7 @@ body {
border: 1px solid #333;
overflow: hidden;
transition: all 0.2s ease;
- padding: 8px 12px; /* 减小内边距 */
+ padding: 10px 14px;
position: relative;
margin-bottom: 8px; /* 减小底部间距 */
width: 100%; /* 确保宽度一致 */
@@ -649,6 +649,7 @@ body {
.history-info {
padding: 0; /* 移除额外的内边距 */
+ min-height: 70px;
}
.history-title {
@@ -715,3 +716,49 @@ body {
overflow: hidden;
text-overflow: ellipsis;
}
+
+/* 添加播放进度条样式 */
+.history-progress {
+ margin: 5px 0;
+}
+
+.progress-bar {
+ height: 3px;
+ background-color: rgba(255, 255, 255, 0.1);
+ border-radius: 2px;
+ overflow: hidden;
+ margin-bottom: 2px;
+}
+
+.progress-filled {
+ height: 100%;
+ background: linear-gradient(to right, #00ccff, #3b82f6);
+ border-radius: 2px;
+}
+
+.progress-text {
+ font-size: 10px;
+ color: #888;
+ text-align: right;
+}
+
+/* 添加恢复播放提示样式 */
+.position-restore-hint {
+ position: absolute;
+ bottom: 60px;
+ left: 50%;
+ transform: translateX(-50%) translateY(20px);
+ background-color: rgba(0, 0, 0, 0.7);
+ color: white;
+ padding: 8px 16px;
+ border-radius: 4px;
+ font-size: 14px;
+ z-index: 100;
+ opacity: 0;
+ transition: all 0.3s ease;
+}
+
+.position-restore-hint.show {
+ opacity: 1;
+ transform: translateX(-50%) translateY(0);
+}
diff --git a/js/ui.js b/js/ui.js
index df2269f..e305962 100644
--- a/js/ui.js
+++ b/js/ui.js
@@ -326,12 +326,29 @@ function loadViewingHistory() {
const episodeText = item.episodeIndex !== undefined ?
`第${item.episodeIndex + 1}集` : '';
+ // 格式化进度信息
+ let progressHtml = '';
+ if (item.playbackPosition && item.duration && item.playbackPosition > 10 && item.playbackPosition < item.duration * 0.95) {
+ const percent = Math.round((item.playbackPosition / item.duration) * 100);
+ const formattedTime = formatPlaybackTime(item.playbackPosition);
+ const formattedDuration = formatPlaybackTime(item.duration);
+
+ progressHtml = `
+ <div class="history-progress">
+ <div class="progress-bar">
+ <div class="progress-filled" style="width:${percent}%"></div>
+ </div>
+ <div class="progress-text">${formattedTime} / ${formattedDuration}</div>
+ </div>
+ `;
+ }
+
// 为防止XSS,使用encodeURIComponent编码URL
const safeURL = encodeURIComponent(item.url);
// 构建历史记录项HTML,添加删除按钮,需要放在position:relative的容器中
return `
- <div class="history-item cursor-pointer relative group" onclick="playFromHistory('${item.url}', '${safeTitle}', ${item.episodeIndex || 0})">
+ <div class="history-item cursor-pointer relative group" onclick="playFromHistory('${item.url}', '${safeTitle}', ${item.episodeIndex || 0}, ${item.playbackPosition || 0})">
<button onclick="event.stopPropagation(); deleteHistoryItem('${safeURL}')"
class="absolute right-2 top-2 opacity-0 group-hover:opacity-100 transition-opacity duration-200 text-gray-400 hover:text-red-400 p-1 rounded-full hover:bg-gray-800 z-10"
title="删除记录">
@@ -346,6 +363,7 @@ function loadViewingHistory() {
${episodeText ? '<span class="history-separator mx-1">·</span>' : ''}
<span class="history-source">${safeSource}</span>
</div>
+ ${progressHtml}
<div class="history-time">${formatTimestamp(item.timestamp)}</div>
</div>
</div>
@@ -358,6 +376,16 @@ function loadViewingHistory() {
}
}
+// 格式化播放时间为 mm:ss 格式
+function formatPlaybackTime(seconds) {
+ if (!seconds || isNaN(seconds)) return '00:00';
+
+ const minutes = Math.floor(seconds / 60);
+ const remainingSeconds = Math.floor(seconds % 60);
+
+ return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
+}
+
// 删除单个历史记录项
function deleteHistoryItem(encodedUrl) {
try {
@@ -385,16 +413,28 @@ function deleteHistoryItem(encodedUrl) {
}
// 从历史记录播放
-function playFromHistory(url, title, episodeIndex) {
- // 构造播放页面URL并跳转
- // 检查URL是否已经包含必要参数
+function playFromHistory(url, title, episodeIndex, playbackPosition = 0) {
+ // 构造带播放进度参数的URL
+ const positionParam = playbackPosition > 10 ? `&position=${Math.floor(playbackPosition)}` : '';
+
if (url.includes('?')) {
- // URL已有参数,添加&index=
- const playUrl = `${url}${url.includes('index=') ? '' : `&index=${episodeIndex}`}`;
+ // URL已有参数,确保包含必要参数
+ let playUrl = url;
+
+ // 添加集数参数(如果没有)
+ if (!url.includes('index=') && episodeIndex > 0) {
+ playUrl += `&index=${episodeIndex}`;
+ }
+
+ // 添加播放位置参数
+ if (playbackPosition > 10) {
+ playUrl += positionParam;
+ }
+
window.open(playUrl, '_blank');
} else {
// 原始URL,添加完整参数
- const playerUrl = `player.html?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&index=${episodeIndex}`;
+ const playerUrl = `player.html?url=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&index=${episodeIndex}${positionParam}`;
window.open(playerUrl, '_blank');
}
}
@@ -417,6 +457,12 @@ function addToViewingHistory(videoInfo) {
existingItem.sourceName = videoInfo.sourceName;
}
+ // 更新播放进度信息,仅当新进度有效且大于10秒时
+ if (videoInfo.playbackPosition && videoInfo.playbackPosition > 10) {
+ existingItem.playbackPosition = videoInfo.playbackPosition;
+ existingItem.duration = videoInfo.duration || existingItem.duration;
+ }
+
// 更新URL,确保能够跳转到正确的集数
existingItem.url = videoInfo.url;
diff --git a/player.html b/player.html
index f379635..38bff0c 100644
--- a/player.html
+++ b/player.html
@@ -246,6 +246,7 @@
let userClickedPosition = null; // 记录用户点击的位置
let shortcutHintTimeout = null; // 用于控制快捷键提示显示时间
let adFilteringEnabled = true; // 默认开启广告过滤
+ let progressSaveInterval = null; // 定期保存进度的计时器
// 页面加载
document.addEventListener('DOMContentLoaded', function() {
@@ -288,6 +289,20 @@
// 初始化播放器
if (videoUrl) {
initPlayer(videoUrl);
+
+ // 尝试从URL参数中恢复播放位置
+ const position = urlParams.get('position');
+ if (position) {
+ setTimeout(() => {
+ if (dp && dp.video) {
+ const positionNum = parseInt(position);
+ if (!isNaN(positionNum) && positionNum > 0) {
+ dp.seek(positionNum);
+ showPositionRestoreHint(positionNum);
+ }
+ }
+ }, 1500);
+ }
} else {
showError('无效的视频链接');
}
@@ -311,6 +326,9 @@
// 添加键盘快捷键事件监听
document.addEventListener('keydown', handleKeyboardShortcuts);
+
+ // 添加页面离开事件监听,保存播放位置
+ window.addEventListener('beforeunload', saveCurrentProgress);
});
// 处理键盘快捷键
@@ -549,6 +567,9 @@
// 视频加载成功后,在稍微延迟后将其添加到观看历史
setTimeout(saveToHistory, 3000);
+
+ // 启动定期保存播放进度
+ startProgressSaveInterval();
});
dp.on('error', function() {
@@ -606,6 +627,9 @@
dp.on('ended', function() {
videoHasEnded = true; // 标记视频已自然结束
+ // 视频已播放完,清除播放进度记录
+ clearVideoProgress();
+
// 如果启用了自动连播,并且有下一集可播放,则自动播放下一集
if (autoplayEnabled && currentEpisodeIndex < currentEpisodes.length - 1) {
console.log('视频播放结束,自动播放下一集');
@@ -777,6 +801,17 @@
function playEpisode(index) {
if (index < 0 || index >= currentEpisodes.length) return;
+ // 保存当前播放进度(如果正在播放)
+ if (dp && dp.video && !dp.video.paused && !videoHasEnded) {
+ saveCurrentProgress();
+ }
+
+ // 清除进度保存计时器
+ if (progressSaveInterval) {
+ clearInterval(progressSaveInterval);
+ progressSaveInterval = null;
+ }
+
// 首先隐藏之前可能显示的错误
document.getElementById('error').style.display = 'none';
// 显示加载指示器
@@ -962,6 +997,15 @@
const urlParams = new URLSearchParams(window.location.search);
const sourceName = urlParams.get('source') || '';
+ // 获取当前播放进度
+ let currentPosition = 0;
+ let videoDuration = 0;
+
+ if (dp && dp.video) {
+ currentPosition = dp.video.currentTime;
+ videoDuration = dp.video.duration;
+ }
+
// 构建要保存的视频信息对象
const videoInfo = {
title: currentVideoTitle,
@@ -969,7 +1013,10 @@
url: `player.html?title=${encodeURIComponent(currentVideoTitle)}&source=${encodeURIComponent(sourceName)}`,
episodeIndex: currentEpisodeIndex,
sourceName: sourceName,
- timestamp: Date.now()
+ timestamp: Date.now(),
+ // 添加播放进度信息
+ playbackPosition: currentPosition > 10 ? currentPosition : 0,
+ duration: videoDuration
};
// 如果外部定义了addToViewingHistory函数,则调用它
@@ -986,6 +1033,9 @@
// 存在则更新现有记录的集数、时间戳和URL
history[existingIndex].episodeIndex = currentEpisodeIndex;
history[existingIndex].timestamp = Date.now();
+ // 更新播放进度信息
+ history[existingIndex].playbackPosition = currentPosition > 10 ? currentPosition : history[existingIndex].playbackPosition;
+ history[existingIndex].duration = videoDuration || history[existingIndex].duration;
// 同时更新URL以保存当前的集数状态
history[existingIndex].url = window.location.href;
@@ -1007,6 +1057,99 @@
}
}
}
+
+ // 显示恢复位置提示
+ function showPositionRestoreHint(position) {
+ if (!position || position < 10) return;
+
+ // 创建提示元素
+ const hint = document.createElement('div');
+ hint.className = 'position-restore-hint';
+ hint.innerHTML = `
+ <div class="hint-content">
+ 已从 ${formatTime(position)} 继续播放
+ </div>
+ `;
+
+ // 添加到播放器容器
+ const playerContainer = document.querySelector('.player-container');
+ playerContainer.appendChild(hint);
+
+ // 显示提示
+ setTimeout(() => {
+ hint.classList.add('show');
+
+ // 3秒后隐藏
+ setTimeout(() => {
+ hint.classList.remove('show');
+ setTimeout(() => hint.remove(), 300);
+ }, 3000);
+ }, 100);
+ }
+
+ // 格式化时间为 mm:ss 格式
+ function formatTime(seconds) {
+ if (isNaN(seconds)) return '00:00';
+
+ const minutes = Math.floor(seconds / 60);
+ const remainingSeconds = Math.floor(seconds % 60);
+
+ return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
+ }
+
+ // 开始定期保存播放进度
+ function startProgressSaveInterval() {
+ // 清除可能存在的旧计时器
+ if (progressSaveInterval) {
+ clearInterval(progressSaveInterval);
+ }
+
+ // 每30秒保存一次播放进度
+ progressSaveInterval = setInterval(saveCurrentProgress, 30000);
+ }
+
+ // 保存当前播放进度
+ function saveCurrentProgress() {
+ if (!dp || !dp.video || dp.video.paused || videoHasEnded) return;
+
+ // 如果播放进度太短或接近结尾,不保存
+ const currentTime = dp.video.currentTime;
+ const duration = dp.video.duration;
+
+ if (currentTime < 10 || currentTime > duration * 0.95) return;
+
+ // 在localStorage中保存进度
+ const progressKey = `videoProgress_${getVideoId()}`;
+ const progressData = {
+ position: currentTime,
+ duration: duration,
+ timestamp: Date.now()
+ };
+
+ try {
+ localStorage.setItem(progressKey, JSON.stringify(progressData));
+ console.log(`已保存播放进度: ${formatTime(currentTime)} / ${formatTime(duration)}`);
+ } catch (e) {
+ console.error('保存播放进度失败', e);
+ }
+ }
+
+ // 清除视频进度记录
+ function clearVideoProgress() {
+ const progressKey = `videoProgress_${getVideoId()}`;
+ try {
+ localStorage.removeItem(progressKey);
+ console.log('已清除播放进度记录');
+ } catch (e) {
+ console.error('清除播放进度记录失败', e);
+ }
+ }
+
+ // 获取视频唯一标识
+ function getVideoId() {
+ // 使用视频标题和集数索引作为唯一标识
+ return `${encodeURIComponent(currentVideoTitle)}_${currentEpisodeIndex}`;
+ }
</script>
</body>
</html>