Control YouTube videos correctly using arrow keys

Control video volume and seek with arrow keys

当前为 2024-08-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Control YouTube videos correctly using arrow keys
// @name:ru      Правильное управление видео на YouTube с помощью клавиш со стрелками
// @namespace    http://tampermonkey.net/
// @license      MIT
// @version      0.9
// @description  Control video volume and seek with arrow keys
// @description:ru Управляйте громкостью видео и поиском с помощью клавиш со стрелками
// @author       Boss of this gym
// @match        www.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to find the first video element on the page
    function getVideoElement() {
        return document.querySelector('video');
    }

    // Function to change volume
    function changeVolume(video, delta) {
        if (video) {
            video.volume = Math.min(Math.max(video.volume + delta, 0), 1);
        }
    }

    // Function to seek video
    function seekVideo(video, time) {
        if (video) {
            video.currentTime = Math.min(Math.max(video.currentTime + time, 0), video.duration);
        }
    }

    // Function to check if the active element is an input or textarea inside a YouTube comment or search form
    function isElementInYouTubeCommentOrSearch(target) {
        if (!target) return false;

        const isInput = target.tagName === 'INPUT' || target.tagName === 'TEXTAREA';
        const isSearchBox = isInput && target.closest('form') && target.closest('form').id === 'search-form';
        const isCommentBox = target.closest('ytd-commentbox') !== null;
        const isCommentReplyButton = target.closest('ytd-comment-replies-renderer') !== null;
        const isCommentActionButton = target.closest('ytd-comment-action-buttons-renderer') !== null;

        return isSearchBox || isCommentBox || isCommentReplyButton || isCommentActionButton;
    }

    // Function to skip content warning
    function skipContentWarning() {
        const button = document.querySelector('button#confirm-button'); // Selector for the "I understand and wish to proceed" button
        if (button) {
            button.click();
        }
    }

    // Periodically check for the content warning dialog
    setInterval(skipContentWarning, 1000);

    // Add event listener for keydown event
    document.addEventListener('keydown', function(event) {
        const video = getVideoElement();
        if (!video) return;

        // Check if the focused element is part of YouTube comments or search
        if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key) && !isElementInYouTubeCommentOrSearch(document.activeElement)) {
            event.preventDefault();

            // Force focus on the video element
            video.focus();

            switch(event.key) {
                case 'ArrowUp':
                    changeVolume(video, 0.1); // Increase volume
                    break;
                case 'ArrowDown':
                    changeVolume(video, -0.1); // Decrease volume
                    break;
                case 'ArrowRight':
                    seekVideo(video, 5); // Seek forward 5 seconds
                    break;
                case 'ArrowLeft':
                    seekVideo(video, -5); // Seek backward 5 seconds
                    break;
            }
        }
    });

    // Set the video element to be focusable
    document.addEventListener('DOMContentLoaded', (event) => {
        const video = getVideoElement();
        if (video) {
            video.setAttribute('tabindex', '-1');
        }
    });

})();