Skip to content

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

fix: validate episode index and add toast notifications for errors

developeree9bd9133b40
diff --git a/player.html b/player.html
index 2a47cc9..bb47165 100644
--- a/player.html
+++ b/player.html
@@ -254,7 +254,7 @@
const urlParams = new URLSearchParams(window.location.search);
const videoUrl = urlParams.get('url');
const title = urlParams.get('title');
- const index = parseInt(urlParams.get('index') || '0');
+ let index = parseInt(urlParams.get('index') || '0');
const episodesList = urlParams.get('episodes'); // 新增:从URL获取集数信息
// 从localStorage获取数据
@@ -285,10 +285,32 @@
currentEpisodes = JSON.parse(localStorage.getItem('currentEpisodes') || '[]');
console.log('从localStorage恢复集数信息:', currentEpisodes.length);
}
+
+ // 检查集数索引是否有效,如果无效则调整为0
+ if (index < 0 || (currentEpisodes.length > 0 && index >= currentEpisodes.length)) {
+ console.warn(`无效的剧集索引 ${index},调整为范围内的值`);
+
+ // 如果索引太大,则使用最大有效索引
+ if (index >= currentEpisodes.length && currentEpisodes.length > 0) {
+ index = currentEpisodes.length - 1;
+ } else {
+ index = 0;
+ }
+
+ // 更新URL以反映修正后的索引
+ const newUrl = new URL(window.location.href);
+ newUrl.searchParams.set('index', index);
+ window.history.replaceState({}, '', newUrl);
+ }
+
+ // 更新当前索引为验证过的值
+ currentEpisodeIndex = index;
+
episodesReversed = localStorage.getItem('episodesReversed') === 'true';
} catch (e) {
console.error('获取集数信息失败:', e);
currentEpisodes = [];
+ currentEpisodeIndex = 0;
episodesReversed = false;
}
@@ -857,7 +879,12 @@
// 播放指定集数
function playEpisode(index) {
- if (index < 0 || index >= currentEpisodes.length) return;
+ // 确保index在有效范围内
+ if (index < 0 || index >= currentEpisodes.length) {
+ console.error(`无效的剧集索引: ${index}, 当前剧集数量: ${currentEpisodes.length}`);
+ showToast(`无效的剧集索引: ${index + 1},当前剧集总数: ${currentEpisodes.length}`);
+ return;
+ }
// 保存当前播放进度(如果正在播放)
if (dp && dp.video && !dp.video.paused && !videoHasEnded) {
@@ -1235,6 +1262,54 @@
// 使用视频标题和集数索引作为唯一标识
return `${encodeURIComponent(currentVideoTitle)}_${currentEpisodeIndex}`;
}
+
+ // 简单的Toast消息提示函数
+ function showToast(message, type = 'error') {
+ // 如果已有Toast,先移除它
+ const existingToast = document.getElementById('custom-toast');
+ if (existingToast) {
+ document.body.removeChild(existingToast);
+ }
+
+ // 创建新的Toast元素
+ const toast = document.createElement('div');
+ toast.id = 'custom-toast';
+
+ // 设置Toast样式
+ toast.style.position = 'fixed';
+ toast.style.top = '20px';
+ toast.style.left = '50%';
+ toast.style.transform = 'translateX(-50%)';
+ toast.style.backgroundColor = type === 'error' ? '#f44336' : '#4caf50';
+ toast.style.color = 'white';
+ toast.style.padding = '12px 20px';
+ toast.style.borderRadius = '4px';
+ toast.style.zIndex = '10000';
+ toast.style.boxShadow = '0 2px 8px rgba(0, 0, 0, 0.3)';
+ toast.style.opacity = '0';
+ toast.style.transition = 'opacity 0.3s ease-in-out';
+
+ // 设置Toast内容
+ toast.textContent = message;
+
+ // 添加到页面
+ document.body.appendChild(toast);
+
+ // 显示Toast
+ setTimeout(() => {
+ toast.style.opacity = '1';
+
+ // 3秒后隐藏并移除
+ setTimeout(() => {
+ toast.style.opacity = '0';
+ setTimeout(() => {
+ if (toast.parentNode) {
+ document.body.removeChild(toast);
+ }
+ }, 300);
+ }, 3000);
+ }, 10);
+ }
</script>
</body>
</html>