Youtube Music Logarithmic/Exponential volume

Makes the YouTube music volume slider logarithmic.

目前為 2021-05-19 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Youtube Music Logarithmic/Exponential volume
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Makes the YouTube music volume slider logarithmic. 
// @author       Andrew Rosiclair <git@arosiclair>
// @match        https://music.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

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

    const player = document.querySelector('ytmusic-player-bar');
    const volumeSlider = document.querySelector("#volume-slider");

    if (!player) {
        console.error("I failed you");
    }

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

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

        var minp = 0;
        var maxp = 100;

        // 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));
        return newVolume;
    }

    function manipulate () {
        const setVolume = player.playerApi_.setVolume;

        // if the player isn't ready yet try again
        if (!setVolume) {
            console.log("player wasn't ready, try again");
            setTimeout(manipulate, 10);
            return;
        }

        volumeSlider.style.width = sliderWidth;
        player.playerApi_.setVolume = function (volume) {
            const newVolume = useExpScale ? scaleExp(volume) : scaleLog(volume);
            console.log('manipulated volume', volume, newVolume.toFixed(2));
            return setVolume.call(this, newVolume);
        };
    }

    manipulate();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址