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-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         YouTube Middle Mouse Button Mute / Unmute
// @author       NWP
// @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.1
// @license      MIT
// @match        *://*.youtube.com/watch*?*v=*
// @match        *://*.youtube.com/embed/*?*v=*
// @match        *://*.youtube.com/v/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function() {
    const videoArea = "video.video-stream.html5-main-video";
    const muteButton = ".ytp-mute-button";

    function handleMiddleMouseButtonDown(event) {
      if (event.button === 1 && event.target.matches(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);
      });
    }

    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(videoArea, (node) => {
      node.addEventListener('mousedown', handleMiddleMouseButtonDown);
    });
  })();

QingJ © 2025

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