Greasy Fork

Greasy Fork is available in English.

哔哩哔哩自动开启AI字幕

自动检测并开启页面中的AI字幕(中文),支持切换合集视频时重新触发检测

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         哔哩哔哩自动开启AI字幕
// @description  自动检测并开启页面中的AI字幕(中文),支持切换合集视频时重新触发检测
// @author       魅影Mazo
// @match        *://*/*
// @grant        none
// @match        https://www.bilibili.com/video/*
// @icon         https://www.bilibili.com/favicon.ico
// @version 0.0.1.20251101114210
// @namespace http://greasyfork.icu/users/1532921
// ==/UserScript==

(function () {
    'use strict';

    // 开启字幕的核心函数
    function turnOnSubtitle() {
        const subtitleButton = document.querySelector('.bpx-player-ctrl-subtitle-language-item[data-lan="ai-zh"]');
        if (subtitleButton) {
            subtitleButton.click();
            return true;
        }
        return false;
    }

    // 初始检查并开启字幕
    let checkInterval = setInterval(() => {
        if (turnOnSubtitle()) {
            clearInterval(checkInterval);
        }
    }, 500);

    // 监听URL变化(处理单页应用路由切换)
    let lastUrl = location.href;
    const urlChangeHandler = () => {
        // URL发生变化时重新开始检查字幕按钮
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            // 清除之前的定时器,重新开始检查
            clearInterval(checkInterval);
            checkInterval = setInterval(() => {
                if (turnOnSubtitle()) {
                    clearInterval(checkInterval);
                }
            }, 500);
        }
    };

    // 监听历史记录变化
    window.addEventListener('popstate', urlChangeHandler);

    // 监听document变化以捕获pushState/replaceState导致的URL变化
    const observer = new MutationObserver(urlChangeHandler);
    observer.observe(document, {
        subtree: true,
        childList: true
    });

})();