一分钟搭建影视站,支持Docker等部署方式
2
developera2cfb459bf02
diff --git a/js/douban.js b/js/douban.jsindex 3592d74..2f897da 100644--- a/js/douban.js+++ b/js/douban.js@@ -175,78 +175,126 @@ function renderRecommend(tag, pageLimit, pageStart) {`;const target = `https://movie.douban.com/j/search_subjects?type=movie&tag=${tag}&sort=recommend&page_limit=${pageLimit}&page_start=${pageStart}`;++ // 添加超时控制+ const controller = new AbortController();+ const timeoutId = setTimeout(() => controller.abort(), 10000); // 10秒超时++ // 设置请求选项,包括信号和头部+ const fetchOptions = {+ signal: controller.signal,+ headers: {+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',+ 'Referer': 'https://movie.douban.com/',+ 'Accept': 'application/json, text/plain, */*',+ }+ };- fetch(PROXY_URL + encodeURIComponent(target))- .then(response => response.json())+ // 尝试直接访问(豆瓣API可能允许部分CORS请求)+ fetch(PROXY_URL + encodeURIComponent(target), fetchOptions)+ .then(response => {+ clearTimeout(timeoutId);+ if (!response.ok) {+ throw new Error(`HTTP error! Status: ${response.status}`);+ }+ return response.json();+ }).then(data => {- // 创建文档片段以提高性能- const fragment = document.createDocumentFragment();+ renderDoubanCards(data, container);+ })+ .catch(err => {+ console.error("豆瓣 API 请求失败(直接代理):", err);- // 如果没有数据- if (!data.subjects || data.subjects.length === 0) {- const emptyEl = document.createElement("div");- emptyEl.className = "col-span-full text-center py-8";- emptyEl.innerHTML = `- <div class="text-pink-500">❌ 暂无数据,请尝试其他分类或刷新</div>- `;- fragment.appendChild(emptyEl);- } else {- // 循环创建每个影视卡片- data.subjects.forEach(item => {- const card = document.createElement("div");- card.className = "bg-[#111] hover:bg-[#222] transition-all duration-300 rounded-lg overflow-hidden flex flex-col transform hover:scale-105 shadow-md hover:shadow-lg";-- // 生成卡片内容,确保安全显示(防止XSS)- const safeTitle = item.title- .replace(/</g, '<')- .replace(/>/g, '>')- .replace(/"/g, '"');-- const safeRate = (item.rate || "暂无")- .replace(/</g, '<')- .replace(/>/g, '>');-- // 修复: 处理豆瓣图片加载的几种方案- // 1. 确保图片URL是安全的- const originalCoverUrl = item.cover;- // 2. 正确使用代理URL (确保使用encodeURIComponent编码完整URL)- const proxiedCoverUrl = PROXY_URL + encodeURIComponent(originalCoverUrl);-- card.innerHTML = `- <div class="relative w-full aspect-[2/3] overflow-hidden cursor-pointer" onclick="fillSearchInput('${safeTitle}')">- <img src="${proxiedCoverUrl}" alt="${safeTitle}"- class="w-full h-full object-cover transition-transform duration-500 hover:scale-110"- onerror="this.onerror=null; this.src='${originalCoverUrl}'; this.classList.add('object-contain');"- loading="lazy" referrerpolicy="no-referrer">- <div class="absolute inset-0 bg-gradient-to-t from-black to-transparent opacity-60"></div>- <div class="absolute bottom-2 left-2 bg-black/70 text-white text-xs px-2 py-1 rounded-sm">- <span class="text-yellow-400">★</span> ${safeRate}- </div>- </div>- <div class="p-2 text-center bg-[#111]">- <button onclick="fillSearchInput('${safeTitle}')"- class="text-sm font-medium text-white truncate w-full hover:text-pink-400 transition">- ${safeTitle}- </button>+ // 失败后尝试备用方法:使用通用JSON API作为备选+ const fallbackUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(target)}`;++ fetch(fallbackUrl)+ .then(response => {+ if (!response.ok) throw new Error(`备用API请求失败! 状态: ${response.status}`);+ return response.json();+ })+ .then(data => {+ // 解析原始内容+ if (data && data.contents) {+ const doubanData = JSON.parse(data.contents);+ renderDoubanCards(doubanData, container);+ } else {+ throw new Error("无法获取有效数据");+ }+ })+ .catch(fallbackErr => {+ console.error("豆瓣 API 备用请求也失败:", fallbackErr);+ container.innerHTML = `+ <div class="col-span-full text-center py-8">+ <div class="text-red-400">❌ 获取豆瓣数据失败,请稍后重试</div>+ <div class="text-gray-500 text-sm mt-2">提示:使用VPN可能有助于解决此问题</div></div>`;-- fragment.appendChild(card);});- }+ });+}++// 抽取渲染豆瓣卡片的逻辑到单独函数+function renderDoubanCards(data, container) {+ // 创建文档片段以提高性能+ const fragment = document.createDocumentFragment();++ // 如果没有数据+ if (!data.subjects || data.subjects.length === 0) {+ const emptyEl = document.createElement("div");+ emptyEl.className = "col-span-full text-center py-8";+ emptyEl.innerHTML = `+ <div class="text-pink-500">❌ 暂无数据,请尝试其他分类或刷新</div>+ `;+ fragment.appendChild(emptyEl);+ } else {+ // 循环创建每个影视卡片+ data.subjects.forEach(item => {+ const card = document.createElement("div");+ card.className = "bg-[#111] hover:bg-[#222] transition-all duration-300 rounded-lg overflow-hidden flex flex-col transform hover:scale-105 shadow-md hover:shadow-lg";- // 清空并添加所有新元素- container.innerHTML = "";- container.appendChild(fragment);- })- .catch(err => {- container.innerHTML = `- <div class="col-span-full text-center py-8">- <div class="text-red-400">❌ 获取豆瓣数据失败,请稍后重试</div>+ // 生成卡片内容,确保安全显示(防止XSS)+ const safeTitle = item.title+ .replace(/</g, '<')+ .replace(/>/g, '>')+ .replace(/"/g, '"');++ const safeRate = (item.rate || "暂无")+ .replace(/</g, '<')+ .replace(/>/g, '>');++ // 处理图片URL+ // 1. 直接使用豆瓣图片URL (添加no-referrer属性)+ const originalCoverUrl = item.cover;+ // 2. 也准备代理URL作为备选+ const proxiedCoverUrl = PROXY_URL + encodeURIComponent(originalCoverUrl);++ card.innerHTML = `+ <div class="relative w-full aspect-[2/3] overflow-hidden cursor-pointer" onclick="fillSearchInput('${safeTitle}')">+ <img src="${originalCoverUrl}" alt="${safeTitle}"+ class="w-full h-full object-cover transition-transform duration-500 hover:scale-110"+ onerror="this.onerror=null; this.src='${proxiedCoverUrl}'; this.classList.add('object-contain');"+ loading="lazy" referrerpolicy="no-referrer">+ <div class="absolute inset-0 bg-gradient-to-t from-black to-transparent opacity-60"></div>+ <div class="absolute bottom-2 left-2 bg-black/70 text-white text-xs px-2 py-1 rounded-sm">+ <span class="text-yellow-400">★</span> ${safeRate}+ </div>+ </div>+ <div class="p-2 text-center bg-[#111]">+ <button onclick="fillSearchInput('${safeTitle}')"+ class="text-sm font-medium text-white truncate w-full hover:text-pink-400 transition">+ ${safeTitle}+ </button></div>`;- console.error("豆瓣 API 请求失败:", err);++ fragment.appendChild(card);});+ }++ // 清空并添加所有新元素+ container.innerHTML = "";+ container.appendChild(fragment);}// 重置到首页