Greasy Fork

Greasy Fork is available in English.

Youtube Music fix volume ratio

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

当前为 2021-09-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         Youtube Music fix volume ratio
// @namespace    http://tampermonkey.net/
// @version      0.4
// @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';

    // manipulation exponent, higher value = lower volume
    // 3 is the value used by pulseaudio, which Barteks2x figured out this gist here: https://gist.github.com/Barteks2x/a4e189a36a10c159bb1644ffca21c02a
    // 0.05 (or 5%) is the lowest you can select in the UI which with an exponent of 3 becomes 0.000125 or 0.0125%
    const EXPONENT = 3;

    const storedVolumes = new WeakMap();
    const {get, set} = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, 'volume');
    Object.defineProperty(HTMLMediaElement.prototype, 'volume', {
        get () {
            const volume = get.call(this);
            const storedVolume = storedVolumes.get(this);
            const calculatedVolume = volume ** (1 / EXPONENT);
            const isStoredAccurateEnough = Math.abs(storedVolume - calculatedVolume) < 0.1; // undefined === NaN ; NaN < 0.01 === false
            const newVolume = isStoredAccurateEnough ? storedVolume : calculatedVolume;
            // console.log('manipulated volume from', volume, 'to  ', newVolume, 'on', this);
            return newVolume;
        },
        set (volume) {
            const newVolume = volume ** EXPONENT;
            storedVolumes.set(this, volume);
            // console.log('manipulated volume to  ', newVolume, 'from', volume, 'on', this);
            return set.call(this, newVolume);
        }
    });
})();