YouTube Shorts Keyboard Nav

Brings back keyboard navigation for YouTube Shorts

  1. // ==UserScript==
  2. // @name YouTube Shorts Keyboard Nav
  3. // @namespace http://tampermonkey.net/
  4. // @version 2025-01-14
  5. // @description Brings back keyboard navigation for YouTube Shorts
  6. // @author x0a
  7. // @match https://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  9. // @grant none
  10. // @license mit
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15. const log = console.log.bind(globalThis, 'YSKN:')
  16. const getVideo = () => {
  17. const selector = '.html5-video-player.ytp-hide-controls.ytp-exp-bottom-control-flexbox.ytp-modern-caption.ytp-exp-ppp-update.ytp-hide-info-bar'
  18. + ':not(.ytp-autonav-endscreen-cancelled-state):not(.unstarted-mode) video';
  19. return document.querySelector(selector);
  20. }
  21. const inTextField = () => {
  22. const el = document.activeElement;
  23. if(!el) return false;
  24. if(el.nodeName === 'INPUT' || el.contentEditable === 'true') return true;
  25. return false;
  26. }
  27.  
  28.  
  29. const startMonitoring = () => {
  30. const onKeyUp = e => {
  31. if(location.pathname.slice(0, 8) !== '/shorts/') return;
  32. if(inTextField()) return;
  33.  
  34. const video = getVideo();
  35. if(!video) throw 'Video not found';
  36.  
  37. switch(event.key){
  38. case 'ArrowLeft':
  39. log('Going back 5 seconds');
  40. video.currentTime -= 5;
  41. break;
  42. case 'ArrowRight':
  43. log('Going forward 5 seconds');
  44. video.currentTime += 5;
  45. break;
  46. case 'j':
  47. log('Going forward 10 seconds');
  48. video.currentTime -= 10;
  49. break;
  50. case'l':
  51. log('Going forward 10 seconds');
  52. video.currentTime += 10;
  53. break;
  54. default:
  55. for(let i = 0; i < 10; i++){
  56. if(event.key === i + ''){
  57. const target = video.duration * (i / 10);
  58. log('Seeking to ' + i * 10 + '%, which is ' + target);
  59. video.currentTime = target;
  60. break;
  61. }
  62. }
  63. }
  64.  
  65. }
  66. window.addEventListener('keyup', onKeyUp);
  67.  
  68. return () => window.removeEventListener('keyup', onKeyUp);
  69.  
  70. }
  71.  
  72. const stopMonitoring = startMonitoring();
  73. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址