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.

  1. // ==UserScript==
  2. // @name Youtube Spacebar Pause/Play Fix
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.4
  5. // @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.
  6. // @author waszner and Mimo
  7. // @match *://www.youtube.com/*
  8.  
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14.  
  15.  
  16. (function() {
  17.  
  18. 'use strict';
  19.  
  20.  
  21.  
  22.  
  23. function isYouTubeWatchPage() {
  24. return window.location.href.includes('www.youtube.com/watch');
  25. }
  26.  
  27.  
  28. // Fonction pour détecter les changements de page sur YouTube
  29. function detectPageChanges() {
  30. // Fonction pour afficher une alerte en cas de changement
  31. function pageIsWatch() {
  32. console.log("new video listeners")
  33. isYoutubeWatch = true;
  34. updateVideoListeners();
  35. }
  36.  
  37. // Ajoutez un écouteur d'événement pour l'événement 'spfdone'
  38. window.addEventListener('spfdone', function() {
  39. if (isYouTubeWatchPage()) {
  40. pageIsWatch();
  41. }else{
  42. isYoutubeWatch = false;
  43. }
  44. });
  45.  
  46. // Ajoutez un écouteur d'événement pour l'événement 'yt-navigate-finish'
  47. window.addEventListener('yt-navigate-finish', function() {
  48. if (isYouTubeWatchPage()) {
  49. pageIsWatch();
  50. }else{
  51. isYoutubeWatch = false;
  52. }
  53. });
  54. }
  55.  
  56.  
  57. detectPageChanges();
  58. if(isYouTubeWatchPage()){
  59. updateVideoListeners();
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67. let isYoutubeWatch = isYouTubeWatchPage();
  68.  
  69. let spacePressed = false;
  70.  
  71. let currentTime = 0;
  72.  
  73. let isVideoPlaying = false;
  74.  
  75. let wasVideoPlaying = false;
  76.  
  77. const YoutubeCommentFieldSelector = '#contenteditable-root'; //css selector of youtube comments
  78. const YoutubeSearchBar = '#search'; //css selector of youtube search bar
  79.  
  80. const KPressDelay = 200; // how much time will the program wait before pressing K (probably ping dependent)
  81.  
  82. // Add event listener
  83.  
  84. document.addEventListener('keydown', pressKifWatch, true);
  85.  
  86. function pressKifWatch(e){
  87. // Only executes the pressK function if it's a youtube watch page
  88. if(isYouTubeWatchPage()){
  89. pressKWithSpacebar(e);
  90. }
  91. }
  92.  
  93. // Function to press "K" with the Spacebar when the video's play/pause state didn't change
  94.  
  95. function pressKWithSpacebar(e) {
  96.  
  97. //preventing playing/pausing the video while typing coments or replies
  98.  
  99. if (e.target.matches(YoutubeCommentFieldSelector) || e.target.matches(YoutubeSearchBar)){
  100.  
  101. return;
  102.  
  103. }else{
  104.  
  105. if (e.key === ' ' && spacePressed === false) {
  106.  
  107. spacePressed = true;
  108.  
  109. wasVideoPlaying = isVideoPlaying;
  110.  
  111. setTimeout(simulateKKeyPress, KPressDelay);
  112.  
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119. function updateVideoListeners(){
  120. // Listen to the video's play and pause events to update the variables
  121.  
  122. const video = document.querySelector('video');
  123.  
  124. //These prevent external play/pause (pressing K, clicking on the video etc) from messing up the program's run
  125.  
  126. video.addEventListener('play', function() {
  127.  
  128. isVideoPlaying = true;
  129.  
  130. if (spacePressed) { //Prevents the "double tap" of spaceBar when the video IS in focus and pressing the space bar did play/pause it
  131.  
  132. spacePressed = false; // Reset the Spacebar press time if video started playing
  133.  
  134. }
  135.  
  136. });
  137.  
  138. video.addEventListener('pause', function() {
  139.  
  140. isVideoPlaying = false;
  141.  
  142. if (spacePressed) { //Prevents the "double tap" of spaceBar when the video IS in focus and pressing the space bar did play/pause it
  143.  
  144. spacePressed = false; // Reset the Spacebar press time if video paused
  145.  
  146. }
  147.  
  148. });
  149.  
  150. }
  151.  
  152. // Function to simulate pressing "K" if the conditions are met
  153.  
  154. function simulateKKeyPress() {
  155.  
  156. if (spacePressed && (wasVideoPlaying === isVideoPlaying)) {
  157.  
  158. // Send an "K" keypress to simulate pressing "K"
  159.  
  160. const nKeyPress = new KeyboardEvent('keydown', { key: 'K', code: 'KeyK', which: 75, keyCode: 75, charCode: 75});
  161.  
  162. document.dispatchEvent(nKeyPress);
  163.  
  164. }
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址