A Youtube bug disables the space key shortcut, which often happens when you change windows and return to Youtube, often with the ALT + TAB shortcut. This script ensures that the spacebar PAUSE / PLAY shortcut works correctly.
// ==UserScript==// @name Youtube Spacebar Pause/Play Fix// @namespace http://tampermonkey.net/// @version 1.4// @description A Youtube bug disables the space key shortcut, which often happens when you change windows and return to Youtube, often with the ALT + TAB shortcut. This script ensures that the spacebar PAUSE / PLAY shortcut works correctly.// @author waszner and Mimo// @match *://www.youtube.com/*// @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com// @grant none// @license MIT// ==/UserScript==(function() {'use strict';function isYouTubeWatchPage() {return window.location.href.includes('www.youtube.com/watch');}// Fonction pour détecter les changements de page sur YouTubefunction detectPageChanges() {// Fonction pour afficher une alerte en cas de changementfunction pageIsWatch() {console.log("new video listeners")isYoutubeWatch = true;updateVideoListeners();}// Ajoutez un écouteur d'événement pour l'événement 'spfdone'window.addEventListener('spfdone', function() {if (isYouTubeWatchPage()) {pageIsWatch();}else{isYoutubeWatch = false;}});// Ajoutez un écouteur d'événement pour l'événement 'yt-navigate-finish'window.addEventListener('yt-navigate-finish', function() {if (isYouTubeWatchPage()) {pageIsWatch();}else{isYoutubeWatch = false;}});}detectPageChanges();if(isYouTubeWatchPage()){updateVideoListeners();}let isYoutubeWatch = isYouTubeWatchPage();let spacePressed = false;let currentTime = 0;let isVideoPlaying = false;let wasVideoPlaying = false;const YoutubeCommentFieldSelector = '#contenteditable-root'; //css selector of youtube commentsconst YoutubeSearchBar = '#search'; //css selector of youtube search barconst KPressDelay = 200; // how much time will the program wait before pressing K (probably ping dependent)// Add event listenerdocument.addEventListener('keydown', pressKifWatch, true);function pressKifWatch(e){// Only executes the pressK function if it's a youtube watch pageif(isYouTubeWatchPage()){pressKWithSpacebar(e);}}// Function to press "K" with the Spacebar when the video's play/pause state didn't changefunction pressKWithSpacebar(e) {//preventing playing/pausing the video while typing coments or repliesif (e.target.matches(YoutubeCommentFieldSelector) || e.target.matches(YoutubeSearchBar)){return;}else{if (e.key === ' ' && spacePressed === false) {spacePressed = true;wasVideoPlaying = isVideoPlaying;setTimeout(simulateKKeyPress, KPressDelay);}}}function updateVideoListeners(){// Listen to the video's play and pause events to update the variablesconst video = document.querySelector('video');//These prevent external play/pause (pressing K, clicking on the video etc) from messing up the program's runvideo.addEventListener('play', function() {isVideoPlaying = true;if (spacePressed) { //Prevents the "double tap" of spaceBar when the video IS in focus and pressing the space bar did play/pause itspacePressed = false; // Reset the Spacebar press time if video started playing}});video.addEventListener('pause', function() {isVideoPlaying = false;if (spacePressed) { //Prevents the "double tap" of spaceBar when the video IS in focus and pressing the space bar did play/pause itspacePressed = false; // Reset the Spacebar press time if video paused}});}// Function to simulate pressing "K" if the conditions are metfunction simulateKKeyPress() {if (spacePressed && (wasVideoPlaying === isVideoPlaying)) {// Send an "K" keypress to simulate pressing "K"const nKeyPress = new KeyboardEvent('keydown', { key: 'K', code: 'KeyK', which: 75, keyCode: 75, charCode: 75});document.dispatchEvent(nKeyPress);}}})();