Youtube Music fix volume ratio

Makes the YouTube music volume slider exponential so it's easier to select lower volumes.

目前為 2021-03-25 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Music fix volume ratio
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Makes the YouTube music volume slider exponential so it's easier to select lower volumes.
// @author       Marco Pfeiffer <[email protected]>
// @match        https://music.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const EXPONENT = 1.8; // manipulation exponent, higher value = lower volume

    tryManipulate(100);

    function tryManipulate(retries) {
        const success = manipulate();
        if (!success && retries > 0) {
            setTimeout(() => tryManipulate(retries - 1), 10);
        }
    }

    function manipulate () {
        // if the element wasn't found, try again
        const player = document.querySelector('ytmusic-player-bar');
        if (!player) {
            return false;
        }

        // if the player isn't ready yet, try again
        const {setVolume, getVolume} = player.playerApi_;
        if (!setVolume || !getVolume) {
            return false;
        }

        player.playerApi_.setVolume = function (volume) {
            const newVolume = Math.ceil((volume / 100) ** EXPONENT * 100);
            console.log('manipulated setVolume to  ', newVolume, 'from', volume);
            return setVolume.call(this, newVolume);
        };

        player.playerApi_.getVolume = function () {
            const volume = getVolume.call(this);
            const newVolume = Math.floor((volume / 100) ** (1 / EXPONENT) * 100);
            console.log('manipulated getVolume from', volume, 'to  ', newVolume);
            return newVolume;
        };

        return true;
    }
})();