YouTube Fix Arrow Key Volume Control

Focus player when an arrow key is pressed

  1. // ==UserScript==
  2. // @name YouTube Fix Arrow Key Volume Control
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Focus player when an arrow key is pressed
  6. // @author merkantilizm
  7. // @license MIT
  8. // @match https://www.youtube.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Define video element
  16. var video;
  17. // Add event listener for keydown
  18. window.addEventListener('keydown', function(e) {
  19. // Check if arrow keys are pressed
  20. if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
  21. // Get video element (has to be done inside of the function or it doesnt work for some reason)
  22. video = document.querySelector('video');
  23. // Prevent default action of arrow keys (e.g., scrolling the page)
  24. e.preventDefault();
  25. // Focus the video player
  26. video.focus();
  27. }
  28. });
  29. })();