YouTube focus player

YouTube focus player when an arrow key is pressed(exclude"textarea")

  1. // ==UserScript==
  2. // @name YouTube focus player
  3. // @description YouTube focus player when an arrow key is pressed(exclude"textarea")
  4. // @name:zh-TW YT永遠聚焦在播放器
  5. // @description:zh-TW YT上下鍵強制聚焦在播放器(文字輸入區除外)
  6. // @version 2.0
  7. // @namespace https://greasyfork.org/zh-TW/users/4839
  8. // @author merkantilizm,hzhbest
  9. // @license MIT
  10. // @match https://www.youtube.com/*
  11. // @icon https://www.youtube.com/favicon.ico
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // Define video element
  19. var video;
  20.  
  21. // Add event listener for keydown
  22. window.addEventListener('keydown', function(e) {
  23. //文字輸入時無效
  24. if (checkTextArea(e.target)) return;
  25. // Check if arrow keys are pressed
  26. if (e.key === 'ArrowUp' || e.key === 'ArrowDown') {
  27.  
  28. // Get video element (has to be done inside of the function or it doesnt work for some reason)
  29. video = document.querySelector('video');
  30.  
  31. // Prevent default action of arrow keys (e.g., scrolling the page)
  32. e.preventDefault();
  33. // Focus the video player
  34. video.focus();
  35. }
  36. });
  37.  
  38. function checkTextArea(node) {
  39. var name = node.localName.toLowerCase();
  40. if (name == "textarea" || name == "input" || name == "select") {
  41. return true;
  42. }
  43. if (name == "div" && (node.id.toLowerCase().indexOf("textarea") != -1 || node.contentEditable !== false)) {
  44. return true;
  45. }
  46. return false;
  47. }
  48.  
  49. })();