YouTube Shorts Keyboard Nav

Brings back keyboard navigation for YouTube Shorts

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         YouTube Shorts Keyboard Nav
// @namespace    http://tampermonkey.net/
// @version      2025-01-14
// @description  Brings back keyboard navigation for YouTube Shorts
// @author       x0a
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license      mit
// ==/UserScript==

(function() {
    'use strict';
    const log = console.log.bind(globalThis, 'YSKN:')
    const getVideo = () => {
        const selector = '.html5-video-player.ytp-hide-controls.ytp-exp-bottom-control-flexbox.ytp-modern-caption.ytp-exp-ppp-update.ytp-hide-info-bar'
        + ':not(.ytp-autonav-endscreen-cancelled-state):not(.unstarted-mode) video';
        return document.querySelector(selector);
    }
    const inTextField = () => {
        const el = document.activeElement;
        if(!el) return false;
        if(el.nodeName === 'INPUT' || el.contentEditable === 'true') return true;
        return false;
    }


    const startMonitoring = () => {
        const onKeyUp = e => {
            if(location.pathname.slice(0, 8) !== '/shorts/') return;
            if(inTextField()) return;

            const video = getVideo();
            if(!video) throw 'Video not found';

            switch(event.key){
                case 'ArrowLeft':
                    log('Going back 5 seconds');
                    video.currentTime -= 5;
                    break;
                case 'ArrowRight':
                    log('Going forward 5 seconds');
                    video.currentTime += 5;
                    break;
                case 'j':
                    log('Going forward 10 seconds');
                    video.currentTime -= 10;
                    break;
                case'l':
                    log('Going forward 10 seconds');
                    video.currentTime += 10;
                    break;
                default:
                    for(let i = 0; i < 10; i++){
                        if(event.key === i + ''){
                            const target = video.duration * (i / 10);
                            log('Seeking to ' + i * 10 + '%, which is ' + target);
                            video.currentTime = target;
                            break;
                        }
                    }
            }

        }
        window.addEventListener('keyup', onKeyUp);

        return () => window.removeEventListener('keyup', onKeyUp);

    }

    const stopMonitoring = startMonitoring();
})();