Youtube Music Logarithmic/Exponential volume

Makes the YouTube music volume slider logarithmic.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Music Logarithmic/Exponential volume
// @namespace    http://tampermonkey.net/
// @version      0.3.3
// @description  Makes the YouTube music volume slider logarithmic.
// @author       Andrew Rosiclair <git@arosiclair>
// @match        https://music.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const useExpScale = false; // set to true for exponential scaling
    const expScalePower = 2;
    const sliderWidth = '200px'; // default: 100px

    function scaleExp (volume) {
        const newVolume = (volume / 1) ** expScalePower;
        return newVolume;
    }

    function scaleLog (volume) {
        if (volume === 0) {
            return 0;
        }

        var minp = 0;
        var maxp = 1;

        // The result should be between 1 an 100
        var minv = Math.log(1);
        var maxv = Math.log(100);

        // calculate adjustment factor
        var scale = (maxv-minv) / (maxp-minp);

        const newVolume = Math.exp(minv + scale*(volume-minp)) / 100;
        return newVolume;
    }

    const {get, set} = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype, 'volume');

    setTimeout(() => {
        Object.defineProperty(HTMLMediaElement.prototype, 'volume', {
            get () {
                const volume = get.call(this);
                const newVolume = useExpScale ? scaleExp(volume) : scaleLog(volume);
                console.log(`[YouTube Music Volume Fix] volume: ${volume} newVolume ${newVolume.toFixed(2)}`);
                return newVolume;
            },
            set (volume) {
                const newVolume = useExpScale ? scaleExp(volume) : scaleLog(volume);
                console.log(`[YouTube Music Volume Fix] volume: ${volume} newVolume ${newVolume.toFixed(2)}`);
                return set.call(this, newVolume);
            }
        });

        const volumeSlider = document.querySelector("#volume-slider");
        if (volumeSlider) {
            volumeSlider.style.width = sliderWidth;
        } else {
            console.error("[YouTube Music Volume Fix] couldn't find the slider element");
        }

        console.log(`[YouTube Music Volume Fix] initialized`);
    }, 1000);
})();