Youtube Space=Pause

Pressing space when watching a video on Youtube will always pause the video instead of functioning like Page Down key.

  1. // ==UserScript==
  2. // @name Youtube Space=Pause
  3. // @namespace s4nji
  4. // @author s4nji
  5. // @description Pressing space when watching a video on Youtube will always pause the video instead of functioning like Page Down key.
  6. // @license CC0
  7. // @include https://www.youtube.com/watch*
  8. // @version 2
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. /* - - - - - *\
  14. * Variables *
  15. \* - - - - - */
  16. var inputFocus = false;
  17. var debug = false;
  18.  
  19.  
  20. /* - - - - - - - - - *\
  21. * Utility Functions *
  22. \* - - - - - - - - - */
  23. // Debug Console Logging
  24. function clog(msg) {
  25. if (debug) {
  26. console.log("YtS=P | "+msg);
  27. }
  28. }
  29.  
  30. // Run codes "unsafely"
  31. function contentEval(source) {
  32. "use strict";
  33. // Check for function input.
  34. if ('function' === typeof source) {
  35. // Execute this function with no arguments, by adding parentheses.
  36. // One set around the function, required for valid syntax, and a
  37. // second empty set calls the surrounded function.
  38. source = '(' + source + ')();';
  39. }
  40.  
  41. // Create a script node holding this source code.
  42.  
  43. var script = document.createElement('script');
  44. script.setAttribute("type", "application/javascript");
  45. script.textContent = source;
  46.  
  47. // Insert the script node into the page, so it will run, and immediately
  48. // remove it to clean up.
  49. document.body.appendChild(script);
  50. document.body.removeChild(script);
  51. }
  52.  
  53. // Set inputFocus to true if an input box is on focus
  54. function setInputFocus(bool) {
  55. "use strict";
  56. return function() {
  57. if ( typeof bool === "boolean" ) {
  58. inputFocus = bool;
  59. clog("inputFocus = " + inputFocus);
  60. }
  61. };
  62. }
  63.  
  64. // Add Event Listeners
  65. function hookInputFocus() {
  66. "use strict";
  67. var inputs = document.querySelectorAll("input"), i;
  68. for (i=0; i<inputs.length; i+=1) {
  69. inputs[i].addEventListener('focus', setInputFocus(true));
  70. inputs[i].addEventListener('blur', setInputFocus(false));
  71. clog("hooked "+i+"!");
  72. }
  73. }
  74.  
  75. /* - - - - - - - *\
  76. * Main Function *
  77. \* - - - - - - - */
  78. function main() {
  79. "use strict";
  80. document.body.addEventListener('keydown', function(event) {
  81. clog("inputFocus == " + inputFocus);
  82. if (event.keyCode === 32 && !inputFocus) {
  83. event.preventDefault();
  84. var status = document.querySelector("#movie_player").getPlayerState();
  85. if ( status === 1 || status === 3 ) {
  86. contentEval('document.querySelector("#movie_player").pauseVideo();');
  87. } else if ( status === 2 || status === 0 ) {
  88. contentEval('document.querySelector("#movie_player").playVideo();');
  89. }
  90. // N/A (-4), unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5).
  91. }
  92. });
  93. hookInputFocus();
  94. }
  95.  
  96. // Start on load
  97. document.addEventListener('DOMContentLoaded', main() );

QingJ © 2025

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