Youtube Scroll Volume

Hold Right-Click and Scroll to Adjust Volume on Youtube. (MADE WITH CHATGPT)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Scroll Volume
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Hold Right-Click and Scroll to Adjust Volume on Youtube. (MADE WITH CHATGPT)
// @author       YIKIKRULES (or ChatGPT)
// @match        *://www.youtube.com/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let rightClickHeld = false;
    let volumeTimeout;

    // Function to adjust volume on mouse wheel scroll
    function adjustVolume(event) {
        if (rightClickHeld) {
            const videoPlayer = document.querySelector('.html5-video-player');
            if (videoPlayer) {
                const currentVolume = videoPlayer.getVolume(); // YouTube API volume range is 0-100
                const delta = event.deltaY;

                // Adjust volume in the YouTube player (range 0-100)
                let newVolume = currentVolume + (delta < 0 ? 1 : -1);
                newVolume = Math.max(0, Math.min(100, newVolume));
                videoPlayer.setVolume(newVolume);

                event.preventDefault(); // Prevent default scroll behavior

                // Show volume indicator
                showVolumeIndicator(newVolume);
            }
        }
    }

    function showVolumeIndicator(volume) {
        const player = document.querySelector(".html5-video-player");
        if (!player) return;

        let existingIndicator = document.querySelector("#customVolumeIndicator");
        if (existingIndicator) {
            clearTimeout(volumeTimeout);
            existingIndicator.remove();
        }

        let volumeIndicator = document.createElement("div");
        volumeIndicator.id = "customVolumeIndicator";
        volumeIndicator.textContent = `${volume}%`;
        volumeIndicator.style.cssText = `
            position: absolute;
            top: 15%;
            left: 50%;
            transform: translateX(-50%);
            padding: 10px 20px;
            font-size: 18px;
            font-weight: bold;
            background: rgba(0, 0, 0, 0.7);
            color: white;
            border-radius: 8px;
            z-index: 9999;
            pointer-events: none;
        `;

        player.appendChild(volumeIndicator);

        volumeTimeout = setTimeout(() => {
            volumeIndicator.remove();
        }, 2000);
    }

    // Listen for mouse down event to detect right-click
    window.addEventListener('mousedown', function(event) {
        if (event.button === 2) { // Right mouse button
            rightClickHeld = true;
        }
    });

    // Listen for mouse up event to stop adjusting volume when right-click is released
    window.addEventListener('mouseup', function(event) {
        if (event.button === 2) { // Right mouse button
            rightClickHeld = false;
        }
    });

    // Attach event listener to the page to track scroll wheel events
    window.addEventListener('wheel', function(event) {
        if (event.target.tagName.toLowerCase() === 'video') {
            adjustVolume(event);
        }
    }, { passive: false });

})();