YouTube Volume Booster

Custom volume slider for YouTube videos with increased length and precise control

  1. // ==UserScript==
  2. // @name YouTube Volume Booster
  3. // @namespace https://github.com/ToLIManl
  4. // @version 0.6
  5. // @description:en Custom volume slider for YouTube videos with increased length and precise control
  6. // @description:ru Пользовательский ползунок громкости для видео YouTube с увеличенной длиной и точным контролем.
  7. // @description Custom volume slider for YouTube videos with increased length and precise control
  8. // @author ToLIMan
  9. // @match https://www.youtube.com/*
  10. // @grant none
  11. // @license MIT
  12. // @name:ru Увелечитель громкости Ютуб
  13. // @name:en Youtube Volume Booster
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. // Function to calculate the volume based on slider position
  20. function calculateVolume(position, sliderWidth) {
  21. // Calculate volume percentage based on slider position
  22. const volume = (position / sliderWidth) * 1400; // Increased maximum volume
  23. // Return the volume percentage with 3 decimal places
  24. return volume.toFixed(3);
  25. }
  26.  
  27. // Function to update the volume display at the center of the screen
  28. function updateVolumeDisplay(volume) {
  29. // Create a div for the volume display
  30. const volumeDisplay = document.createElement('div');
  31. volumeDisplay.id = 'customVolumeDisplay';
  32. volumeDisplay.style.position = 'fixed';
  33. volumeDisplay.style.top = '50%';
  34. volumeDisplay.style.left = '50%';
  35. volumeDisplay.style.transform = 'translate(-50%, -50%)';
  36. volumeDisplay.style.background = 'rgba(0, 0, 0, 0.7)';
  37. volumeDisplay.style.color = '#fff';
  38. volumeDisplay.style.padding = '10px';
  39. volumeDisplay.style.borderRadius = '5px';
  40. volumeDisplay.style.zIndex = '9999';
  41. volumeDisplay.innerText = `Volume: ${volume}%`;
  42.  
  43. // Append the volume display to the body
  44. document.body.appendChild(volumeDisplay);
  45.  
  46. // Remove the volume display after 1 second
  47. setTimeout(() => {
  48. volumeDisplay.remove();
  49. }, 1000);
  50. }
  51.  
  52. // Wait for the page to fully load
  53. window.addEventListener('load', function() {
  54. // Find the YouTube video player
  55. const videoPlayer = document.querySelector('.video-stream');
  56.  
  57. // Create a custom volume slider
  58. const customVolumeSlider = document.createElement('input');
  59. customVolumeSlider.type = 'range';
  60. customVolumeSlider.min = '0';
  61. customVolumeSlider.max = '1400'; // Increased maximum volume
  62. customVolumeSlider.step = '1';
  63. customVolumeSlider.style.width = '700px'; // Set the width
  64. customVolumeSlider.style.transform = 'scaleX(-1)'; // Flip the slider horizontally
  65. customVolumeSlider.style.display = 'none'; // Hide initially
  66.  
  67. // Create an AudioContext once on page load
  68. const audioContext = new (window.AudioContext || window.webkitAudioContext)();
  69. const gainNode = audioContext.createGain();
  70. gainNode.connect(audioContext.destination);
  71.  
  72. // Connect the video player to the gain node
  73. const videoSource = audioContext.createMediaElementSource(videoPlayer);
  74. videoSource.connect(gainNode);
  75.  
  76. // Add an event listener for the slider input
  77. customVolumeSlider.addEventListener('input', function() {
  78. // Calculate the volume based on the slider position
  79. const volume = calculateVolume(this.value, this.max);
  80.  
  81. // Update the gain node's gain value
  82. gainNode.gain.value = volume / 100;
  83.  
  84. // Update the volume display at the center of the screen
  85. updateVolumeDisplay(volume);
  86. });
  87.  
  88. // Add an event listener to reset the slider when a new video is opened
  89. videoPlayer.addEventListener('timeupdate', function() {
  90. if (videoPlayer.currentTime === 0) {
  91. resetCustomVolumeSlider();
  92. }
  93. });
  94.  
  95. // Function to reset the custom volume slider
  96. function resetCustomVolumeSlider() {
  97. customVolumeSlider.value = '100'; // Set default value to 100%
  98. const initialVolume = calculateVolume(100, customVolumeSlider.max);
  99. gainNode.gain.value = initialVolume / 100;
  100. updateVolumeDisplay(initialVolume);
  101. }
  102.  
  103. // Function to toggle the visibility of the custom volume slider
  104. function toggleCustomVolumeSlider() {
  105. const isSliderHidden = customVolumeSlider.style.display === 'none';
  106. customVolumeSlider.style.display = isSliderHidden ? 'block' : 'none';
  107. }
  108.  
  109. // Create a custom button for volume boost
  110. const volumeBoostButton = document.createElement('button');
  111. volumeBoostButton.id = 'volumeBoostButton';
  112. volumeBoostButton.style.background = 'none';
  113. volumeBoostButton.style.border = 'none';
  114. volumeBoostButton.style.cursor = 'pointer';
  115. volumeBoostButton.style.marginRight = '10px';
  116. volumeBoostButton.innerText = 'Volume Boost';
  117. volumeBoostButton.style.color = '#fff';
  118. volumeBoostButton.style.fontWeight = 'bold';
  119.  
  120. // Add an event listener to toggle the slider when the volume boost button is clicked
  121. volumeBoostButton.addEventListener('click', function() {
  122. toggleCustomVolumeSlider();
  123. resetCustomVolumeSlider();
  124. });
  125.  
  126. // Insert the custom volume slider and button into the YouTube video player controls
  127. const controls = document.querySelector('.ytp-chrome-controls');
  128. if (controls) {
  129. controls.insertBefore(volumeBoostButton, controls.firstChild);
  130. controls.appendChild(customVolumeSlider);
  131. }
  132.  
  133. // Reset the custom volume slider on page load
  134. resetCustomVolumeSlider();
  135. });
  136. })();