Control video volume and seek with arrow keys
当前为
// ==UserScript==
// @name Youtube Video Control with Arrow Keys
// @name:ru Ютуб видео правильная перемотка звука и видео через стрелки
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Control video volume and seek with arrow keys
// @description:ru Правильная перемотка видео через стрелки
// @author Boss of this gym
// @match *://www.youtube.com/*
// @grant none
// @license MIT
// ==/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 check if the active element is an input or textarea
function isInputElementFocused() {
const activeElement = document.activeElement;
if (!activeElement) return false;
const tagName = activeElement.tagName.toUpperCase();
return tagName === 'INPUT' || tagName === 'TEXTAREA' || activeElement.isContentEditable;
}
// Add event listener for keydown event
document.addEventListener('keydown', function(event) {
const video = getVideoElement();
if (!video || event.altKey || isInputElementFocused()) return; // Ignore if Alt is pressed or input is focused
if (['ArrowUp', 'ArrowDown'].includes(event.key)) {
event.preventDefault();
// Make the video element focusable and focus on it
if (document.activeElement !== video) {
video.setAttribute('tabindex', '-1');
video.focus();
}
switch(event.key) {
case 'ArrowUp':
changeVolume(video, 0.1); // Increase volume
break;
case 'ArrowDown':
changeVolume(video, -0.1); // Decrease volume
break;
}
}
});
})();