Greasy Fork is available in English.

bilibili记忆字幕开关状态

2023/10/7

// ==UserScript==
// @name        bilibili记忆字幕开关状态
// @namespace   Violentmonkey Scripts
// @match         *://www.bilibili.com/video/av*
// @match         *://www.bilibili.com/video/BV*
// @match         *://www.bilibili.com/list/*
// @match         *://www.bilibili.com/bangumi/play/ep*
// @match         *://www.bilibili.com/bangumi/play/ss*
// @match         *://www.bilibili.com/cheese/play/ep*
// @match         *://www.bilibili.com/cheese/play/ss*
// @grant       GM_setValue
// @grant       GM_getValue
// @version     2.3
// @author      lazy cat
// @description 2023/10/7
// @run-at      document-end
// @license MIT
// @homepageURL https://greasyfork.org/zh-CN/scripts/462859-bilibili%E8%AE%B0%E5%BF%86%E5%AD%97%E5%B9%95%E5%BC%80%E5%85%B3%E7%8A%B6%E6%80%81
// @homepage    https://greasyfork.org/zh-CN/scripts/462859-bilibili%E8%AE%B0%E5%BF%86%E5%AD%97%E5%B9%95%E5%BC%80%E5%85%B3%E7%8A%B6%E6%80%81
// ==/UserScript==
// 是否为ai字幕
let want_open = false
function isAiSubtitle() {
    let sub = document.querySelector('.bpx-player-ctrl-subtitle-major-inner > .bpx-state-active')
    if (sub && sub.innerText.includes("自动")) return true
    return false
}
// 获取浏览器型号
function get_borwse_mode() {
    const mode_data = navigator.userAgent
    if (mode_data.includes('Firefox')) {
        return 'Firefox'
    } else if (mode_data.includes('Chrome')) {
        return 'Chrome'
    } else return 'Chrome'
}
// 字幕是否开启
function isSubtitleOpen() {
    let max_length = 3
    if (get_borwse_mode() === 'Firefox') max_length = 2
    let sub = document.querySelectorAll('svg[preserveAspectRatio="xMidYMid meet"] > defs > filter')
    if (sub.length === max_length) return false
    return true
}
// 本地储存是否开启
function isRememberOpen() {
    return GM_getValue('subtitleOpen', false)
}
// 开启字幕
function openSubtitle() {
    let sub = document.querySelector('[aria-label="字幕"] [class="bpx-common-svg-icon"]')
    console.log('尝试开启字幕' + isRememberOpen())
    if (isAiSubtitle() && !isSubtitleOpen() && isRememberOpen() && !want_open) {
        want_open = true
        setTimeout(() => {
            sub.click()
            want_open = false
        }, 300);
    }
    rememberSwitch()
}
// 记忆开关状态回调函数
function rememberSwitchCallback(e) {
    if (!e.isTrusted) return
    if (!isAiSubtitle()) return
    setTimeout(() => {
        if (isSubtitleOpen()) GM_setValue('subtitleOpen', true)
        else GM_setValue('subtitleOpen', false)
        console.log('储存字幕开关状态', isRememberOpen())
    }, 300)
}
// 记忆开关状态
function rememberSwitch() {
    let sub = document.querySelector('div[aria-label="字幕"]')
    sub?.addEventListener('click', rememberSwitchCallback)
}
// 视频页面改变监听器
function videoChange() {
    const videoElement = document.querySelector('video')
    videoElement.addEventListener('play', openSubtitle)
}
(function () {
    let i_num = 0
    i_num = setInterval(() => {
        let k = document.querySelector('div[aria-label="宽屏"]')
        if (!k) return
        clearInterval(i_num)
        openSubtitle()
        videoChange()
    }, 100)
})()