自动打开b站的字幕

自动打开b站的字幕,每个视频只检测一次

// ==UserScript==
// @name         自动打开b站的字幕
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  自动打开b站的字幕,每个视频只检测一次
// @author       lhr3572651322
// @license      MIT
// @match        https://www.bilibili.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        none
// ==/UserScript==

(function() {
    let checkInterval = null;
    let currentHref = window.location.href;
    let hasClicked = false;

    function checkSubtitle() {
        // 精准定位字幕按钮
        const buttons = document.querySelectorAll('.bpx-common-svg-icon');
        for (const btn of buttons) {
            const parent = btn.parentElement?.parentElement;
            if (parent?.getAttribute('aria-label') === '字幕') {
                if (!hasClicked) {
                    btn.click();
                    hasClicked = true;
                    clearInterval(checkInterval);
                }
                return;
            }
        }
    }

    function startCheck() {
        // 重置状态
        hasClicked = false;
        if (checkInterval) clearInterval(checkInterval);
        
        // 立即检查一次
        checkSubtitle();
        
        // 设置检测间隔
        checkInterval = setInterval(checkSubtitle, 300);
        
        // 10秒后自动停止检测
        setTimeout(() => {
            clearInterval(checkInterval);
            checkInterval = null;
        }, 10000);
    }

    // 初始检测
    startCheck();

    // 监听URL变化(SPA路由切换)
    setInterval(() => {
        if (window.location.href !== currentHref) {
            currentHref = window.location.href;
            startCheck();
        }
    }, 500);

    // 页面完全加载后重新检测
    window.addEventListener('load', startCheck);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址