// ==UserScript== // @name Open in Vidleaf — YouTube, Bilibili, Facebook & X Transcript Reader // @name:zh-CN 在 Vidleaf 打开 — YouTube、B 站、Facebook 与 X 视频字幕阅读按钮 // @name:zh-TW 在 Vidleaf 開啟 — YouTube、Bilibili、Facebook 與 X 影片字幕閱讀按鈕 // @name:ja Vidleaf で開く — YouTube・Bilibili・Facebook・X 動画字幕リーダー // @name:pt-BR Abrir no Vidleaf — Leitor de Legendas para YouTube, Bilibili, Facebook e X // @name:es Abrir en Vidleaf — Lector de Subtítulos para YouTube, Bilibili, Facebook y X // @namespace https://vidleaf.app/ // @version 1.2.0 // @description Adds a clean "Open in Vidleaf" button and Alt+V / Alt+Shift+V shortcuts on YouTube, Bilibili, Facebook, X (Twitter), Douyin, and Xiaohongshu to read, phrase-search, summarize, and export video transcripts (SRT/TXT/Markdown) with no login. // @description:zh-CN 在 YouTube、B 站、Facebook、X(Twitter)、抖音与小红书视频页添加「在 Vidleaf 读字幕」按钮与 Alt+V(字幕)/ Alt+Shift+V(总结)快捷键,免登录提取检索字幕并导出 SRT、TXT 或 Markdown 笔记。 // @description:zh-TW 在 YouTube、Bilibili、Facebook、X(Twitter)、抖音與小紅書影片頁加入「在 Vidleaf 讀字幕」按鈕與 Alt+V / Alt+Shift+V 快捷鍵,免登入擷取檢索字幕並匯出附時間戳記的 SRT、TXT 或 Markdown 筆記。 // @description:ja YouTube・Bilibili・Facebook・X・抖音・小紅書の動画ページに「Vidleaf で字幕を読む」ボタンと Alt+V / Alt+Shift+V ショートカットを追加し、ログインなしで字幕の検索・要約・SRT/TXT/Markdown 出力を行えます。 // @description:pt-BR Adiciona um botão "Abrir no Vidleaf" e atalhos Alt+V / Alt+Shift+V no YouTube, Bilibili, Facebook, X, Douyin e Xiaohongshu para ler, pesquisar frases, resumir e exportar legendas em SRT, TXT ou Markdown sem login. // @description:es Añade el botón "Abrir en Vidleaf" y atajos Alt+V / Alt+Shift+V en YouTube, Bilibili, Facebook, X (Twitter), Douyin y Xiaohongshu para leer, buscar citas, resumir y exportar subtítulos en SRT, TXT o Markdown sin registro. // @author Vidleaf // @homepageURL https://github.com/blackmirror69/vidleaf-open // @supportURL https://vidleaf.app/en/youtube-transcript // @downloadURL https://raw.githubusercontent.com/blackmirror69/vidleaf-open/main/vidleaf-open.user.js // @updateURL https://raw.githubusercontent.com/blackmirror69/vidleaf-open/main/vidleaf-open.user.js // @license MIT // @match *://*.youtube.com/* // @match *://*.bilibili.com/* // @match *://*.bilibili.tv/* // @match *://*.facebook.com/* // @match *://*.douyin.com/* // @match *://*.xiaohongshu.com/* // @match *://*.rednote.com/* // @match *://*.x.com/* // @match *://*.twitter.com/* // @grant GM_registerMenuCommand // @grant GM_openInTab // @run-at document-idle // ==/UserScript== (function () { 'use strict'; var VIDLEAF_OPEN_ENDPOINT = 'https://vidleaf.app/open?url='; var BTN_ID = 'vidleaf-quick-open-btn'; var FLOAT_ID = 'vidleaf-quick-open-float'; var STYLE_ID = 'vidleaf-quick-open-style'; var X_SKIP_HANDLES = /^(i|home|explore|search|notifications|messages|settings|compose|hashtag|tos|privacy|intent|share|safety|login|logout|signup)$/i; function detectLocale(rawLang) { var navLang = String(rawLang || 'en').toLowerCase(); if (/^zh-(hant|tw|hk|mo)/.test(navLang)) return 'zh-Hant'; if (/^zh\b/.test(navLang)) return 'zh-CN'; if (/^ja\b/.test(navLang)) return 'ja'; if (/^pt\b/.test(navLang)) return 'pt-BR'; if (/^es\b/.test(navLang)) return 'es'; return 'en'; } var locale = detectLocale((typeof navigator !== 'undefined' && navigator.language) || 'en'); var COPY = { 'zh-CN': { label: '在 Vidleaf 读字幕', summaryMenu: '在 Vidleaf 生成章节总结 (Alt+Shift+V)', tooltip: '免登录提取字幕、搜原话并导出 SRT/TXT/Markdown(Alt+V · 总结:Alt+Shift+V)', hide: '本次会话隐藏' }, 'zh-Hant': { label: '在 Vidleaf 讀字幕', summaryMenu: '在 Vidleaf 產生章節摘要 (Alt+Shift+V)', tooltip: '免登入擷取字幕、搜尋原話並匯出 SRT/TXT/Markdown(Alt+V · 摘要:Alt+Shift+V)', hide: '本次工作階段隱藏' }, ja: { label: 'Vidleaf で字幕を読む', summaryMenu: 'Vidleaf で章立て要約を作成 (Alt+Shift+V)', tooltip: 'ログインなしで字幕検索・SRT/TXT/Markdown出力(Alt+V・要約:Alt+Shift+V)', hide: 'このタブで非表示' }, 'pt-BR': { label: 'Ler no Vidleaf', summaryMenu: 'Resumir no Vidleaf (Alt+Shift+V)', tooltip: 'Ler, pesquisar frases e exportar SRT/TXT/Markdown sem login (Alt+V · Resumo: Alt+Shift+V)', hide: 'Ocultar nesta aba' }, es: { label: 'Abrir en Vidleaf', summaryMenu: 'Resumir en Vidleaf (Alt+Shift+V)', tooltip: 'Leer, buscar citas y exportar SRT/TXT/Markdown sin registro (Alt+V · Resumen: Alt+Shift+V)', hide: 'Ocultar en esta pestaña' }, en: { label: 'Open in Vidleaf', summaryMenu: 'Summarize in Vidleaf (Alt+Shift+V)', tooltip: 'Read, search & export captions as SRT/TXT/Markdown in Vidleaf (Alt+V · Summary: Alt+Shift+V)', hide: 'Hide for this tab' } }; var LABEL_TEXT = COPY[locale].label; var SUMMARY_MENU_TEXT = COPY[locale].summaryMenu; var TOOLTIP_TEXT = COPY[locale].tooltip; var HIDE_TEXT = COPY[locale].hide; var dismissedForSession = false; /** * Normalizes a supported video page URL. Returns empty string on non-video pages * (such as home feeds, photo posts, or search result listings) so no button is shown unnecessarily. */ function resolveVideoUrl(loc) { var host = (loc.hostname || '').toLowerCase(); var path = loc.pathname || '/'; var params = new URLSearchParams(loc.search || ''); var t = params.get('t'); var tParam = t && /^[0-9hms.]+$/i.test(t) ? t : ''; if (/(^|\.)youtube\.com$/i.test(host)) { var v = params.get('v'); if (path === '/watch' && v) { return 'https://www.youtube.com/watch?v=' + encodeURIComponent(v) + (tParam ? '&t=' + encodeURIComponent(tParam) : ''); } if (/^\/(shorts|live|embed)\/[A-Za-z0-9_-]{11}\/?$/.test(path)) { return 'https://www.youtube.com' + path.replace(/\/$/, ''); } return ''; } if (/(^|\.)bilibili\.com$/i.test(host)) { if (/^\/(bangumi\/play\/(ep|ss)|cheese\/play\/ep)[1-9]\d*\/?$/i.test(path)) { return 'https://www.bilibili.com' + path.replace(/\/$/, ''); } var bvMatch = path.match(/^\/video\/(BV[A-Za-z0-9]{10}|av\d+)/i); var bvid = bvMatch ? bvMatch[1] : (/^\/(festival|list|medialist|blackboard)\//i.test(path) && /^BV[A-Za-z0-9]{10}$/.test(params.get('bvid') || '') ? params.get('bvid') : ''); if (bvid) { var ep = params.get('p'); var qParts = []; if (ep && ep !== '1' && /^\d+$/.test(ep)) qParts.push('p=' + encodeURIComponent(ep)); if (tParam) qParts.push('t=' + encodeURIComponent(tParam)); return 'https://www.bilibili.com/video/' + bvid + (qParts.length ? '?' + qParts.join('&') : ''); } return ''; } if (/(^|\.)bilibili\.tv$/i.test(host)) { var intl = path.match(/\/video\/([1-9]\d{0,19})\/?$/); return intl ? 'https://www.bilibili.tv/en/video/' + intl[1] : ''; } if (/(^|\.)facebook\.com$/i.test(host)) { if (path === '/watch' || path === '/watch/' || path === '/video.php') { var fvs = params.getAll('v'); return fvs.length === 1 && /^[1-9]\d{0,19}$/.test(fvs[0]) ? 'https://www.facebook.com/watch/?v=' + fvs[0] : ''; } var fbm = path.match(/^\/(?:reel|[A-Za-z0-9._-]+\/videos)\/([1-9]\d{0,19})\/?$/); if (fbm) return 'https://www.facebook.com/watch/?v=' + fbm[1]; if (/^\/share\/[vr]\/[A-Za-z0-9_-]{1,64}\/?$/.test(path)) return 'https://www.facebook.com' + path; return ''; } if (/(^|\.)douyin\.com$/i.test(host)) { var modalId = params.get('modal_id'); if (modalId && /^\d+$/.test(modalId)) { return 'https://www.douyin.com/video/' + modalId; } var dy = path.match(/^\/(?:share\/)?video\/(\d+)/); if (dy) { return 'https://www.douyin.com/video/' + dy[1]; } return ''; } if (/(^|\.)(xiaohongshu|rednote)\.com$/i.test(host)) { if (/^\/(explore|discovery\/item)\/[0-9a-fA-F]{24}\/?$/.test(path)) { return loc.href; } return ''; } if (/(^|\.)(x|twitter)\.com$/i.test(host)) { var xm = path.match(/^\/(?:i(?:\/web)?\/status|statuses|([A-Za-z0-9_]{1,15})\/status(?:es)?)\/([1-9]\d{0,19})(?:\/(video|photo)\/([^/]*))?\/?$/i); if (!xm || (xm[1] && X_SKIP_HANDLES.test(xm[1])) || xm[3] === 'photo') return ''; if (xm[3] === 'video' && !/^[1-4]$/.test(xm[4] || '')) return ''; var vIdx = xm[3] === 'video' && xm[4] !== '1' ? '/video/' + xm[4] : ''; return 'https://x.com/i/status/' + xm[2] + vIdx; } return ''; } function buildVidleafHref(videoUrl, goal) { return VIDLEAF_OPEN_ENDPOINT + encodeURIComponent(videoUrl) + (goal === 'summary' ? '&goal=summary' : ''); } function launchVidleaf(videoUrl, goal) { if (!videoUrl) return; var target = buildVidleafHref(videoUrl, goal); if (typeof GM_openInTab === 'function') { GM_openInTab(target, { active: true, insert: true }); return; } var win = window.open(target, '_blank', 'noopener,noreferrer'); if (!win) { window.location.href = target; } } function injectStyles() { if (document.getElementById(STYLE_ID)) return; var style = document.createElement('style'); style.id = STYLE_ID; style.textContent = [ '#' + BTN_ID + ' {', ' display: inline-flex;', ' align-items: center;', ' gap: 6px;', ' height: 36px;', ' padding: 0 14px;', ' margin-left: 8px;', ' border-radius: 999px;', ' border: 1px solid rgba(127, 127, 127, 0.28);', ' background: rgba(255, 255, 255, 0.92);', ' color: #0f172a;', ' font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;', ' font-size: 13px;', ' font-weight: 500;', ' line-height: 1;', ' text-decoration: none;', ' cursor: pointer;', ' white-space: nowrap;', ' transition: background 0.15s ease, border-color 0.15s ease;', '}', 'html[dark] #' + BTN_ID + ', [data-theme="dark"] #' + BTN_ID + ' {', ' background: rgba(255, 255, 255, 0.1);', ' border-color: rgba(255, 255, 255, 0.16);', ' color: #f1f5f9;', '}', '#' + BTN_ID + ':hover {', ' background: #e0f2fe;', ' border-color: #38bdf8;', ' color: #0369a1;', '}', 'html[dark] #' + BTN_ID + ':hover {', ' background: rgba(56, 189, 248, 0.2);', ' border-color: #38bdf8;', ' color: #bae6fd;', '}', '#' + FLOAT_ID + ' {', ' position: fixed;', ' right: 20px;', ' bottom: 84px;', ' z-index: 2147483640;', ' display: inline-flex;', ' align-items: center;', ' gap: 4px;', ' padding: 4px 6px 4px 12px;', ' border-radius: 999px;', ' background: rgba(15, 23, 42, 0.88);', ' color: #f8fafc;', ' box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);', ' backdrop-filter: blur(8px);', ' font-family: system-ui, -apple-system, sans-serif;', ' font-size: 13px;', '}', '#' + FLOAT_ID + ' a {', ' color: #f8fafc;', ' text-decoration: none;', ' font-weight: 500;', ' padding-right: 4px;', '}', '#' + FLOAT_ID + ' a:hover { color: #7dd3fc; }', '#' + FLOAT_ID + ' button {', ' border: none;', ' background: transparent;', ' color: #94a3b8;', ' cursor: pointer;', ' font-size: 14px;', ' line-height: 1;', ' padding: 2px 6px;', ' border-radius: 999px;', '}', '#' + FLOAT_ID + ' button:hover { color: #ffffff; background: rgba(255,255,255,0.14); }' ].join('\n'); (document.head || document.documentElement).appendChild(style); } function createSvgIcon() { var svgNS = 'http://www.w3.org/2000/svg'; var svg = document.createElementNS(svgNS, 'svg'); svg.setAttribute('width', '14'); svg.setAttribute('height', '14'); svg.setAttribute('viewBox', '0 0 24 24'); svg.setAttribute('fill', 'none'); svg.setAttribute('stroke', 'currentColor'); svg.setAttribute('stroke-width', '2'); svg.setAttribute('stroke-linecap', 'round'); svg.setAttribute('stroke-linejoin', 'round'); var p1 = document.createElementNS(svgNS, 'path'); p1.setAttribute('d', 'M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z'); var p2 = document.createElementNS(svgNS, 'polyline'); p2.setAttribute('points', '14 2 14 8 20 8'); var p3 = document.createElementNS(svgNS, 'line'); p3.setAttribute('x1', '8'); p3.setAttribute('y1', '13'); p3.setAttribute('x2', '16'); p3.setAttribute('y2', '13'); var p4 = document.createElementNS(svgNS, 'line'); p4.setAttribute('x1', '8'); p4.setAttribute('y1', '17'); p4.setAttribute('x2', '13'); p4.setAttribute('y2', '17'); svg.appendChild(p1); svg.appendChild(p2); svg.appendChild(p3); svg.appendChild(p4); return svg; } function removeWidgets() { var inlineBtn = document.getElementById(BTN_ID); if (inlineBtn && inlineBtn.parentNode) inlineBtn.parentNode.removeChild(inlineBtn); var floatPill = document.getElementById(FLOAT_ID); if (floatPill && floatPill.parentNode) floatPill.parentNode.removeChild(floatPill); } function findInlineMountTarget() { var host = (window.location.hostname || '').toLowerCase(); var path = window.location.pathname || '/'; if (/(^|\.)youtube\.com$/i.test(host) && host !== 'music.youtube.com' && path === '/watch') { return ( document.querySelector('ytd-watch-metadata #top-level-buttons-computed') || document.querySelector('#above-the-fold #top-level-buttons-computed') ); } if (/(^|\.)bilibili\.com$/i.test(host) && /^\/(video|bangumi\/play|cheese\/play|list|festival|medialist)\//i.test(path)) { return ( document.querySelector('.video-toolbar-left') || document.querySelector('#arc_toolbar_report .video-toolbar-left-main') || document.querySelector('#toolbar_module .toolbar-left') ); } return null; } function syncUi() { if (typeof document === 'undefined' || !document.body) return; var videoUrl = resolveVideoUrl(window.location); if (!videoUrl) { removeWidgets(); return; } injectStyles(); var targetHref = buildVidleafHref(videoUrl); var inlineContainer = findInlineMountTarget(); if (inlineContainer) { var floatExisting = document.getElementById(FLOAT_ID); if (floatExisting && floatExisting.parentNode) floatExisting.parentNode.removeChild(floatExisting); var btn = document.getElementById(BTN_ID); if (!btn) { btn = document.createElement('a'); btn.id = BTN_ID; btn.target = '_blank'; btn.rel = 'noopener noreferrer'; btn.title = TOOLTIP_TEXT; btn.appendChild(createSvgIcon()); var span = document.createElement('span'); span.textContent = LABEL_TEXT; btn.appendChild(span); inlineContainer.appendChild(btn); } btn.href = targetHref; return; } // Fallback floating pill for YouTube Shorts, X (Twitter), Douyin, Xiaohongshu, or delayed toolbar loads if (dismissedForSession) return; var pill = document.getElementById(FLOAT_ID); if (!pill) { pill = document.createElement('div'); pill.id = FLOAT_ID; var link = document.createElement('a'); link.target = '_blank'; link.rel = 'noopener noreferrer'; link.title = TOOLTIP_TEXT; link.textContent = LABEL_TEXT + ' ↗'; var closeBtn = document.createElement('button'); closeBtn.type = 'button'; closeBtn.title = HIDE_TEXT; closeBtn.textContent = '×'; closeBtn.addEventListener('click', function (ev) { ev.preventDefault(); ev.stopPropagation(); dismissedForSession = true; removeWidgets(); }); pill.appendChild(link); pill.appendChild(closeBtn); document.body.appendChild(pill); } var anchor = pill.querySelector('a'); if (anchor) anchor.href = targetHref; } function isEditableTarget(el) { if (!el) return false; var tag = (el.tagName || '').toUpperCase(); return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || Boolean(el.isContentEditable); } function initBrowserHooks() { if (typeof GM_registerMenuCommand === 'function') { GM_registerMenuCommand(LABEL_TEXT + ' (Alt+V)', function () { var url = resolveVideoUrl(window.location) || window.location.href; launchVidleaf(url, 'transcript'); }); GM_registerMenuCommand(SUMMARY_MENU_TEXT, function () { var url = resolveVideoUrl(window.location) || window.location.href; launchVidleaf(url, 'summary'); }); } window.addEventListener('keydown', function (ev) { if (ev.altKey && !ev.ctrlKey && !ev.metaKey && ev.code === 'KeyV') { if (isEditableTarget(document.activeElement)) return; var videoUrl = resolveVideoUrl(window.location); if (videoUrl) { ev.preventDefault(); launchVidleaf(videoUrl, ev.shiftKey ? 'summary' : 'transcript'); } } }); window.addEventListener('yt-navigate-finish', syncUi); window.addEventListener('popstate', syncUi); var lastHref = window.location.href; var syncTimer = null; var observer = new MutationObserver(function () { if (window.location.href !== lastHref) { lastHref = window.location.href; dismissedForSession = false; } if (syncTimer) return; syncTimer = setTimeout(function () { syncTimer = null; syncUi(); }, 350); }); observer.observe(document.documentElement, { childList: true, subtree: true }); syncUi(); } if (typeof window !== 'undefined' && typeof document !== 'undefined') { initBrowserHooks(); } if (typeof module !== 'undefined' && module.exports) { module.exports = { resolveVideoUrl: resolveVideoUrl, buildVidleafHref: buildVidleafHref, detectLocale: detectLocale, COPY: COPY }; } })();