YouTube Seek 1 Second (Bracket Keys)

Press '[' to seek back 1 second, ']' to seek forward 1 second on YouTube (Uses bracket keys, common in other players)

// ==UserScript==
// @name         YouTube Seek 1 Second (Bracket Keys)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Press '[' to seek back 1 second, ']' to seek forward 1 second on YouTube (Uses bracket keys, common in other players)
// @author       You
// @match        https://www.youtube.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Define seek amount in seconds
    const SEEK_AMOUNT = 1;

    // Listen for keydown events on the document
    document.addEventListener('keydown', function(event) {
        // Check if the active element is an input field (e.g., search box, comments)
        const isInputActive = ['INPUT', 'TEXTAREA', 'SELECT'].includes(document.activeElement.tagName);

        // Handle Seek Backward ('[' key)
        if (event.key === '[' && !isInputActive) {
            event.preventDefault(); // Prevent default browser action for '[' (if any)
            seekVideo(-SEEK_AMOUNT); // Seek backward
        }
        // Handle Seek Forward (']' key)
        else if (event.key === ']' && !isInputActive) {
            event.preventDefault(); // Prevent default browser action for ']' (if any)
            seekVideo(SEEK_AMOUNT); // Seek forward
        }
    });

    // Function to perform the seek operation
    function seekVideo(direction) {
        const videoPlayer = document.querySelector('video');
        if (videoPlayer) {
            let currentTime = videoPlayer.currentTime;
            let newTime = currentTime + (direction * SEEK_AMOUNT);

            // Ensure the new time doesn't go below 0 or exceed the video duration
            if (newTime < 0) {
                newTime = 0;
            } else if (newTime > videoPlayer.duration) {
                newTime = videoPlayer.duration;
            }

            videoPlayer.currentTime = newTime;
            console.log(`Seeked ${direction > 0 ? 'forward' : 'backward'} ${SEEK_AMOUNT} second(s). New time: ${newTime.toFixed(2)}s`);
        } else {
            console.log('YouTube video player element not found.');
        }
    }

})();

(function() {
    'use strict';

    // Define seek amount in seconds
    const SEEK_AMOUNT = 1;

    // Listen for keydown events on the document
    document.addEventListener('keydown', function(event) {
        // Check if the active element is an input field (e.g., search box, comments)
        const isInputActive = ['INPUT', 'TEXTAREA', 'SELECT'].includes(document.activeElement.tagName);

        // Handle Seek Backward ('[' key)
        if (event.key === '[' && !isInputActive) {
            event.preventDefault(); // Prevent default browser action for '[' (if any)
            seekVideo(-SEEK_AMOUNT); // Seek backward
        }
        // Handle Seek Forward (']' key)
        else if (event.key === ']' && !isInputActive) {
            event.preventDefault(); // Prevent default browser action for ']' (if any)
            seekVideo(SEEK_AMOUNT); // Seek forward
        }
    });

    // Function to perform the seek operation
    function seekVideo(direction) {
        const videoPlayer = document.querySelector('video');
        if (videoPlayer) {
            let currentTime = videoPlayer.currentTime;
            let newTime = currentTime + (direction * SEEK_AMOUNT);

            // Ensure the new time doesn't go below 0 or exceed the video duration
            if (newTime < 0) {
                newTime = 0;
            } else if (newTime > videoPlayer.duration) {
                newTime = videoPlayer.duration;
            }

            videoPlayer.currentTime = newTime;
            console.log(`Seeked ${direction > 0 ? 'forward' : 'backward'} ${SEEK_AMOUNT} second(s). New time: ${newTime.toFixed(2)}s`);
        } else {
            console.log('YouTube video player element not found.');
        }
    }

})();