哔哩哔哩自动打开字幕(兼容自动连播)

bilibili b站 哔哩哔哩 播放视频时自动打开网站字幕(兼容自动连播)

目前为 2024-07-29 提交的版本。查看 最新版本

// ==UserScript==
// @name         哔哩哔哩自动打开字幕(兼容自动连播)
// @namespace    http://tampermonkey.net/
// @version      2024-07-29 0.0.2
// @description  bilibili b站 哔哩哔哩 播放视频时自动打开网站字幕(兼容自动连播)
// @author       You
// @match        https://www.bilibili.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let queryValue = '';
    // 定时检测URL是否发生变化
    let timer = setInterval(function() {
        // 获取URL中的查询字符串部分
        const queryString = window.location.search;
        // 解析查询字符串,将参数以对象的形式存储
        const params = new URLSearchParams(queryString);
        // 获取特定参数的值
        const value = getBilibiliVideoIdFromUrlRegex();
        if (queryValue !== value) {
            openSubtitle();
            queryValue = value;
        }
    }, 2000);

    window.addEventListener('unload', function(_event) {
        clearInterval(timer)
    });

    function getBilibiliVideoIdFromUrlRegex() {
        var url = window.location.href;
        var regex = /https:\/\/www\.bilibili\.com\/video\/(BV[a-zA-Z0-9]+)/;
        var match = url.match(regex);

        if (match && match.length > 1) {
            return match[1]; // match[0] 是整个匹配的字符串,match[1] 是第一个捕获组(即BV编号)
        } else {
            return null; // 没有找到匹配项
        }
    }

    function openSubtitle(){
        setTimeout(() => { document.querySelector('.bpx-player-ctrl-btn[aria-label="字幕"] .bpx-common-svg-icon').click(); }, 1000)
    }
})();

QingJ © 2025

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