BetterYTM.user.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // ==UserScript==
  2. // @name BetterYTM
  3. // @namespace https://github.com/Sv443/BetterYTM#readme
  4. // @version 0.2.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/*
  10. // @icon https://www.google.com/s2/favicons?domain=music.youtube.com
  11. // @grant none
  12. // @run-at document-start
  13. // ==/UserScript==
  14. /* Disclaimer: I am not affiliated with YouTube, Google, Alphabet or anyone else */
  15. /* C&D this Susan 🖕 */
  16. /**
  17. * This is where you can enable or disable features
  18. * If this userscript ever becomes something I might add like a menu to toggle these
  19. */
  20. const features = Object.freeze({
  21. /** Whether arrow keys should skip forwards and backwards by 10 seconds */
  22. arrowKeySupport: true,
  23. /** Whether to add a button or key combination (TODO) to switch between the YouTube and YouTube Music pages */
  24. switchBetweenSites: true,
  25. // /** The theme color - accepts any CSS color value - default is "#ff0000" */
  26. // themeColor: "#0f0",
  27. });
  28. const info = Object.freeze({
  29. name: GM.info.script.name, // eslint-disable-line no-undef
  30. version: GM.info.script.version, // eslint-disable-line no-undef
  31. namespace: GM.info.script.namespace, // eslint-disable-line no-undef
  32. });
  33. //#MARKER init
  34. function init()
  35. {
  36. console.log(`${info.name} v${info.version} - ${info.namespace}`);
  37. document.addEventListener("DOMContentLoaded", onDomLoad);
  38. }
  39. //#MARKER events
  40. /**
  41. * Called when the DOM has finished loading
  42. */
  43. function onDomLoad()
  44. {
  45. document.addEventListener("keydown", onKeyDown);
  46. // if(features.themeColor != "#f00" && features.themeColor != "#ff0000")
  47. // applyTheme();
  48. }
  49. /**
  50. * Called when the user presses keys
  51. * @param {KeyboardEvent} evt
  52. */
  53. function onKeyDown(evt)
  54. {
  55. if(features.arrowKeySupport && ["ArrowLeft", "ArrowRight"].includes(evt.code))
  56. {
  57. switch(evt.code)
  58. {
  59. case "ArrowLeft":
  60. // 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
  61. document.body.dispatchEvent(new KeyboardEvent("keydown", {
  62. altKey: false,
  63. bubbles: true,
  64. cancelBubble: false,
  65. cancelable: true,
  66. charCode: 0,
  67. code: "KeyH",
  68. composed: true,
  69. ctrlKey: false,
  70. currentTarget: null,
  71. defaultPrevented: evt.defaultPrevented,
  72. explicitOriginalTarget: document.body,
  73. isTrusted: true,
  74. key: "h",
  75. keyCode: 72,
  76. metaKey: false,
  77. originalTarget: document.body,
  78. repeat: false,
  79. shiftKey: false,
  80. srcElement: document.body,
  81. target: document.body,
  82. type: "keydown",
  83. view: window,
  84. which: 72,
  85. }));
  86. break;
  87. case "ArrowRight":
  88. // 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
  89. document.body.dispatchEvent(new KeyboardEvent("keydown", {
  90. altKey: false,
  91. bubbles: true,
  92. cancelBubble: false,
  93. cancelable: true,
  94. charCode: 0,
  95. code: "KeyL",
  96. composed: true,
  97. ctrlKey: false,
  98. currentTarget: null,
  99. defaultPrevented: evt.defaultPrevented,
  100. explicitOriginalTarget: document.body,
  101. isTrusted: true,
  102. key: "l",
  103. keyCode: 76,
  104. metaKey: false,
  105. originalTarget: document.body,
  106. repeat: false,
  107. shiftKey: false,
  108. srcElement: document.body,
  109. target: document.body,
  110. type: "keydown",
  111. view: window,
  112. which: 76,
  113. }));
  114. break;
  115. default:
  116. console.warn("Unknown key", evt.code);
  117. break;
  118. }
  119. }
  120. }
  121. // /**
  122. // * Applies the set theme color
  123. // */
  124. // function applyTheme()
  125. // {
  126. // const formatRegex = /^(\d{3}){1,2}$/;
  127. // const color = features.themeColor.match(formatRegex) ? `#${color}` : color;
  128. // /**
  129. // * A list of changes to be made to the page to apply the theme color
  130. // */
  131. // const themeChanges = [
  132. // {
  133. // elem: document.querySelector("#progressContainer > #primaryProgress"),
  134. // prop: "background",
  135. // important: false,
  136. // },
  137. // {
  138. // elem: document.querySelector(),
  139. // prop: "",
  140. // important: false,
  141. // },
  142. // {
  143. // elem: document.querySelector(),
  144. // prop: "",
  145. // important: false,
  146. // },
  147. // ];
  148. // themeChanges.forEach(change => {
  149. // if(change.elem)
  150. // {
  151. // const value = change.important === true ? `${color} !important` : color;
  152. // change.elem.style[change.prop] = value;
  153. // }
  154. // });
  155. // }
  156. (() => init())();