// ==UserScript== // @name AI 视频解析 · 灵析 // @namespace https://workbuddy.cn/userscripts/ai-video-parse // @version 8.5.2 // @description 漂亮的通用视频解析面板:粘贴视频页/直链,多线路解析,ArtPlayer 播放,AI 智能解读,历史记录。 // @author WorkBuddy // @match *://*/* // @exclude *://*.google.com/* // @connect * // @grant GM_xmlhttpRequest // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand // @grant GM_openInTab // @run-at document-idle // @noframes // @license MIT // ==/UserScript== (function () { 'use strict'; /* ------------------------------------------------------------------ * * AI 视频解析 · 灵析 * 单文件油猴脚本。Shadow DOM 隔离样式;解析线路 / AI 接口均可配置。 * ------------------------------------------------------------------ */ const SCRIPT_TAG = 'aivp'; const STORE_LINES = SCRIPT_TAG + '_lines_v1'; const STORE_HIST = SCRIPT_TAG + '_history_v1'; const STORE_AI = SCRIPT_TAG + '_ai_v1'; const STORE_THEME = SCRIPT_TAG + '_theme_v1'; const MAX_HISTORY = 60; const DEFAULT_LINES = [ { name: '极速线路', url: 'https://im1907.top/?jx=', on: true, encode: false }, { name: '高清线路', url: 'https://jx.m3u8.tv/jiexi/?url=', on: true, encode: false }, { name: '备用线路', url: 'https://jx.xmflv.com/?url=', on: true, encode: false }, { name: 'JSON 接口', url: '', on: false, encode: true }, { name: 'im1907(默认)', url: 'https://www.pangujiexi.com/jiexi/?url=', encode: false }, { name: '推荐线路 xymp4', url: 'https://www.8090g.cn/?url=', encode: true }, { name: '线路1 2s0', url: 'https://www.ckplayer.vip/jiexi/?url=', encode: true }, { name: '线路2 xmflv', url: 'https://yparse.ik9.cc/index.php?url=', encode: true }, { name: '线路3 789jiexi', url: 'https://jx.789jiexi.com/?url=', encode: true }, { name: '线路4 hls.one', url: 'https://jx.202617.xyz/tv.php?url=', encode: true }, { name: '线路5 txnp', url: 'https://im1907.top/?jx=', encode: true }, { name: '线路6 fongmi', url: 'https://jx.789jiexi.net:4433/?url=', encode: true } ]; const DEFAULT_AI = { endpoint: 'https://api.openai.com/v1/chat/completions', model: 'gpt-4o-mini', key: '', }; const CSS = ` :host { all: initial; --bg: #f4f6fc; --panel: #ffffff; --panel-2: #f8faff; --text: #1e2430; --muted: #7a8499; --line: #e5e9f3; --accent: #6d5efc; --accent-2: #21c7d8; --chip: #f0f2fa; --shadow: 0 30px 80px rgba(26, 32, 54, .22); } :host([data-theme="dark"]) { --bg: #10131d; --panel: #171b28; --panel-2: #1d2232; --text: #eef1f8; --muted: #99a2b6; --line: #2b3244; --chip: #222a3b; --shadow: 0 30px 80px rgba(0, 0, 0, .55); } * { box-sizing: border-box; margin: 0; padding: 0; font-family: "PingFang SC", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; } button, input, textarea, select { font: inherit; } button { cursor: pointer; } .fab { position: fixed; right: 22px; bottom: 26px; z-index: 2147483600; width: 56px; height: 56px; border-radius: 18px; border: 0; background: linear-gradient(135deg, #6d5efc, #21c7d8); color: #fff; display: grid; place-items: center; box-shadow: 0 12px 28px rgba(109, 94, 252, .45); transition: transform .22s, box-shadow .22s; } .fab:hover { transform: translateY(-3px) rotate(-4deg); box-shadow: 0 16px 34px rgba(109, 94, 252, .55); } .fab svg { width: 26px; height: 26px; } .fab::after { content: ""; position: absolute; inset: -6px; border-radius: 24px; border: 1px solid rgba(109, 94, 252, .45); animation: pulse 2.6s ease-in-out infinite; } @keyframes pulse { 0%,100% { opacity: .7; transform: scale(1); } 50% { opacity: 0; transform: scale(1.14); } } .overlay { position: fixed; inset: 0; z-index: 2147483601; display: none; align-items: center; justify-content: center; padding: 24px; background: rgba(14, 18, 32, .46); backdrop-filter: blur(10px); } .overlay.show { display: flex; animation: fade .22s ease; } @keyframes fade { from { opacity: 0; } to { opacity: 1; } } .panel { width: min(940px, 100%); height: min(700px, calc(100vh - 48px)); background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 26px; box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; animation: rise .28s cubic-bezier(.2,.9,.3,1); } @keyframes rise { from { transform: translateY(18px) scale(.98); opacity: 0; } to { transform: none; opacity: 1; } } .head { display: flex; align-items: center; gap: 16px; padding: 16px 22px; border-bottom: 1px solid var(--line); background: var(--panel); flex: none; } .brand { display: flex; align-items: center; gap: 11px; min-width: 190px; } .brand .logo { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; background: linear-gradient(135deg, #6d5efc, #21c7d8); color: #fff; flex: none; box-shadow: 0 6px 16px rgba(109, 94, 252, .32); } .brand .logo svg { width: 21px; height: 21px; } .brand b { display: block; font-size: 15px; letter-spacing: .3px; } .brand small { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; } .tabs { display: flex; gap: 4px; flex: 1; justify-content: center; } .tab { height: 34px; padding: 0 15px; border-radius: 11px; border: 0; background: transparent; color: var(--muted); font-size: 13px; transition: .18s; } .tab:hover { background: var(--chip); color: var(--text); } .tab.on { background: var(--chip); color: var(--accent); font-weight: 700; } .head-actions { display: flex; gap: 6px; } .icon-btn { width: 34px; height: 34px; border-radius: 11px; border: 0; background: transparent; color: var(--muted); font-size: 16px; display: grid; place-items: center; transition: .18s; } .icon-btn:hover { background: var(--chip); color: var(--text); } .body { flex: 1; overflow: auto; padding: 20px 22px 26px; background: var(--bg); } .body::-webkit-scrollbar { width: 8px; } .body::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; } .hero { position: relative; overflow: hidden; border-radius: 22px; padding: 26px 28px; color: #fff; background: linear-gradient(135deg, #6d5efc 0%, #8b5cf6 46%, #21c7d8 100%); box-shadow: 0 16px 34px rgba(109, 94, 252, .26); } .hero::before, .hero::after { content: ""; position: absolute; border-radius: 50%; background: #fff; opacity: .13; } .hero::before { width: 230px; height: 230px; right: -70px; top: -100px; } .hero::after { width: 170px; height: 170px; right: 96px; bottom: -120px; } .hero > * { position: relative; z-index: 1; } .badge { display: inline-flex; align-items: center; gap: 6px; height: 24px; padding: 0 10px; border-radius: 12px; background: rgba(255,255,255,.2); border: 1px solid rgba(255,255,255,.3); font-size: 10px; letter-spacing: 1.4px; font-weight: 700; } .hero h1 { font-size: 26px; margin: 12px 0 6px; letter-spacing: .5px; } .hero p { font-size: 12px; opacity: .88; line-height: 1.7; } .input-row { display: flex; gap: 10px; margin-top: 18px; } .input-row input { flex: 1; min-width: 0; height: 46px; border-radius: 13px; border: 1px solid rgba(255,255,255,.55); background: rgba(255,255,255,.94); color: #1e2430; padding: 0 16px; font-size: 13px; outline: none; } .input-row input:focus { box-shadow: 0 0 0 3px rgba(255,255,255,.32); } .btn { height: 46px; padding: 0 20px; border-radius: 13px; border: 0; font-size: 13px; font-weight: 600; background: var(--chip); color: var(--text); white-space: nowrap; transition: .18s; } .btn:hover { filter: brightness(1.04); transform: translateY(-1px); } .btn.primary { background: linear-gradient(135deg, #6d5efc, #21c7d8); color: #fff; } .hero .btn.primary { background: #fff; color: #5b4ee6; } .hero .btn.ghost { background: rgba(255,255,255,.16); color: #fff; border: 1px solid rgba(255,255,255,.38); } .section { margin-top: 18px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 18px; padding: 17px 20px; } .section-title { display: flex; align-items: center; gap: 9px; font-size: 13px; font-weight: 700; margin-bottom: 13px; } .section-title .num { width: 20px; height: 20px; border-radius: 7px; display: grid; place-items: center; background: linear-gradient(135deg, rgba(109,94,252,.16), rgba(33,199,216,.16)); color: var(--accent); font-size: 11px; } .section-title .hint { font-weight: 400; font-size: 11px; color: var(--muted); margin-left: auto; } .lines { display: flex; flex-wrap: wrap; gap: 8px; } .line-chip { height: 32px; padding: 0 14px; border-radius: 16px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font-size: 12px; display: flex; align-items: center; gap: 7px; transition: .18s; } .line-chip:hover { border-color: var(--accent); transform: translateY(-1px); } .line-chip.on { background: linear-gradient(135deg, rgba(109,94,252,.14), rgba(33,199,216,.14)); border-color: var(--accent); color: var(--accent); font-weight: 700; } .line-chip i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent-2); display: block; } .result { margin-top: 18px; display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(230px, 1fr); gap: 18px; } .stage { background: #0b0e16; border-radius: 18px; overflow: hidden; min-height: 300px; display: flex; flex-direction: column; box-shadow: inset 0 0 0 1px rgba(255,255,255,.05); } .frame-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 13px; background: var(--panel-2); color: var(--muted); font-size: 11px; border-bottom: 1px solid var(--line); } .stage iframe { width: 100%; height: 400px; border: 0; background: #fff; } .video-box { width: 100%; aspect-ratio: 16 / 9; background: #000; } .video-box video { width: 100%; height: 100%; display: block; background: #000; } .info-card { background: var(--panel-2); border: 1px solid var(--line); border-radius: 18px; padding: 18px; display: flex; flex-direction: column; gap: 11px; } .info-kicker { font-size: 10px; letter-spacing: 1.3px; color: var(--accent); font-weight: 700; } .info-title { font-size: 15px; font-weight: 700; word-break: break-all; line-height: 1.5; } .info-url { font-size: 11px; color: var(--muted); word-break: break-all; line-height: 1.65; } .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: auto; } .mini-btn { height: 32px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font-size: 12px; transition: .18s; } .mini-btn:hover { border-color: var(--accent); color: var(--accent); } .mini-btn.hi { background: linear-gradient(135deg, #6d5efc, #21c7d8); border-color: transparent; color: #fff; } .mini-btn.danger:hover { border-color: #ef5b6b; color: #ef5b6b; } .loading { padding: 64px 20px; text-align: center; color: var(--muted); font-size: 13px; } .spinner { width: 34px; height: 34px; margin: 0 auto 14px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; } @keyframes spin { to { transform: rotate(360deg); } } .empty { text-align: center; padding: 56px 20px; color: var(--muted); font-size: 13px; line-height: 1.8; } .empty .big { font-size: 32px; margin-bottom: 10px; opacity: .55; } .list { display: flex; flex-direction: column; gap: 10px; } .item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-radius: 14px; background: var(--panel-2); border: 1px solid var(--line); transition: .18s; } .item:hover { border-color: var(--accent); } .item .grow { flex: 1; min-width: 0; } .item .t { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .item .m { font-size: 11px; color: var(--muted); margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .tag { height: 20px; padding: 0 9px; border-radius: 10px; background: var(--chip); color: var(--accent); font-size: 10px; display: inline-grid; place-items: center; flex: none; } .row { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; } .row input, .row select { height: 36px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); color: var(--text); padding: 0 10px; outline: none; font-size: 12px; } .row input:focus { border-color: var(--accent); } .row input.nm { width: 128px; flex: none; } .row input.url { flex: 1; min-width: 0; } .toggle { height: 32px; padding: 0 11px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--muted); font-size: 11px; flex: none; } .toggle.on { border-color: var(--accent); color: var(--accent); background: rgba(109,94,252,.09); } .field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; } .field label { font-size: 11px; color: var(--muted); } .field input { height: 38px; border: 1px solid var(--line); border-radius: 11px; background: var(--panel); color: var(--text); padding: 0 12px; outline: none; font-size: 12px; } .field input:focus { border-color: var(--accent); } textarea { width: 100%; min-height: 88px; resize: vertical; border: 1px solid var(--line); border-radius: 13px; background: var(--panel); color: var(--text); padding: 12px 14px; outline: none; font-size: 13px; line-height: 1.7; } textarea:focus { border-color: var(--accent); } .ai-result { white-space: pre-wrap; line-height: 1.85; font-size: 13px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 14px; padding: 16px; min-height: 130px; margin-top: 14px; } .toast-layer { position: fixed; top: 18px; left: 50%; transform: translateX(-50%); z-index: 2147483605; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; } .toast { padding: 10px 18px; border-radius: 12px; background: rgba(24,28,40,.93); color: #fff; font-size: 13px; box-shadow: 0 10px 30px rgba(0,0,0,.24); animation: toastIn .25s ease; } .toast.out { opacity: 0; transform: translateY(-8px); transition: .25s; } @keyframes toastIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: none; } } @media (max-width: 760px) { .overlay { padding: 0; } .panel { width: 100%; height: 100%; border-radius: 0; } .head { flex-wrap: wrap; gap: 8px; padding: 12px 14px; } .brand { min-width: 0; } .tabs { order: 3; width: 100%; justify-content: space-between; } .tab { flex: 1; padding: 0 6px; } .body { padding: 14px; } .hero { padding: 20px; } .hero h1 { font-size: 21px; } .input-row { flex-wrap: wrap; } .input-row input { flex-basis: 100%; } .result { grid-template-columns: 1fr; } } `; let ui = null; const state = { tab: 'parse', url: '', title: '', lineIdx: 0, player: null, hls: null, aiContext: null, lastResult: null, }; /* ------------------------- 基础工具 ------------------------- */ function el(tag, cls, text) { const n = document.createElement(tag); if (cls) n.className = cls; if (text != null) n.textContent = text; return n; } function load(key, fallback) { let raw = null; try { raw = typeof GM_getValue === 'function' ? GM_getValue(key, null) : localStorage.getItem(key); } catch (e) { /* ignore */ } if (!raw) return JSON.parse(JSON.stringify(fallback)); try { return JSON.parse(raw); } catch (e) { return JSON.parse(JSON.stringify(fallback)); } } function save(key, value) { const raw = JSON.stringify(value); try { if (typeof GM_setValue === 'function') GM_setValue(key, raw); else localStorage.setItem(key, raw); } catch (e) { /* ignore */ } } function getLines() { const list = load(STORE_LINES, DEFAULT_LINES); return Array.isArray(list) ? list : JSON.parse(JSON.stringify(DEFAULT_LINES)); } function gmRequest(url, opt) { opt = opt || {}; return new Promise(function (resolve, reject) { if (typeof GM_xmlhttpRequest === 'function') { GM_xmlhttpRequest({ method: opt.method || 'GET', url: url, headers: opt.headers || {}, data: opt.body, timeout: 30000, onload: function (r) { if (r.status >= 200 && r.status < 400) resolve(r.responseText); else reject(new Error('HTTP ' + r.status)); }, onerror: function () { reject(new Error('网络错误')); }, ontimeout: function () { reject(new Error('请求超时')); }, }); } else { fetch(url, opt).then(function (r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.text(); }).then(resolve, reject); } }); } function toast(msg, ms) { if (!ui) return; const t = el('div', 'toast', msg); ui.$toasts.appendChild(t); setTimeout(function () { t.classList.add('out'); setTimeout(function () { t.remove(); }, 280); }, ms || 2600); } function copyText(text) { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(text).then(function () { toast('已复制到剪贴板'); }, function () { fallbackCopy(text); }); } else fallbackCopy(text); } function fallbackCopy(text) { const ta = document.createElement('textarea'); ta.value = text; ta.style.cssText = 'position:fixed;opacity:0'; document.body.appendChild(ta); ta.select(); try { document.execCommand('copy'); toast('已复制到剪贴板'); } catch (e) { toast('复制失败,请手动选择'); } ta.remove(); } function openTab(url) { try { if (typeof GM_openInTab === 'function') GM_openInTab(url, { active: true }); else window.open(url, '_blank', 'noopener'); } catch (e) { window.open(url, '_blank'); } } function normalizeUrl(raw) { let u = String(raw || '').trim(); if (!u) return ''; if (/^(https?|blob):\/\//i.test(u)) return u; if (/^[a-z][a-z0-9+.-]*:\/\//i.test(u)) return u; return 'https://' + u; } function isDirect(url) { return /\.(mp4|m3u8|webm|ogg|mov|m4v|ts)(\?|#|$)/i.test(url) || /^blob:/i.test(url); } function cleanTitle(t) { return String(t || '').replace(/[_|\-–—]\s*(爱奇艺|腾讯视频|优酷|芒果TV|搜狐视频|哔哩哔哩|bilibili).*$/i, '').trim(); } function guessTitle(url) { try { const host = new URL(url).hostname; if (location.hostname === host && cleanTitle(document.title)) return cleanTitle(document.title); return host; } catch (e) { return url.slice(0, 60); } } function isVideoHost() { return /(^|\.)(v\.qq\.com|iqiyi\.com|youku\.com|mgtv\.com|sohu\.com|bilibili\.com|douyin\.com|kuaishou\.com)$/i.test(location.hostname); } function sniffPage() { const found = []; function push(v) { if (!v) return; v = String(v).trim(); if (/^https?:\/\//i.test(v) && !/^https?:\/\/[^/]+\/?$/i.test(v) && found.indexOf(v) < 0) found.push(v); } document.querySelectorAll('video[src], source[src]').forEach(function (n) { push(n.getAttribute('src')); }); document.querySelectorAll('meta[property="og:video"], meta[property="og:video:url"], meta[property="og:video:secure_url"], meta[name="twitter:player"]').forEach(function (n) { push(n.content); }); document.querySelectorAll('script[type="application/ld+json"]').forEach(function (s) { try { const walk = function (o) { if (!o || typeof o !== 'object') return; if (Array.isArray(o)) { o.forEach(walk); return; } ['contentUrl', 'embedUrl', 'videoUrl'].forEach(function (k) { if (typeof o[k] === 'string') push(o[k]); }); Object.keys(o).forEach(function (k) { walk(o[k]); }); }; walk(JSON.parse(s.textContent)); } catch (e) { /* ignore */ } }); if (isVideoHost()) push(location.href); return found; } /* ------------------------- 播放器 ------------------------- */ let libsPromise = null; function loadScript(src) { return new Promise(function (resolve, reject) { const s = document.createElement('script'); s.src = src; s.onload = resolve; s.onerror = function () { reject(new Error('播放器组件加载失败')); }; document.head.appendChild(s); }); } function loadLibs() { if (libsPromise) return libsPromise; libsPromise = (async function () { if (!window.ArtPlayer) await loadScript('https://cdn.jsdelivr.net/npm/artplayer@5.2.5/dist/artplayer.js'); if (!window.Hls) await loadScript('https://cdn.jsdelivr.net/npm/hls.js@1.5.17/dist/hls.min.js'); return true; })().catch(function (e) { libsPromise = null; throw e; }); return libsPromise; } function destroyPlayer() { try { if (state.player) state.player.destroy(true); } catch (e) { /* ignore */ } state.player = null; try { if (state.hls) state.hls.destroy(); } catch (e) { /* ignore */ } state.hls = null; } async function mountPlayer(container, url, title) { destroyPlayer(); container.innerHTML = ''; const box = el('div', 'video-box'); container.appendChild(box); function native() { box.innerHTML = ''; const v = el('video'); v.controls = true; v.src = url; v.playsInline = true; box.appendChild(v); } try { await loadLibs(); const opt = { container: box, url: url, title: title, theme: '#6d5efc', pip: true, mutex: true, fullscreen: true, setting: true, playbackRate: true, aspectRatio: true, moreVideoAttr: { crossOrigin: 'anonymous' }, }; if (/\.m3u8(\?|#|$)/i.test(url) && window.Hls && window.Hls.isSupported()) { opt.customType = { m3u8: function (video, src, art) { const hls = new window.Hls({ maxBufferLength: 30 }); hls.loadSource(src); hls.attachMedia(video); hls.on(window.Hls.Events.MANIFEST_PARSED, function (e, d) { if (d.levels && d.levels.length > 1) hls.currentLevel = d.levels.length - 1; }); state.hls = hls; art.on('destroy', function () { try { hls.destroy(); } catch (err) { /* ignore */ } }); }, }; } state.player = new window.ArtPlayer(opt); } catch (e) { native(); toast('增强播放器加载失败,已切换原生播放器'); } } /* ------------------------- UI 骨架 ------------------------- */ function ensureUI() { if (ui) return; const host = document.createElement('div'); host.id = SCRIPT_TAG + '-host'; host.setAttribute('data-theme', load(STORE_THEME, 'light') === 'dark' ? 'dark' : 'light'); document.documentElement.appendChild(host); const shadow = host.attachShadow({ mode: 'open' }); shadow.innerHTML = '' + '' + '
' + ''; ui = { host: host, shadow: shadow, fab: shadow.querySelector('.fab'), overlay: shadow.querySelector('.overlay'), panel: shadow.querySelector('.panel'), $body: shadow.querySelector('.body'), $toasts: shadow.querySelector('.toast-layer'), }; ui.fab.addEventListener('click', openPanel); shadow.querySelector('#closeBtn').addEventListener('click', closePanel); ui.overlay.addEventListener('click', function (e) { if (e.target === ui.overlay) closePanel(); }); shadow.querySelector('#themeBtn').addEventListener('click', function () { const next = host.getAttribute('data-theme') === 'dark' ? 'light' : 'dark'; host.setAttribute('data-theme', next); this.textContent = next === 'dark' ? '☀' : '☾'; save(STORE_THEME, next); }); shadow.querySelector('#themeBtn').textContent = host.getAttribute('data-theme') === 'dark' ? '☀' : '☾'; shadow.querySelectorAll('.tab').forEach(function (b) { b.addEventListener('click', function () { switchTab(b.dataset.tab); }); }); document.addEventListener('keydown', function (e) { if (e.altKey && e.code === 'KeyA') { e.preventDefault(); ui.overlay.classList.contains('show') ? closePanel() : openPanel(); } if (e.key === 'Escape' && ui.overlay.classList.contains('show')) closePanel(); }, true); if (typeof GM_registerMenuCommand === 'function') { GM_registerMenuCommand('打开 AI 视频解析', openPanel); } } function openPanel() { ensureUI(); ui.overlay.classList.add('show'); switchTab(state.tab); } function closePanel() { if (!ui) return; destroyPlayer(); ui.overlay.classList.remove('show'); } function switchTab(tab) { ensureUI(); state.tab = tab; ui.shadow.querySelectorAll('.tab').forEach(function (b) { b.classList.toggle('on', b.dataset.tab === tab); }); if (tab === 'parse') renderParse(); else if (tab === 'history') renderHistory(); else if (tab === 'ai') renderAI(); else renderSettings(); } function setBody(node) { destroyPlayer(); ui.$body.innerHTML = ''; if (node) ui.$body.appendChild(node); ui.$body.scrollTop = 0; } /* ------------------------- 视图:智能解析 ------------------------- */ function renderParse() { const wrap = el('div'); const hero = el('div', 'hero'); hero.appendChild(el('span', 'badge', 'SMART PARSE · 智能解析')); hero.appendChild(el('h1', null, '粘贴链接,一眼看穿')); hero.appendChild(el('p', null, '支持腾讯 / 爱奇艺 / 优酷 / 芒果 / B站等视频页,或 mp4 / m3u8 直链。线路可在设置中自定义。')); const inputRow = el('div', 'input-row'); const input = el('input'); input.type = 'text'; input.placeholder = 'https://v.qq.com/x/cover/… 或 https://…/index.m3u8'; input.value = state.url || (isVideoHost() ? location.href : ''); const parseBtn = el('button', 'btn primary', '开始解析'); const sniffBtn = el('button', 'btn ghost', '嗅探本页'); inputRow.append(input, parseBtn, sniffBtn); hero.appendChild(inputRow); wrap.appendChild(hero); input.addEventListener('keydown', function (e) { if (e.key === 'Enter') doParse(input.value, state.lineIdx); }); parseBtn.addEventListener('click', function () { doParse(input.value, state.lineIdx); }); sniffBtn.addEventListener('click', function () { const found = sniffPage(); if (!found.length) { toast('本页没有嗅探到视频地址,可手动粘贴'); return; } input.value = found[0]; state.url = found[0]; toast('已嗅探到 ' + found.length + ' 个地址,已填入第一个'); }); const lineSec = el('div', 'section'); const title = el('div', 'section-title'); title.appendChild(el('span', 'num', '1')); title.appendChild(el('span', null, '解析线路')); title.appendChild(el('span', 'hint', '点击线路即可用该线路解析')); lineSec.appendChild(title); const lines = el('div', 'lines'); getLines().filter(function (l) { return l.on && l.url; }).forEach(function (l, i) { const chip = el('button', 'line-chip' + (i === state.lineIdx ? ' on' : '')); chip.appendChild(el('i')); chip.appendChild(el('span', null, l.name)); chip.title = l.url; chip.addEventListener('click', function () { state.lineIdx = i; lines.querySelectorAll('.line-chip').forEach(function (c, j) { c.classList.toggle('on', j === i); }); doParse(input.value || state.url, i); }); lines.appendChild(chip); }); if (!lines.children.length) lines.appendChild(el('div', 'hint', '没有启用的线路,请到「设置」添加')); lineSec.appendChild(lines); wrap.appendChild(lineSec); const result = el('div', 'result'); result.id = 'result'; wrap.appendChild(result); setBody(wrap); if (state.lastResult) { renderResult(state.lastResult); } else { const empty = el('div', 'empty'); empty.style.cssText = 'grid-column:1/-1;background:var(--panel-2);border:1px dashed var(--line);border-radius:18px;padding:48px 20px'; empty.appendChild(el('div', 'big', '▷')); empty.appendChild(el('div', null, '粘贴链接后,解析结果与播放器会显示在这里')); ui.$body.querySelector('#result').appendChild(empty); } } function loadingResult(text) { const box = document.createElement('div'); box.className = 'loading'; box.style.gridColumn = '1 / -1'; const sp = el('div', 'spinner'); box.append(sp, el('div', null, text || '正在解析,请稍候…')); return box; } function doParse(raw, lineIdx) { const url = normalizeUrl(raw); if (!url) { toast('请先粘贴视频地址'); return; } state.url = url; state.title = guessTitle(url); const lines = getLines().filter(function (l) { return l.on && l.url; }); if (!isDirect(url) && !lines.length) { toast('没有可用线路,请到「设置」添加'); return; } state.lineIdx = Math.max(0, Math.min(typeof lineIdx === 'number' ? lineIdx : state.lineIdx, Math.max(0, lines.length - 1))); const result = ui.$body.querySelector('#result'); if (!result) { renderParse(); } const target = ui.$body.querySelector('#result'); target.innerHTML = ''; target.appendChild(loadingResult(isDirect(url) ? '正在加载视频…' : '正在接入 ' + (lines[state.lineIdx] ? lines[state.lineIdx].name : '解析') + ' …')); const type = isDirect(url) ? 'direct' : 'parse'; const data = { url: url, title: state.title, lineIdx: state.lineIdx, lineName: lines[state.lineIdx] ? lines[state.lineIdx].name : '内置播放', type: type }; state.lastResult = data; setTimeout(function () { renderResult(data); }, 60); pushHistory(data); } function renderResult(data) { const target = ui.$body.querySelector('#result'); if (!target) return; target.innerHTML = ''; const stage = el('div', 'stage'); if (data.type === 'direct') { mountPlayer(stage, data.url, data.title); } else { const line = getLines().filter(function (l) { return l.on && l.url; })[data.lineIdx]; const prefix = line ? line.url : ''; const src = prefix + (line && line.encode ? encodeURIComponent(data.url) : data.url); const bar = el('div', 'frame-bar'); bar.append(el('span', null, '线路:' + (data.lineName || '自定义')), el('span', null, '若页面空白,可切换线路或点「新窗口」')); const frame = document.createElement('iframe'); frame.src = src; frame.setAttribute('allowfullscreen', 'true'); frame.setAttribute('referrerpolicy', 'no-referrer'); stage.append(bar, frame); } const info = el('div', 'info-card'); info.appendChild(el('div', 'info-kicker', data.type === 'direct' ? 'DIRECT LINK · 直链播放' : 'PARSED · 解析结果')); info.appendChild(el('div', 'info-title', data.title || '未命名视频')); const urlBox = el('div', 'info-url', data.url); info.appendChild(urlBox); const actions = el('div', 'actions'); const btnCopy = el('button', 'mini-btn', '复制链接'); btnCopy.addEventListener('click', function () { copyText(data.url); }); const btnOpen = el('button', 'mini-btn', '新窗口'); btnOpen.addEventListener('click', function () { openTab(data.url); }); const btnAI = el('button', 'mini-btn hi', 'AI 解读'); btnAI.addEventListener('click', function () { state.aiContext = { url: data.url, title: data.title }; switchTab('ai'); }); actions.append(btnCopy, btnOpen, btnAI); if (data.type === 'direct') { const btnDown = el('button', 'mini-btn', '下载'); btnDown.addEventListener('click', function () { openTab(data.url); }); actions.appendChild(btnDown); } info.appendChild(actions); info.appendChild(el('div', 'info-url', '提示:第三方解析线路取决于线路本身可用性,失效时请在设置中更换前缀。AI 解读需在设置中填写兼容 OpenAI 的接口。')); target.append(stage, info); } /* ------------------------- 视图:历史 ------------------------- */ function getHistory() { const h = load(STORE_HIST, []); return Array.isArray(h) ? h : []; } function pushHistory(data) { const h = getHistory(); h.unshift({ url: data.url, title: data.title || guessTitle(data.url), type: data.type, lineIdx: data.lineIdx || 0, lineName: data.lineName || '', time: Date.now(), }); save(STORE_HIST, h.slice(0, MAX_HISTORY)); } function renderHistory() { const wrap = el('div'); const h = getHistory(); const sec = el('div', 'section'); const title = el('div', 'section-title'); title.appendChild(el('span', 'num', '2')); title.appendChild(el('span', null, '历史记录')); title.appendChild(el('span', 'hint', '共 ' + h.length + ' 条')); sec.appendChild(title); if (!h.length) { sec.appendChild(el('div', 'empty', '还没有解析记录。去「智能解析」粘贴一个链接试试。')); } else { const list = el('div', 'list'); h.forEach(function (item, idx) { const row = el('div', 'item'); row.appendChild(el('span', 'tag', item.type === 'direct' ? '直链' : '解析')); const grow = el('div', 'grow'); grow.appendChild(el('div', 't', item.title)); grow.appendChild(el('div', 'm', new Date(item.time).toLocaleString() + ' · ' + item.url)); row.appendChild(grow); const go = el('button', 'mini-btn', '重播'); go.addEventListener('click', function () { switchTab('parse'); state.lineIdx = item.lineIdx || 0; doParse(item.url, item.lineIdx || 0); }); const del = el('button', 'mini-btn danger', '删除'); del.addEventListener('click', function () { const cur = getHistory(); cur.splice(idx, 1); save(STORE_HIST, cur); renderHistory(); }); row.append(go, del); list.appendChild(row); }); sec.appendChild(list); const clear = el('button', 'mini-btn danger', '清空全部'); clear.style.marginTop = '14px'; clear.addEventListener('click', function () { save(STORE_HIST, []); renderHistory(); }); sec.appendChild(clear); } wrap.appendChild(sec); setBody(wrap); } /* ------------------------- 视图:AI 解读 ------------------------- */ function renderAI() { const wrap = el('div'); const cfg = load(STORE_AI, DEFAULT_AI); const sec = el('div', 'section'); const title = el('div', 'section-title'); title.appendChild(el('span', 'num', '3')); title.appendChild(el('span', null, 'AI 智能解读')); title.appendChild(el('span', 'hint', '可选功能,需自行填写 OpenAI 兼容接口')); sec.appendChild(title); if (!cfg.key) { const empty = el('div', 'empty'); empty.appendChild(el('div', 'big', '✧')); empty.appendChild(el('div', null, '还没有配置 AI 接口。')); const btn = el('button', 'mini-btn hi', '去设置'); btn.style.marginTop = '12px'; btn.addEventListener('click', function () { switchTab('settings'); }); empty.appendChild(btn); sec.appendChild(empty); wrap.appendChild(sec); setBody(wrap); return; } const ctx = state.aiContext || state.lastResult; const context = el('div', 'info-card'); context.style.marginBottom = '14px'; context.appendChild(el('div', 'info-kicker', 'CURRENT CONTEXT · 当前上下文')); context.appendChild(el('div', 'info-title', ctx ? ctx.title : '未选择视频')); context.appendChild(el('div', 'info-url', ctx ? ctx.url : '请先在「智能解析」中解析一个视频,再回来解读。')); sec.appendChild(context); const q = el('textarea'); q.value = '这个视频讲了什么?请用三句话总结,并列出 3 个值得关注的点。'; sec.appendChild(q); const btn = el('button', 'btn primary', '生成解读'); btn.style.marginTop = '12px'; sec.appendChild(btn); const out = el('div', 'ai-result', '解读结果会显示在这里。'); sec.appendChild(out); wrap.appendChild(sec); setBody(wrap); btn.addEventListener('click', async function () { if (!ctx) { toast('请先解析一个视频'); return; } btn.disabled = true; out.textContent = '正在思考…'; try { const body = { model: cfg.model, messages: [ { role: 'system', content: '你是视频解析助手。基于用户给出的标题和链接做简洁、客观的解读;不知道的内容不要编造。' }, { role: 'user', content: '视频标题:' + (ctx.title || '未知') + '\n视频链接:' + ctx.url + '\n问题:' + q.value }, ], stream: false, }; const text = await gmRequest(cfg.endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + cfg.key }, body: JSON.stringify(body), }); const json = JSON.parse(text); const content = json && json.choices && json.choices[0] && json.choices[0].message && json.choices[0].message.content; out.textContent = content || JSON.stringify(json).slice(0, 800); } catch (e) { out.textContent = '解读失败:' + (e && e.message ? e.message : e) + '\n请检查接口地址、模型名和 API Key。'; } finally { btn.disabled = false; } }); } /* ------------------------- 视图:设置 ------------------------- */ function renderSettings() { const wrap = el('div'); const lineSec = el('div', 'section'); const lt = el('div', 'section-title'); lt.appendChild(el('span', 'num', '4')); lt.appendChild(el('span', null, '解析线路')); lt.appendChild(el('span', 'hint', '前缀 + 视频页地址;勾选「编码」时对地址做 URL 编码')); lineSec.appendChild(lt); const rows = []; function addRow(s) { const row = el('div', 'row'); const nm = el('input', 'nm'); nm.value = s.name || ''; nm.placeholder = '名称'; const url = el('input', 'url'); url.value = s.url || ''; url.placeholder = 'https://jx.xxx.com/?url='; const on = el('button', 'toggle' + (s.on ? ' on' : ''), s.on ? '启用' : '停用'); const enc = el('button', 'toggle' + (s.encode ? ' on' : ''), s.encode ? '编码' : '不编码'); const del = el('button', 'toggle', '删除'); const st = { on: !!s.on, encode: !!s.encode }; on.addEventListener('click', function () { st.on = !st.on; on.classList.toggle('on', st.on); on.textContent = st.on ? '启用' : '停用'; }); enc.addEventListener('click', function () { st.encode = !st.encode; enc.classList.toggle('on', st.encode); enc.textContent = st.encode ? '编码' : '不编码'; }); row.append(nm, url, on, enc, del); lineSec.appendChild(row); const rec = { row: row, get data() { return { name: nm.value.trim(), url: url.value.trim(), on: st.on, encode: st.encode }; } }; rows.push(rec); del.addEventListener('click', function () { row.remove(); const i = rows.indexOf(rec); if (i >= 0) rows.splice(i, 1); }); } getLines().forEach(addRow); const addLine = el('button', 'mini-btn', '+ 添加线路'); addLine.addEventListener('click', function () { addRow({ name: '', url: '', on: true, encode: false }); }); lineSec.appendChild(addLine); wrap.appendChild(lineSec); const aiSec = el('div', 'section'); const at = el('div', 'section-title'); at.appendChild(el('span', 'num', '5')); at.appendChild(el('span', null, 'AI 接口(OpenAI 兼容)')); at.appendChild(el('span', 'hint', '密钥仅保存在本机')); aiSec.appendChild(at); const cfg = load(STORE_AI, DEFAULT_AI); function field(label, placeholder, value, type) { const f = el('div', 'field'); f.appendChild(el('label', null, label)); const inp = el('input'); inp.placeholder = placeholder; inp.value = value || ''; if (type) inp.type = type; f.appendChild(inp); aiSec.appendChild(f); return inp; } const ep = field('接口地址', 'https://api.openai.com/v1/chat/completions', cfg.endpoint); const model = field('模型名称', 'gpt-4o-mini', cfg.model); const key = field('API Key', 'sk-…', cfg.key, 'password'); wrap.appendChild(aiSec); const saveBtn = el('button', 'btn primary', '保存设置'); saveBtn.style.marginTop = '18px'; saveBtn.addEventListener('click', function () { const lines = rows.map(function (r) { return r.data; }).filter(function (l) { return l.name && l.url; }); if (!lines.length) { toast('至少保留一条完整线路'); return; } save(STORE_LINES, lines); save(STORE_AI, { endpoint: ep.value.trim(), model: model.value.trim(), key: key.value.trim() }); state.lineIdx = 0; toast('设置已保存'); }); const reset = el('button', 'mini-btn danger', '恢复默认线路'); reset.style.marginTop = '18px'; reset.style.marginLeft = '10px'; reset.addEventListener('click', function () { save(STORE_LINES, JSON.parse(JSON.stringify(DEFAULT_LINES))); toast('已恢复默认线路,重新打开设置查看'); renderSettings(); }); const tip = el('div', 'info-url', '快捷键:Alt + A 打开/关闭面板,Esc 关闭。解析线路为第三方服务,请自行确认可用性;AI 解读只会把你当前解析的标题和链接发送给所填接口。'); tip.style.marginTop = '18px'; wrap.append(saveBtn, reset, tip); setBody(wrap); } /* ------------------------- 启动 ------------------------- */ ensureUI(); })();