Twitch Middle Mouse Button Mute / Unmute

Mutes / unmutes a Twitch video by clicking the middle mouse button within the video player. While performing the middle mouse button click, the scroll button gets disabled. Mute / unmute / scroll disabling won't work with any elements floating over the video player.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitch Middle Mouse Button Mute / Unmute
// @author       NWP
// @description  Mutes / unmutes a Twitch video by clicking the middle mouse button within the video player. While performing the middle mouse button click, the scroll button gets disabled. Mute / unmute / scroll disabling won't work with any elements floating over the video player.
// @namespace    https://greasyfork.org/users/877912
// @version      0.1
// @license      MIT
// @match        *://*.twitch.tv/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    // div[data... is for when the controls are visible, video is for when the controls are hidden
    // due to https://greasyfork.org/en/scripts/501592-auto-hide-video-controls-almost-instantly-really-quickly-for-the-twitch-desktop-and-mobile-site
    // being enabled
    //
    // Normally, only div[data... is needed
    const videoArea = "div[data-a-target='player-overlay-click-handler'], video";
    const muteButton = "button[data-a-target='player-mute-unmute-button']";

    function handleMiddleMouseButtonDown(event) {
        if (event.button === 1 && event.target.closest(videoArea)) {
            event.preventDefault();
            const button = document.querySelector(muteButton);
            if (button) {
                button.click();
            }
        }
    }

    function addEventListenersToVideos() {
        const videos = document.querySelectorAll(videoArea);
        videos.forEach((video) => {
            video.removeEventListener('mousedown', handleMiddleMouseButtonDown);
            video.addEventListener('mousedown', handleMiddleMouseButtonDown);
        });
    }

    const observer = new MutationObserver((mutationsList) => {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList' && mutation.addedNodes.length) {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === 1) {
                        if (node.matches(videoArea) || node.querySelector(videoArea)) {
                            addEventListenersToVideos();
                        }
                    }
                });
            }
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

    addEventListenersToVideos();
})();