VK Volume Scroll

Shift + Scroll изменяет громкость плеера

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         VK Volume Scroll
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Shift + Scroll изменяет громкость плеера
// @author       wilovan
// @match        https://vk.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  function linearToVkVolume(linear) {
    return Math.pow(linear, 2.2);
  }

  function vkVolumeToLinear(vkVol) {
    return Math.pow(vkVol, 1 / 2.2);
  }

  let volumeLinear = 0;

  setTimeout(() => {
    try {
      const vkVol = getAudioPlayer()._userVolume ?? 0;
      volumeLinear = vkVolumeToLinear(vkVol);
      console.log('Начальная линейная громкость:', volumeLinear.toFixed(2));
    } catch {
      volumeLinear = 0;
      console.warn('Не удалось получить громкость, ставим 0');
    }
  }, 2000);

  document.addEventListener('wheel', function (e) {
    if (!e.shiftKey) return;

    const delta = e.deltaY < 0 ? 0.01 : -0.01; //0.01 шаг громкости - 1%
    volumeLinear = Math.min(1, Math.max(0, volumeLinear + delta));

    const vkVolToSet = linearToVkVolume(volumeLinear);

    try {
      getAudioPlayer().setVolume(vkVolToSet);
      console.log(`🔊 Громкость: ${volumeLinear.toFixed(2)} (VK: ${vkVolToSet.toFixed(3)})`);
    } catch (err) {
      console.error('❌ Ошибка при вызове getAudioPlayer().setVolume()', err);
    }

    e.preventDefault();
  }, { passive: false });
})();