Youtube強制用空白鍵播放/暫停

讓空白鍵像k鍵一樣方便 原始功能依舊保持 所以空白鍵會同時擁有空白鍵和k鍵的效果

  1. // ==UserScript==
  2. // @name Youtube強制用空白鍵播放/暫停
  3. // @namespace https://gf.qytechs.cn/scripts/479470
  4. // @version 1.1
  5. // @description 讓空白鍵像k鍵一樣方便 原始功能依舊保持 所以空白鍵會同時擁有空白鍵和k鍵的效果
  6. // @author fmnijk
  7. // @match https://www.youtube.com/*
  8. // @icon https://www.google.com/s2/favicons?domain=youtube.com
  9. // @grant none
  10. // @run-at document-end
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. /*main function*/
  15.  
  16. let old_state = true
  17. let new_state = true
  18.  
  19. function kevent(){
  20. const kEvent = new KeyboardEvent("keydown", {
  21. keyCode: 75, // k鍵的keyCode是75
  22. bubbles: true, // 事件要冒泡
  23. cancelable: true // 事件要可取消
  24. });
  25. document.activeElement.dispatchEvent(kEvent);
  26. }
  27.  
  28. (window.onload = function() {
  29. 'use strict';
  30.  
  31. Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
  32. get: function(){
  33. return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
  34. }
  35. })
  36.  
  37. document.addEventListener("keydown", function(event) {
  38. // 如果按下的是空格鍵
  39. if (event.keyCode == 32 &&
  40. event.target != document.querySelector('#movie_player') &&
  41. event.target != document.querySelector('h2#title')){
  42. console.log(event.target)
  43. old_state = document.querySelector(".video-stream.html5-main-video").playing
  44. kevent()
  45. setTimeout(() => {
  46. new_state = document.querySelector(".video-stream.html5-main-video").playing
  47. if (new_state == old_state) {
  48. kevent()
  49. }
  50. }, "125");
  51. }
  52. });
  53.  
  54. })
  55.  
  56.  
  57.  
  58.  
  59.  

QingJ © 2025

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