YouTube Middle Mouse Button Mute / Unmute

Mutes / unmutes a YouTube 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.

目前為 2024-07-29 提交的版本,檢視 最新版本

// ==UserScript==
// @name         YouTube Middle Mouse Button Mute / Unmute
// @description  Mutes / unmutes a YouTube 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://gf.qytechs.cn/users/877912
// @version      0.2
// @license      MIT
// @match        *://*.youtube.com/watch*?*v=*
// @match        *://*.youtube.com/embed/*?*v=*
// @match        *://*.youtube.com/v/*
// @match        *://*.youtube.com/shorts/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    const videoAreaRegularAndShorts = "video.video-stream.html5-main-video";
    const videoAreaRegularBlackBars = "div.ytp-player-content.ytp-iv-player-content[data-layer='4']";
    const muteButtonRegular = ".ytp-mute-button";
    const muteButtonShorts = "button.YtdDesktopShortsVolumeControlsMuteIconButton";

    function handleMiddleMouseButtonDown(event) {
      if (event.button === 1) {
        let button = null;
        if (event.target.matches(videoAreaRegularAndShorts)) {
          event.preventDefault();
          if (window.location.href.includes("shorts")) {
            button = document.querySelector(muteButtonShorts);
          } else {
            button = document.querySelector(muteButtonRegular);
          }
        } else if (event.target.matches(videoAreaRegularBlackBars)) {
          event.preventDefault();
          button = document.querySelector(muteButtonRegular);
        }
        if (button) {
          button.click();
        }
      }
    }

    function addEventListenersToVideos() {
      const elements = document.querySelectorAll(`${videoAreaRegularAndShorts}, ${videoAreaRegularBlackBars}`);
      elements.forEach((element) => {
        element.removeEventListener('mousedown', handleMiddleMouseButtonDown);
        element.addEventListener('mousedown', handleMiddleMouseButtonDown);
      });
    }

    function waitForKeyElements(selector, actionFunction) {
      const observer = new MutationObserver((mutationsList) => {
        for (const mutation of mutationsList) {
          if (mutation.type === 'childList' && mutation.addedNodes.length) {
            for (const node of mutation.addedNodes) {
              if (node.matches && node.matches(selector)) {
                actionFunction(node);
              }
            }
          }
        }
      });

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

    waitForKeyElements(`${videoAreaRegularAndShorts}, ${videoAreaRegularBlackBars}`, (node) => {
      node.addEventListener('mousedown', handleMiddleMouseButtonDown);
    });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址