YouTube Volume Fix

Makes YouTube usable for listening to music :)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube Volume Fix
// @description  Makes YouTube usable for listening to music :)
// @author       kapinzzal02
// @license      MIT
// @match        https://www.youtube.com/*
// @version      1.0
// @run-at       document-start
// @namespace https://greasyfork.org/users/1419143
// ==/UserScript==

(() => {
  "use strict";

  const addStyle = () => {
      const style = document.createElement("style");
      style.textContent = `
        .rocket-indicator {
          position: relative;
          left: 5px;
          display: none;
          margin-right: 5px; /* Added margin for the gap */
        }
      `;
      document.head.appendChild(style);
  }

  const handleVideo = (video) => {
    const player = video.closest("#movie_player");
    if (!player) return;

    const volumeButton = player.querySelector(".ytp-mute-button");
    const volumePanel = player.querySelector(".ytp-volume-panel");
    if (!volumeButton || !volumePanel) return;

    const rocket = document.createElement("span");
    rocket.textContent = "🚀";
    rocket.classList.add("rocket-indicator");

    volumePanel.parentNode.insertBefore(rocket, volumePanel.nextSibling);

    const setVolumeFromPanel = () => {
      const desiredVolume = Number(volumePanel.getAttribute("aria-valuenow")) / 100;
      if (video.volume !== desiredVolume) {
        video.volume = desiredVolume;
      }
    };

    const volumePanelObserver = new MutationObserver(setVolumeFromPanel);
    volumePanelObserver.observe(volumePanel, { attributeFilter: ["aria-valuenow"] });

    const videoVolumeObserver = new MutationObserver(setVolumeFromPanel);
    videoVolumeObserver.observe(video, { attributes: true, attributeFilter: ["volume"] });

    setVolumeFromPanel();

    volumeButton.addEventListener("mouseover", () => {
      rocket.style.display = "inline-block";
    });

    volumeButton.addEventListener("mouseout", () => {
      rocket.style.display = "none";
    });
  };

  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      mutation.addedNodes.forEach((node) => {
        if (node.nodeName === "VIDEO") {
          handleVideo(node);
        }
      });
    });
  });

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

  document.querySelectorAll("video").forEach(handleVideo);
  addStyle();
})();