BetterYTM.user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // ==UserScript==
  2. // @name BetterYTM
  3. // @namespace https://github.com/Sv443/BetterYTM#readme
  4. // @version 0.1.0
  5. // @description:en Improvements for YouTube Music
  6. // @description:de Verbesserungen für YouTube Music
  7. // @author Sv443
  8. // @license MIT
  9. // @match https://music.youtube.com/watch*
  10. // @icon https://www.google.com/s2/favicons?domain=music.youtube.com
  11. // @grant none
  12. // @run-at document-start
  13. // ==/UserScript==
  14. /**
  15. * This is where you can enable or disable features
  16. * If this userscript ever becomes something I might add like a menu to toggle these
  17. */
  18. const features = Object.freeze({
  19. /** Whether arrow keys should skip the video */
  20. arrowKeySupport: true,
  21. });
  22. //#MARKER init
  23. function init()
  24. {
  25. document.addEventListener("DOMContentLoaded", onDomLoad);
  26. }
  27. //#MARKER events
  28. /**
  29. * Called when the DOM has finished loading
  30. */
  31. function onDomLoad()
  32. {
  33. document.addEventListener("keydown", onKeyDown);
  34. }
  35. /**
  36. * Called when the user presses keys
  37. * @param {KeyboardEvent} evt
  38. */
  39. function onKeyDown(evt)
  40. {
  41. if(features.arrowKeySupport && ["ArrowLeft", "ArrowRight"].includes(evt.code))
  42. {
  43. switch(evt.code)
  44. {
  45. case "ArrowLeft":
  46. // ripped this stuff from the console, most of these are probably unnecessary but this was finnicky af and I am sick and tired of trial and error
  47. document.body.dispatchEvent(new KeyboardEvent("keydown", {
  48. altKey: false,
  49. bubbles: true,
  50. cancelBubble: false,
  51. cancelable: true,
  52. charCode: 0,
  53. code: "KeyH",
  54. composed: true,
  55. ctrlKey: false,
  56. currentTarget: null,
  57. defaultPrevented: evt.defaultPrevented,
  58. explicitOriginalTarget: document.body,
  59. isTrusted: true,
  60. key: "h",
  61. keyCode: 72,
  62. metaKey: false,
  63. originalTarget: document.body,
  64. repeat: false,
  65. shiftKey: false,
  66. srcElement: document.body,
  67. target: document.body,
  68. type: "keydown",
  69. view: window,
  70. which: 72,
  71. }));
  72. break;
  73. case "ArrowRight":
  74. // ripped this stuff from the console, most of these are probably unnecessary but this was finnicky af and I am sick and tired of trial and error
  75. document.body.dispatchEvent(new KeyboardEvent("keydown", {
  76. altKey: false,
  77. bubbles: true,
  78. cancelBubble: false,
  79. cancelable: true,
  80. charCode: 0,
  81. code: "KeyL",
  82. composed: true,
  83. ctrlKey: false,
  84. currentTarget: null,
  85. defaultPrevented: evt.defaultPrevented,
  86. explicitOriginalTarget: document.body,
  87. isTrusted: true,
  88. key: "l",
  89. keyCode: 76,
  90. metaKey: false,
  91. originalTarget: document.body,
  92. repeat: false,
  93. shiftKey: false,
  94. srcElement: document.body,
  95. target: document.body,
  96. type: "keydown",
  97. view: window,
  98. which: 76,
  99. }));
  100. break;
  101. default:
  102. console.warn("Unknown key", evt.code);
  103. break;
  104. }
  105. }
  106. }
  107. (() => init())();