Youtube Play/Pause Instead of Changing Playback Speed

Pressing space pauses or plays the video without affecting playback speed.

As of 2024-10-08. See the latest version.

// ==UserScript==
// @name         Youtube Play/Pause Instead of Changing Playback Speed
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Pressing space pauses or plays the video without affecting playback speed.
// @author       rasm446i
// @match        *://www.youtube.com/*
// @grant        none
// @license MIT
// ==/UserScript==
 
(function() {
    'use strict';
 
    document.addEventListener('keydown', function(event) {
        if (event.code === 'Space' && !event.ctrlKey && !event.altKey && !event.shiftKey) {
            event.preventDefault();
            event.stopPropagation();
 
            let video = document.querySelector('video');
 
            if (video) {
                if (video.paused) {
                    video.play();
                } else {
                    video.pause();
                }
            }
        }
    }, true);
})();