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