点击静音

点击静音、网页静音、视频静音

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         点击静音
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  点击静音、网页静音、视频静音
// @author       你
// @match        https://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function generateUniqueId() {
        return 'mute-button-' + Math.random().toString(36).substr(2, 9);
    }

    if (document.getElementById('mute-button-container')) {
        return; // 页面上已经有按钮,直接退出
    }

    const buttonContainer = document.createElement('div');
    buttonContainer.id = 'mute-button-container';
    buttonContainer.style.position = 'fixed';
    buttonContainer.style.top = '10px';
    buttonContainer.style.right = '10px';
    buttonContainer.style.zIndex = '9999';

    const button = document.createElement('button');
    button.id = generateUniqueId(); 
    button.innerText = '点击静音';
    button.style.backgroundColor = '#ff5733';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '20px';
    button.style.padding = '10px 20px';
    button.style.cursor = 'pointer';
    button.style.fontSize = '14px';
    buttonContainer.appendChild(button);

    document.body.appendChild(buttonContainer);

    let isMuted = false;

    // 静音功能
    function mutePage() {
        isMuted = true;
        button.innerText = '点击恢复';
        document.querySelectorAll('audio, video').forEach(el => el.muted = true);
    }

    function unmutePage() {
        isMuted = false;
        button.innerText = '点击静音';
        document.querySelectorAll('audio, video').forEach(el => el.muted = false);
    }

    button.addEventListener('click', function() {
        if (isMuted) {
            unmutePage();
        } else {
            mutePage();
        }
    });

    function checkMutedStatus() {
        const videos = document.querySelectorAll('audio, video');
        if (videos.length > 0) {
            const anyMuted = Array.from(videos).some(v => v.muted);
            if (anyMuted) {
                isMuted = true;
                button.innerText = '点击恢复';
            } else {
                isMuted = false;
                button.innerText = '点击静音';
            }
        }
    }

    setInterval(checkMutedStatus, 1000);

})();