Greasy Fork

Greasy Fork is available in English.

小红书Pro

小红书网页端:屏蔽登录弹窗+视频自动取消静音.

当前为 2025-05-10 提交的版本,查看 最新版本

// ==UserScript==
// @name         小红书Pro
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  小红书网页端:屏蔽登录弹窗+视频自动取消静音.
// @match        *://www.xiaohongshu.com/*
// @icon         https://www.xiaohongshu.com/favicon.ico
// @run-at       document-start
// @grant        none
// @license      MIT
// ==/UserScript==


(function() {
    // ========== 屏蔽登录弹窗 ==========
    function injectLoginBlockStyle() {
        if (!document.head) return false;
        // 防止多次注入同一样式
        if (document.getElementById('xhs-login-style-block')) return true;
        let style = document.createElement('style');
        style.id = 'xhs-login-style-block';
        style.textContent = `.login-container{display:none!important;}`;
        document.head.appendChild(style);
        return true;
    }
    function clickLoginCloseBtn() {
        let $close = document.querySelector('.login-container .icon-btn-wrapper');
        if ($close) $close.click();
    }
    function setupLoginBlockObserver() {
        // 只启动一次
        if (window.__xhsLoginBlockObserver) return;
        window.__xhsLoginBlockObserver = true;
        const mo = new MutationObserver(() => {
            injectLoginBlockStyle();
            clickLoginCloseBtn();
        });
        mo.observe(document.body, {subtree: true, childList: true});
        // 初始化执行一次
        injectLoginBlockStyle();
        clickLoginCloseBtn();
    }
    // 保证head和body都加载完再注入
    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(setupLoginBlockObserver);

    // ========== 自动取消视频静音 ==========
    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 });
    }
    waitForHeadAndBody(setupVideoObserver);

    // 保底,页面完全加载时再执行一遍(抗SPA/异步加载)
    window.addEventListener('DOMContentLoaded', () => {
        injectLoginBlockStyle();
        clickLoginCloseBtn();
        scanAndHandleVideos();
    });

})();