YouTube Shorts Keyboard Nav

Brings back keyboard navigation for YouTube Shorts

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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();
})();