YouTube Video Playback Shortcut

2/22/2024, 11:29:11 PM

  1. // ==UserScript==
  2. // @name YouTube Video Playback Shortcut
  3. // @namespace Violentmonkey Scripts
  4. // @match https://www.youtube.com/*
  5. // @grant none
  6. // @license MIT
  7. // @version 0.1.1
  8. // @description 2/22/2024, 11:29:11 PM
  9. // @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1.4.1
  10. // ==/UserScript==
  11.  
  12. const { KeyboardService } = VM.shortcut;
  13.  
  14. const service = new KeyboardService();
  15.  
  16. function getVideo() {
  17. return document.querySelector('.video-stream.html5-main-video');
  18. }
  19.  
  20. service.setContext('activeOnInput', false);
  21.  
  22. async function updateActiveOnInput() {
  23. const elm = document.activeElement;
  24. service.setContext('activeOnInput', elm instanceof HTMLInputElement || elm instanceof HTMLTextAreaElement);
  25. }
  26.  
  27. document.addEventListener('focus', (e) => {
  28. updateActiveOnInput();
  29. }, true);
  30.  
  31. document.addEventListener('blur', (e) => {
  32. updateActiveOnInput();
  33. }, true);
  34.  
  35. function regkey(i) {
  36. let position = 0;
  37. service.register('s-a-digit' + i, () => {
  38. let v = getVideo();
  39. if (!v) return;
  40. let c = (v.currentTime - 0.005).toFixed(2);
  41. if (position === c) position = 0;
  42. else position = c;
  43. }, {
  44. condition: '!activeOnInput',
  45. });
  46. service.register('a-digit' + i, () => {
  47. let v = getVideo();
  48. if (!v) return;
  49. if (!position) return;
  50. v.currentTime = +position;
  51. }, {
  52. condition: '!activeOnInput',
  53. });
  54. }
  55.  
  56. for (let i = 1; i < 9; i++) {
  57. regkey(i);
  58. }
  59.  
  60. service.enable();