Youtube Spacebar Pause/Play Fix

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.

Ajankohdalta 29.10.2023. Katso uusin versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Youtube Spacebar Pause/Play Fix
// @namespace    http://tampermonkey.net/
// @version      0.1
// @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        https://www.youtube.com/watch*
// @match        http://www.youtube.com/watch*
// @match        https://youtube.com/watch*
// @match        http://youtube.com/watch*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @license MIT
// ==/UserScript==



(function() {

'use strict';

let spacePressed = false;

let currentTime = 0;

let isVideoPlaying = false;

let wasVideoPlaying = false;

const YoutubeCommentFieldSelector = '#contenteditable-root'; //css selector of youtube comments

const KPressDelay = 200; // how much time will the program wait before pressing K (probably ping dependent)

// Add event listener

document.addEventListener('keydown', pressKWithSpacebar, true);

// Function to press "K" with the Spacebar when the video's play/pause state didn't change

function pressKWithSpacebar(e) {


//preventing playing/pausing the video while typing coments or replies

if (e.target.matches(YoutubeCommentFieldSelector)){

return;

}else{

if (e.key === ' ' && spacePressed === false) {

spacePressed = true;

wasVideoPlaying = isVideoPlaying;

setTimeout(simulateKKeyPress, KPressDelay);

}

}

}

// Listen to the video's play and pause events to update the variables

const video = document.querySelector('video');

//These prevent external play/pause (pressing K, clicking on the video etc) from messing up the program's run

video.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 it

spacePressed = 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 it

spacePressed = false; // Reset the Spacebar press time if video paused

}

});

// Function to simulate pressing "K" if the conditions are met

function 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);

}

}

})();