Greasy Fork

Greasy Fork is available in English.

静音控制按钮

给页面加一个静音控制按钮,支持静音和恢复功能,并确保每页仅显示一个按钮

当前为 2024-12-28 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         静音控制按钮
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  给页面加一个静音控制按钮,支持静音和恢复功能,并确保每页仅显示一个按钮
// @author       你
// @match        https://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 生成唯一的ID,避免重复
    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(); // 为按钮设置唯一ID
    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);

})();