Created
February 1, 2025 18:20
-
-
Save harry0805/7428b7690be37b6e46dd6d4170547164 to your computer and use it in GitHub Desktop.
TamperMonkey Script: Youtube Keyboard Shortcuts Fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Youtube Keyboard Shortcuts Fix | |
| // @namespace https://github.com/harry0805 | |
| // @version 0.1 | |
| // @description Fixes focus issues preventing usage of keyboard shortcuts on youtube. | |
| // @author harry0805 | |
| // @match https://www.youtube.com/* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| console.log('Active <Youtube Keyboard Shortcuts Fix>') | |
| // Variable to store the current mouse state | |
| let isMouseDown = false; | |
| const matchElementClass = ["ytp-progress-bar", "ytp-play-button", "ytp-next-button", "ytp-volume-panel"] | |
| function hasAnyClass(element) { | |
| for (var i = 0; i < matchElementClass.length; i++) { | |
| if (element.classList.contains(matchElementClass[i])) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| function focusVideo() { | |
| let focused = document.activeElement | |
| let condition = focused.tagName == "BODY" || hasAnyClass(focused) | |
| if (condition && isMouseDown == false) { | |
| document.getElementById("movie_player").focus({ preventScroll: true }); | |
| } | |
| } | |
| // Add event listener for mousedown events on the document | |
| document.addEventListener('mousedown', function(event) { | |
| // Check if the left mouse button was pressed | |
| if (event.button === 0) { | |
| isMouseDown = true; | |
| } | |
| }); | |
| // Add event listener for mouseup events on the document | |
| document.addEventListener('mouseup', function(event) { | |
| // Check if the left mouse button was released | |
| if (event.button === 0) { | |
| isMouseDown = false; | |
| focusVideo() | |
| } | |
| }); | |
| document.addEventListener('focus', function(event) { | |
| focusVideo() | |
| }, true); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment