小红书 & 百度贴吧 视频自动取消静音

小红书、百度贴吧网页端:视频自动取消静音

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         小红书 & 百度贴吧 视频自动取消静音
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  小红书、百度贴吧网页端:视频自动取消静音
// @match        *://www.xiaohongshu.com/*
// @match        *://tieba.baidu.com/*
// @icon         https://www.xiaohongshu.com/favicon.ico
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    function handleVideo(video) {
        if (video.__xhsAutoUnmute) return;
        video.__xhsAutoUnmute = true;

        video.addEventListener('play', function() {
            if (video.muted) video.muted = false;
            if (video.volume === 0) video.volume = 1;
        });

        if (video.muted) video.muted = false;
        if (video.volume === 0) video.volume = 1;
    }

    function scanAndHandleVideos() {
        document.querySelectorAll('video').forEach(handleVideo);
    }

    function setupVideoObserver() {
        if (window.__xhsVideoUnmuteObserver) return;
        window.__xhsVideoUnmuteObserver = true;

        scanAndHandleVideos();
        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeName === 'VIDEO') {
                        handleVideo(node);
                    } else if (node.querySelectorAll) {
                        node.querySelectorAll('video').forEach(handleVideo);
                    }
                });
            });
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    function waitForHeadAndBody(fn) {
        if (document.head && document.body) {
            fn();
        } else {
            const observer = new MutationObserver(() => {
                if (document.head && document.body) {
                    observer.disconnect();
                    fn();
                }
            });
            observer.observe(document.documentElement, { childList: true, subtree: true });
        }
    }
    waitForHeadAndBody(setupVideoObserver);

    window.addEventListener('DOMContentLoaded', () => {
        scanAndHandleVideos();
    });
})();