Fix Brave Bug for YouTube Live Chat

To Fix Brave Bug for YouTube Live Chat

目前为 2023-08-30 提交的版本。查看 最新版本

// ==UserScript==
// @name         Fix Brave Bug for YouTube Live Chat
// @namespace    UserScripts
// @version      1.0
// @description  To Fix Brave Bug for YouTube Live Chat
// @author       CY Fung
// @license      MIT
// @match        https://www.youtube.com/*
// @grant        none
// @run-at       document-start
// @unwrap
// @inject-into  page
// ==/UserScript==

(async () => {
    'use strict';

    let enableDetection = false;
    customElements.whenDefined('ytd-live-chat-frame').then(()=>{
        enableDetection = true;
    });

    const chat = await new Promise(resolve => {
        let mo = new MutationObserver(entries => {
            const chat = enableDetection ? document.getElementById('chat') : false;
            if (chat) {
                mo.disconnect();
                mo.takeRecords();
                mo = null;
                resolve(chat);
            }
        });
        mo.observe(document, { childList: true, subtree: true })
    });

    if (!chat || chat.is !== 'ytd-live-chat-frame') return;

    /** @param {HTMLIFrameElement} chatframe */
    const onChatFrameFound = (chatframe) => {
        try {
            const body = chatframe.contentDocument.body;
            let io = new IntersectionObserver(function () {
                io.disconnect();
                io.takeRecords();
                io = null;
                const frameLocation = chatframe.contentWindow.location;
                const src = chatframe.src || '';
                if (body.firstChild === null && src.includes('/live_chat') && frameLocation.href === 'about:blank') {
                    frameLocation.replace(src.replace(/&\d+$/, '') + "&1");
                }
            });
            io.observe(body);
        } catch (e) {
            console.warn(e);
        }
    }

    const f = () => {
        const chatframe = ((chat.$ || 0).chatframe || 0);
        if (chatframe instanceof HTMLIFrameElement) {
            if (!chatframe.__b375__) {
                chatframe.__b375__ = 1;
                Promise.resolve(chatframe).then(onChatFrameFound);
            }
        }
    }

    const mo = new MutationObserver(f);

    mo.observe(chat, {
        attributes: true,
        attributeFilter: ['collapsed', 'hidden']
    });

    f();
})();

QingJ © 2025

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