Youtube Video Max Volume Patch

Override the html5 video volume to 1 when player's volume slider is set to 100%.

  1. // ==UserScript==
  2. // @name Youtube Video Max Volume Patch
  3. // @namespace http://github.com/jaytohe/
  4. // @version 0.2
  5. // @description Override the html5 video volume to 1 when player's volume slider is set to 100%.
  6. // @author jaytohe
  7. // @license MIT
  8. // @match https://www.youtube.com/*
  9. // @run-at document-idle
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const curr_volume = () => document.querySelector('div[class^="ytp-volume-panel"]').getAttribute('aria-valuenow');
  17. const get_video = () => document.querySelector('video[src]'); //video[src] is used to prevent accidental firing on bogus video in homepage
  18.  
  19. const forceMaxVol = (event) => {
  20. if (curr_volume() === '100') {
  21. event.target.volume = 1;
  22. }
  23.  
  24. }
  25. const initHijack = (video) => {
  26. if (video !== null && curr_volume() !== null) {
  27. console.log('Patching max volume...');
  28.  
  29. if (curr_volume() === '100') //in case video autoplays
  30. video.volume = 1;
  31.  
  32. if (video.onplay === null)
  33. video.onplay = forceMaxVol;
  34.  
  35. if(video.onvolumechange === null)
  36. video.onvolumechange = forceMaxVol;
  37. }
  38. }
  39. const wait_for_video = setInterval(function() {
  40. if (get_video() !== null) {
  41. console.log('[YOTUBE MAX VOLUME PATCH] -- FOUND VIDEO.');
  42. setTimeout(()=>initHijack(get_video()), 1000);
  43. clearInterval(wait_for_video);
  44. }
  45. }, 500);
  46. })();