YouTube Volume Control with Memory

Set YouTube volume manually on a scale of 1-100, remember last set volume, and inject the UI to the left of the volume slider on the video player. Syncs the slider, disables invalid inputs, and adds debugging.

目前為 2024-10-30 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name YouTube Volume Control with Memory
  3. // @namespace https://github.com/Nick2bad4u/UserStyles
  4. // @version 4.0
  5. // @description Set YouTube volume manually on a scale of 1-100, remember last set volume, and inject the UI to the left of the volume slider on the video player. Syncs the slider, disables invalid inputs, and adds debugging.
  6. // @author Nick2bad4u
  7. // @match *://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant GM.getValue
  10. // @grant GM.setValue
  11. // @license UnLicense
  12. // ==/UserScript==
  13.  
  14. (async function () {
  15. 'use strict';
  16. // Default volume if none is saved
  17. let previousVolume = await GM.getValue('youtubeVolume', 50);
  18. // Create input element for volume control
  19. const volumeInput = document.createElement('input');
  20. volumeInput.type = 'number';
  21. volumeInput.min = 0;
  22. volumeInput.max = 100;
  23. volumeInput.value = previousVolume;
  24. // Set input field styles to resemble YouTube's UI
  25. volumeInput.style.width = '30px';
  26. volumeInput.style.marginRight = '10px';
  27. volumeInput.style.backgroundColor = 'rgba(255, 255, 255, 0.0)';
  28. volumeInput.style.color = 'white';
  29. volumeInput.style.border = '0px solid rgba(255, 255, 255, 0.0)';
  30. volumeInput.style.borderRadius = '4px';
  31. volumeInput.style.zIndex = 9999;
  32. volumeInput.style.height = '24px';
  33. volumeInput.style.fontSize = '16px';
  34. volumeInput.style.padding = '0 4px';
  35. volumeInput.style.transition = 'border-color 0.3s, background-color 0.3s';
  36. volumeInput.style.outline = 'none';
  37. volumeInput.style.position = 'relative';
  38. volumeInput.style.top = '13px';
  39. // Change border color on focus
  40. volumeInput.addEventListener('focus', () => {
  41. volumeInput.style.borderColor = 'rgba(255, 255, 255, 0.6)';
  42. });
  43. volumeInput.addEventListener('blur', () => {
  44. volumeInput.style.borderColor = 'rgba(255, 255, 255, 0.3)';
  45. });
  46. // Change background color on hover
  47. volumeInput.addEventListener('mouseenter', () => {
  48. volumeInput.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
  49. });
  50. volumeInput.addEventListener('mouseleave', () => {
  51. volumeInput.style.backgroundColor = 'rgba(255, 255, 255, 0.0)';
  52. });
  53. // Prevent YouTube hotkeys when typing in the input
  54. volumeInput.addEventListener('keydown', function (event) {
  55. event.stopPropagation();
  56. console.log('Keydown event in volume input, stopping propagation.');
  57. });
  58. // Function to set the volume based on input value
  59. async function setVolume(volumeValue) {
  60. const player = document.querySelector('video');
  61. if (player) {
  62. // Validate input (must be between 0 and 100)
  63. if (volumeValue < 0) volumeValue = 0;
  64. if (volumeValue > 100) volumeValue = 100;
  65. volumeInput.value = volumeValue;
  66. // Set the player volume and save to Tampermonkey storage
  67. player.volume = volumeValue / 100;
  68. await GM.setValue('youtubeVolume', volumeValue);
  69. console.log(`Volume set to ${volumeValue} and saved to Tampermonkey storage.`);
  70. // Sync YouTube's volume slider UI
  71. const volumeSlider = document.querySelector('.ytp-volume-slider-handle');
  72. if (volumeSlider) {
  73. volumeSlider.style.left = `${volumeValue}%`;
  74. console.log('YouTube volume slider updated.');
  75. }
  76. }
  77. }
  78. // Event listener for input change (manually changing the volume in the input box)
  79. volumeInput.addEventListener('input', () => setVolume(volumeInput.value));
  80. // Function to update the input field when YouTube's player volume is changed
  81. async function updateVolumeInput() {
  82. const player = document.querySelector('video');
  83. if (player) {
  84. const currentVolume = Math.round(player.volume * 100);
  85. volumeInput.value = currentVolume;
  86. await GM.setValue('youtubeVolume', currentVolume);
  87. console.log(`Volume input updated to ${currentVolume} from video player.`);
  88. // Show 0 if the video is muted
  89. if (player.muted) {
  90. volumeInput.value = 0;
  91. }
  92. }
  93. }
  94. // Function to handle mute changes
  95. async function handleMuteChange() {
  96. const player = document.querySelector('video');
  97. if (player) {
  98. if (player.muted) {
  99. volumeInput.value = 0; // Show 0 when muted
  100. } else {
  101. volumeInput.value = previousVolume; // Restore previous volume when unmuted
  102. player.volume = previousVolume / 100; // Set the player volume back to previous
  103. }
  104. console.log(`Mute state changed: muted = ${player.muted}`);
  105. }
  106. }
  107. // Inject the input box into YouTube's control bar
  108. function injectVolumeControl() {
  109. const volumeSliderPanel = document.querySelector('.ytp-volume-panel');
  110. if (volumeSliderPanel) {
  111. volumeSliderPanel.parentNode.insertBefore(volumeInput, volumeSliderPanel);
  112. setVolume(previousVolume); // Set initial volume
  113. const player = document.querySelector('video');
  114. if (player) {
  115. player.addEventListener('volumechange', updateVolumeInput);
  116. player.addEventListener('mute', handleMuteChange);
  117. player.addEventListener('unmute', handleMuteChange);
  118. console.log('Volume input injected and event listeners attached.');
  119. }
  120. } else {
  121. console.log('Volume panel not found, retrying...');
  122. setTimeout(injectVolumeControl, 500);
  123. }
  124. }
  125. // Inject the volume control when the page is ready
  126. injectVolumeControl();
  127. })();