Greasy Fork

Greasy Fork is available in English.

YouTube 中英双语字幕

通过菜单配置中文字体大小和Debug 日志开关

当前为 2026-02-27 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube 中英双语字幕
// @version      3.5
// @author       4Aiur
// @namespace    http://greasyfork.icu/zh-CN/users/394849-4aiur
// @description  通过菜单配置中文字体大小和Debug 日志开关
// @match        *://www.youtube.com/*
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// @grant        GM_openInTab
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const TARGET_LANG = 'zh-CN';
    const translationCache = new Map();
    const timers = new Map();

    // -------------------- 1. 配置 & 菜单 --------------------
    let fontSize = GM_getValue('subtitle_font_size', 1.5);
    let isDebug = GM_getValue('debug_mode', false); // 默认关闭日志

    // 封装日志输出,增加 debug 判断
    const log = {
        info: (msg, ...args) => {
            if (isDebug) console.log(`%c[YouTube双语字幕]%c ${msg}`, 'color: #00bcd4; font-weight: bold', '', ...args);
        },
        warn: (msg, ...args) => {
            if (isDebug) console.warn(`[YouTube双语字幕] ${msg}`, ...args);
        },
        error: (msg, ...args) => {
            // Error 级别日志通常建议始终保留,或根据需求也加上 isDebug 判断
            console.error(`[YouTube双语字幕] ${msg}`, ...args);
        }
    };

    function registerMenu() {
        // 清除之前的菜单(某些脚本管理器支持,若不支持则需刷新页面)
        // 1. 设置字体大小
        GM_registerMenuCommand(
            `⚙️ 设置中文字体大小 (当前: ${fontSize}em)`,
            () => {
                let newSize = prompt('请输入字体大小 (建议 1.1 - 2.0):', fontSize);
                if (newSize !== null) {
                    newSize = parseFloat(newSize);
                    if (!isNaN(newSize) && newSize > 0) {
                        fontSize = newSize;
                        GM_setValue('subtitle_font_size', newSize);
                        updateStyle();
                        location.reload();
                    }
                }
            }
        );

        // 2. Debug 日志开关
        GM_registerMenuCommand(
            isDebug ? '🚫 关闭调试日志 (当前: 开启)' : '🐞 开启调试日志 (当前: 关闭)',
            () => {
                isDebug = !isDebug;
                GM_setValue('debug_mode', isDebug);
                // 为了让逻辑重新加载,建议刷新页面
                location.reload();
            }
        );

        // 3. 反馈建议
        GM_registerMenuCommand('💬 反馈 & 建议', () => {
            GM_openInTab(
                'http://greasyfork.icu/zh-CN/scripts/567512-youtube-%E4%B8%AD%E8%8B%B1%E5%8F%8C%E8%AF%AD%E5%AD%97%E5%B9%95/feedback',
                { active: true }
            );
        });
    }

    // -------------------- 2. Trusted Types 兼容 --------------------
    let ttPolicy;
    if (window.trustedTypes && window.trustedTypes.createPolicy) {
        ttPolicy = window.trustedTypes.createPolicy(
            'youtube-dual-subtitles-policy',
            { createHTML: s => s }
        );
    }

    function setSafeContent(el, content) {
        if (!el) return;
        el.innerText = content;
    }

    // -------------------- 3. CSS --------------------
    function updateStyle() {
        document.getElementById('my-subtitle-style')?.remove();

        const css = `
        .ytp-caption-window-container .caption-visual-line {
            display: flex !important;
            flex-direction: column !important;
            align-items: center !important;
            justify-content: flex-end !important;
            background: transparent !important;
        }
        .ytp-caption-segment {
            display: inline-block !important;
            white-space: pre-wrap !important;
        }
        .my-trans-line {
            display: block !important;
            order: -1 !important;

            color: #FFCC00 !important;
            font-size: ${fontSize}em !important;
            font-weight: bold !important;
            line-height: 1.2 !important;

            min-height: 1.2em !important;

            /* --- 修改部分 --- */
            white-space: nowrap !important;    /* 强制不换行 */
            width: max-content !important;     /* 宽度自适应内容 */
            max-width: 95vw !important;        /* 防止超出屏幕太多 */
            /* ---------------- */

            margin-top: 4px !important;
            padding: 2px 8px !important;
            background: rgba(0, 0, 0, 0.7) !important;
            border-radius: 4px !important;
            text-shadow: 2px 2px 3px rgba(0,0,0,1) !important;
            text-align: center !important;
        }
        `;

        const style = document.createElement('style');
        style.id = 'my-subtitle-style';
        style.innerHTML = ttPolicy ? ttPolicy.createHTML(css) : css;
        document.head.appendChild(style);
        log.info('样式已加载/更新');
    }

    // -------------------- 4. 翻译 --------------------
    async function translate(text) {
        if (!text || text.length < 2) return null;
        if (translationCache.has(text)) {
            log.info(`命中缓存: "${text.substring(0, 15)}..."`);
            return translationCache.get(text);
        }

        try {
            log.info(`发起请求: "${text.substring(0, 15)}..."`);
            const url =
                `https://translate.googleapis.com/translate_a/single` +
                `?client=gtx&sl=auto&tl=${TARGET_LANG}&dt=t&q=${encodeURIComponent(text)}`;

            const res = await fetch(url);
            const data = await res.json();
            if (data?.[0]) {
                const result = data[0].map(x => x[0]).join('');
                translationCache.set(text, result);
                return result;
            }
        } catch (e) {
            log.error('翻译异常:', e);
        }
        return null;
    }

    // -------------------- 5. 核心逻辑 --------------------
    function processLine(line) {
        const segments = line.querySelectorAll('.ytp-caption-segment');
        if (!segments.length) return;

        let transDiv = line.querySelector('.my-trans-line');
        if (!transDiv) {
            transDiv = document.createElement('div');
            transDiv.className = 'my-trans-line';
            transDiv.innerText = '';
            line.prepend(transDiv);
            log.info('DOM: 创建占位行');
        }

        const fullText = Array.from(segments)
            .map(s => s.innerText)
            .join(' ')
            .replace(/\s+/g, ' ')
            .trim();

        if (!fullText) return;

        if (translationCache.has(fullText)) {
            const cached = translationCache.get(fullText);
            if (transDiv.innerText !== cached) {
                setSafeContent(transDiv, cached);
            }
            return;
        }

        clearTimeout(timers.get(line));
        const timer = setTimeout(async () => {
            if (!line.isConnected) return;
            const result = await translate(fullText);
            if (result && line.isConnected) {
                setSafeContent(transDiv, result);
            }
        }, 200);

        timers.set(line, timer);
    }

    // -------------------- 6. 初始化 --------------------
    const observer = new MutationObserver(() => {
        document.querySelectorAll('.caption-visual-line').forEach(processLine);
    });

    function init() {
        const container = document.querySelector('.ytp-caption-window-container');
        if (container) {
            log.info('初始化: 字幕容器已就绪');
            observer.observe(container, { childList: true, subtree: true });
        } else {
            // 不打印 warn 避免刷屏,除非开启 Debug
            setTimeout(init, 1000);
        }
    }

    registerMenu();
    updateStyle();
    init();

    window.addEventListener('yt-navigate-finish', () => {
        log.info('页面切换: 重置状态');
        timers.clear();
        init();
    });
})();