Youtube Music Logarithmic/Exponential volume

Makes the YouTube music volume slider logarithmic.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();