Boot.dev Keyboard Control for Video

Adds basic keyboard controls for video playback for the videos in boot.dev

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Boot.dev Keyboard Control for Video
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds basic keyboard controls for video playback for the videos in boot.dev
// @author       NymDev
// @match        https://*.boot.dev/*
// @icon         https://media.licdn.com/dms/image/v2/D560BAQGLKuZWsexDdQ/company-logo_200_200/B56Zrxve7ELIAI-/0/1764992348316/bootdotdev_logo?e=2147483647&v=beta&t=_ixDnPg_rbRO8b2tFEJ5B_JjNCBiyBJRvUtZv98y7KQ
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let video_element_focus = false;

    document.addEventListener('click', (event) => {
        const clickedElement = event.target;
        // If you clicked a video control button or icon, immediately remove focus from it
        if (clickedElement.closest('button') || clickedElement.closest('.video-poster')) {
            document.activeElement.blur();
        }

        if (clickedElement.closest('.video-poster') || clickedElement.closest('video')) {
            video_element_focus = true;
        } else {
            video_element_focus = false;
        }
    });

    document.addEventListener('keydown', (event) => {
        if (video_element_focus === true) {
            // Removed TypeScript type syntax so browser JavaScript can run it safely
            const video_element = document.querySelector(".h-full.video-poster");

            // If the element doesn't exist on this part of Boot.dev, step out safely
            if (!video_element) return;

            // Convert the key to lowercase for matching
            switch (event.key.toLowerCase()) {
                case 'f':
                    if (document.fullscreenElement) {
                        document.exitFullscreen();
                    } else {
                        video_element.requestFullscreen().catch(err => console.warn(err));
                    }
                    break;

                case 'arrowright':
                    video_element.currentTime += 5;
                    break;

                case 'arrowleft':
                    video_element.currentTime -= 5;
                    break;

                case ' ':
                    event.preventDefault(); // Stop spacebar from scrolling down your page
                    if (video_element.paused) {
                        video_element.play();
                    } else {
                        video_element.pause();
                    }
                    break;

                case 'arrowup':
                    event.preventDefault(); // Stop up arrow from scrolling page
                    // Volume values range strictly between 0.0 and 1.0 (0.1 represents 10%)
                    video_element.volume = Math.min(1, video_element.volume + 0.1);
                    break;

                case 'arrowdown':
                    event.preventDefault(); // Stop down arrow from scrolling page
                    video_element.volume = Math.max(0, video_element.volume - 0.1);
                    break;

                default:
                    break;
            }
        }
    });
})();