// ==UserScript== // @license MIT // @name 5266ys TMDB 助手 // @namespace http://tampermonkey.net/ // @version 2.9 // @description 适配多站,唯一匹配点击复制,多个结果或未搜到时点击跳转 TMDB 搜索页 // @author Gemini // @match https://www.5266ys.com/*/*/*.htm* // @match https://web5.mukaku.com/mv/* // @grant GM_xmlhttpRequest // @grant GM_setClipboard // @grant GM_openInTab // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @connect api.tmdb.org // @run-at document-end // @downloadURL https://update.greasyfork.icu/scripts/573986/5266ys%20TMDB%20%E5%8A%A9%E6%89%8B.user.js // @updateURL https://update.greasyfork.icu/scripts/573986/5266ys%20TMDB%20%E5%8A%A9%E6%89%8B.meta.js // ==/UserScript== (function() { 'use strict'; if (window.tmdb_helper_initialized) return; window.tmdb_helper_initialized = true; GM_registerMenuCommand("⚙️ 设置 TMDB API Key", setApiKey); function setApiKey() { const oldKey = GM_getValue("tmdb_api_key", ""); const newKey = prompt("请输入 TMDB API Key (v3):", oldKey); if (newKey !== null) { GM_setValue("tmdb_api_key", newKey.trim()); location.reload(); } } function checkAndInit() { if (document.getElementById('tmdb-helper-btn')) return; const target = document.querySelector('.info') || document.getElementById('text') || document.querySelector('.stui-content__detail'); if (target) { runLogic(target); } else { if (performance.now() < 10000) setTimeout(checkAndInit, 500); } } async function runLogic(container) { if (document.getElementById('tmdb-helper-btn')) return; const mainBtn = document.createElement('button'); mainBtn.id = 'tmdb-helper-btn'; mainBtn.innerText = "🔍 正在解析页面信息..."; mainBtn.style.cssText = "margin:10px 0; padding:12px; color:#fff; border:none; border-radius:8px; cursor:default; font-size:14px; width:100%; text-align:center; font-weight:bold; display:block; width:100%; background:#2196F3; opacity:0.7; z-index:999; position:relative;"; container.prepend(mainBtn); const apiKey = GM_getValue("tmdb_api_key", ""); if (!apiKey) { updateBtn(mainBtn, "⚠️ 请点击此处设置 TMDB API Key", "#f44336", setApiKey); return; } let imdbId = null; const imdbLink = document.querySelector('a[href*="imdb.com/title/tt"]'); if (imdbLink) { const match = imdbLink.href.match(/tt\d{7,10}/); if (match) imdbId = match[0]; } let movieTitle = ""; let movieYear = ""; const h1 = container.querySelector('h1'); if (h1) { const yearEl = h1.querySelector('.year'); if (yearEl) movieYear = yearEl.innerText.replace(/[()]/g, '').trim(); movieTitle = h1.firstChild?.innerText || h1.innerText.replace(/\(\d{4}\)/, '').trim(); } if (!movieTitle) { const titleMatch = container.innerText.match(/◎片\s*名[::]?\s*([^\n\r]+)/); if (titleMatch) movieTitle = titleMatch[1].trim(); } if (!movieYear) { const yearMatch = container.innerText.match(/◎年\s*代[::]?\s*(\d{4})/); if (yearMatch) movieYear = yearMatch[1].trim(); } // 搜索关键词清洗:去掉季/部等后缀,只取核心片名 const searchTitle = movieTitle.replace(/第[一二三四五六七八九十\d]+[季部]/g, '').split(' ')[0].trim(); if (!searchTitle && !imdbId) { mainBtn.remove(); return; } let results = []; if (imdbId) { results = await requestTMDB(`https://api.tmdb.org/3/find/${imdbId}?api_key=${apiKey}&language=zh-CN&external_source=imdb_id`, true); } if ((!results || results.length === 0) && searchTitle) { let rawResults = await requestTMDB(`https://api.tmdb.org/3/search/multi?api_key=${apiKey}&query=${encodeURIComponent(searchTitle)}&language=zh-CN`, false); if (rawResults && rawResults.length > 0) { if (movieYear) { const filtered = rawResults.filter(item => (item.release_date || item.first_air_date || "").startsWith(movieYear)); results = filtered.length > 0 ? filtered : rawResults; } else { results = rawResults; } } } // --- 4. 渲染结果逻辑 --- if (results.length === 1) { // 🎯 情况 A: 唯一匹配成功,点击复制 const res = results[0]; const name = res.title || res.name; const year = (res.release_date || res.first_air_date || "0000").split('-')[0]; const copyText = `${name} (${year}) {tmdbid-${res.id}}`; updateBtn(mainBtn, `🎯 匹配成功:${copyText}`, "#4caf50", () => { GM_setClipboard(copyText); const originalText = mainBtn.innerHTML; mainBtn.innerText = "✅ 已复制到剪贴板"; setTimeout(() => { mainBtn.innerHTML = originalText; }, 2000); }); } else if (results.length > 1) { // ⚠️ 情况 B: 匹配到多个结果,点击跳转搜索页面 updateBtn(mainBtn, `⚠️ 存在 ${results.length} 个同名项,请点击手动确认并查看 ID`, "#ff9800", () => { GM_openInTab(`https://www.themoviedb.org/search?query=${encodeURIComponent(searchTitle)}`); }); } else { // ❌ 情况 C: 未搜到,点击跳转手动搜索 updateBtn(mainBtn, `❌ 未搜到 "${searchTitle}",点击前往官网搜索`, "#607d8b", () => { GM_openInTab(`https://www.themoviedb.org/search?query=${encodeURIComponent(searchTitle)}`); }); } } function updateBtn(btn, text, color, clickFn) { btn.innerHTML = text; btn.style.background = color; btn.style.cursor = "pointer"; btn.style.opacity = "1"; btn.onclick = clickFn; } function requestTMDB(url, isFindApi) { return new Promise((resolve) => { GM_xmlhttpRequest({ method: "GET", url, timeout: 8000, headers: { "Accept": "application/json" }, onload: (res) => { if (res.status === 200) { try { const data = JSON.parse(res.responseText); resolve(isFindApi ? [...(data.movie_results || []), ...(data.tv_results || [])] : data.results || []); } catch(e) { resolve([]); } } else resolve([]); }, onerror: () => resolve([]), }); }); } checkAndInit(); })();