File size: 14,113 Bytes
7eff83b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
// 豆瓣热门电影电视剧推荐功能
// 豆瓣标签列表
const doubanTags = ['热门', '最新', '经典', '豆瓣高分','古装剧', '冷门佳片', '科幻' ,'喜剧', '综艺', '欧美', '电视剧', '韩国', '日本', '动漫','动画片'];
let doubanCurrentTag = localStorage.getItem('doubanCurrentTag') || '热门';
let doubanPageStart = 0;
const doubanPageSize = 16; // 一次显示的项目数量
// 初始化豆瓣功能
function initDouban() {
// 设置豆瓣开关的初始状态
const doubanToggle = document.getElementById('doubanToggle');
if (doubanToggle) {
const isEnabled = localStorage.getItem('doubanEnabled') === 'true';
doubanToggle.checked = isEnabled;
// 设置开关外观
const toggleBg = doubanToggle.nextElementSibling;
const toggleDot = toggleBg.nextElementSibling;
if (isEnabled) {
toggleBg.classList.add('bg-pink-600');
toggleDot.classList.add('translate-x-6');
}
// 添加事件监听
doubanToggle.addEventListener('change', function(e) {
const isChecked = e.target.checked;
localStorage.setItem('doubanEnabled', isChecked);
// 更新开关外观
if (isChecked) {
toggleBg.classList.add('bg-pink-600');
toggleDot.classList.add('translate-x-6');
} else {
toggleBg.classList.remove('bg-pink-600');
toggleDot.classList.remove('translate-x-6');
}
// 更新显示状态
updateDoubanVisibility();
});
// 初始更新显示状态
updateDoubanVisibility();
}
// 渲染豆瓣标签
renderDoubanTags();
// 换一批按钮事件监听
setupDoubanRefreshBtn();
// 初始加载热门内容
if (localStorage.getItem('doubanEnabled') === 'true') {
renderRecommend(doubanCurrentTag, doubanPageSize, doubanPageStart);
}
}
// 根据设置更新豆瓣区域的显示状态
function updateDoubanVisibility() {
const doubanArea = document.getElementById('doubanArea');
if (!doubanArea) return;
const isEnabled = localStorage.getItem('doubanEnabled') === 'true';
const isSearching = document.getElementById('resultsArea') &&
!document.getElementById('resultsArea').classList.contains('hidden');
// 只有在启用且没有搜索结果显示时才显示豆瓣区域
if (isEnabled && !isSearching) {
doubanArea.classList.remove('hidden');
// 如果豆瓣结果为空,重新加载
if (document.getElementById('douban-results').children.length === 0) {
renderRecommend(doubanCurrentTag, doubanPageSize, doubanPageStart);
}
} else {
doubanArea.classList.add('hidden');
}
}
// 只填充搜索框,不执行搜索,让用户自主决定搜索时机
function fillSearchInput(title) {
if (!title) return;
// 安全处理标题,防止XSS
const safeTitle = title
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
const input = document.getElementById('searchInput');
if (input) {
input.value = safeTitle;
// 聚焦搜索框,便于用户立即使用键盘操作
input.focus();
// 显示一个提示,告知用户点击搜索按钮进行搜索
showToast('已填充搜索内容,点击搜索按钮开始搜索', 'info');
}
}
// 填充搜索框并执行搜索
function fillAndSearch(title) {
if (!title) return;
// 安全处理标题,防止XSS
const safeTitle = title
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
const input = document.getElementById('searchInput');
if (input) {
input.value = safeTitle;
search(); // 使用已有的search函数执行搜索
}
}
// 填充搜索框,确保豆瓣资源API被选中,然后执行搜索
function fillAndSearchWithDouban(title) {
if (!title) return;
// 安全处理标题,防止XSS
const safeTitle = title
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
// 确保豆瓣资源API被选中
if (typeof selectedAPIs !== 'undefined' && !selectedAPIs.includes('dbzy')) {
// 在设置中勾选豆瓣资源API复选框
const doubanCheckbox = document.querySelector('input[id="api_dbzy"]');
if (doubanCheckbox) {
doubanCheckbox.checked = true;
// 触发updateSelectedAPIs函数以更新状态
if (typeof updateSelectedAPIs === 'function') {
updateSelectedAPIs();
} else {
// 如果函数不可用,则手动添加到selectedAPIs
selectedAPIs.push('dbzy');
localStorage.setItem('selectedAPIs', JSON.stringify(selectedAPIs));
// 更新选中API计数(如果有这个元素)
const countEl = document.getElementById('selectedAPICount');
if (countEl) {
countEl.textContent = selectedAPIs.length;
}
}
showToast('已自动选择豆瓣资源API', 'info');
}
}
// 填充搜索框并执行搜索
const input = document.getElementById('searchInput');
if (input) {
input.value = safeTitle;
search(); // 使用已有的search函数执行搜索
}
}
// 渲染豆瓣标签选择器
function renderDoubanTags() {
const tagContainer = document.getElementById('douban-tags');
if (!tagContainer) return;
tagContainer.innerHTML = '';
doubanTags.forEach(tag => {
const btn = document.createElement('button');
// 更新标签样式:统一高度,添加过渡效果,改进颜色对比度
btn.className = 'py-1.5 px-3.5 rounded text-sm font-medium transition-all duration-300 ' +
(tag === doubanCurrentTag ?
'bg-pink-600 text-white shadow-md' :
'bg-[#1a1a1a] text-gray-300 hover:bg-pink-700 hover:text-white');
btn.textContent = tag;
btn.onclick = function() {
if (doubanCurrentTag !== tag) {
doubanCurrentTag = tag;
localStorage.setItem('doubanCurrentTag', tag);
doubanPageStart = 0;
renderRecommend(doubanCurrentTag, doubanPageSize, doubanPageStart);
renderDoubanTags();
}
};
tagContainer.appendChild(btn);
});
}
// 设置换一批按钮事件
function setupDoubanRefreshBtn() {
// 修复ID,使用正确的ID douban-refresh 而不是 douban-refresh-btn
const btn = document.getElementById('douban-refresh');
if (!btn) return;
btn.onclick = function() {
doubanPageStart += doubanPageSize;
if (doubanPageStart > 9 * doubanPageSize) {
doubanPageStart = 0;
}
renderRecommend(doubanCurrentTag, doubanPageSize, doubanPageStart);
};
}
// 渲染热门推荐内容
function renderRecommend(tag, pageLimit, pageStart) {
const container = document.getElementById("douban-results");
if (!container) return;
// 显示加载状态
container.innerHTML = `
<div class="col-span-full text-center py-10">
<div class="w-6 h-6 border-2 border-pink-500 border-t-transparent rounded-full animate-spin mr-2 inline-block"></div>
<span class="text-pink-500">加载中...</span>
</div>
`;
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, */*',
}
};
// 尝试直接访问(豆瓣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 => {
renderDoubanCards(data, container);
})
.catch(err => {
console.error("豆瓣 API 请求失败(直接代理):", err);
// 失败后尝试备用方法:作为备选
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>
`;
});
});
}
// 抽取渲染豆瓣卡片的逻辑到单独函数
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";
// 生成卡片内容,确保安全显示(防止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="fillAndSearchWithDouban('${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 class="absolute bottom-2 right-2 bg-black/70 text-white text-xs px-2 py-1 rounded-sm hover:bg-[#333] transition-colors">
<a href="${item.url}" target="_blank" rel="noopener noreferrer" title="在豆瓣查看">
🔗
</a>
</div>
</div>
<div class="p-2 text-center bg-[#111]">
<button onclick="fillAndSearchWithDouban('${safeTitle}')"
class="text-sm font-medium text-white truncate w-full hover:text-pink-400 transition"
title="${safeTitle}">
${safeTitle}
</button>
</div>
`;
fragment.appendChild(card);
});
}
// 清空并添加所有新元素
container.innerHTML = "";
container.appendChild(fragment);
}
// 重置到首页
function resetToHome() {
resetSearchArea();
updateDoubanVisibility();
}
// 加载豆瓣首页内容
document.addEventListener('DOMContentLoaded', initDouban); |