BetterYTM.user.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // ==UserScript==
  2. // @name BetterYTM
  3. // @name:de BetterYTM
  4. // @namespace https://github.com/Sv443/BetterYTM#readme
  5. // @version 0.2.0
  6. // @license MIT
  7. // @author Sv443
  8. // @copyright Sv443 <[email protected]> (https://github.com/Sv443)
  9. // @description Improvements for YouTube Music
  10. // @description:de Verbesserungen für YouTube Music
  11. // @match https://music.youtube.com/*
  12. // @match https://www.youtube.com/*
  13. // @icon https://www.google.com/s2/favicons?domain=music.youtube.com
  14. // @run-at document-start
  15. // @connect self
  16. // @connect *.youtube.com
  17. // @downloadURL https://raw.githubusercontent.com/Sv443/BetterYTM/main/BetterYTM.user.js
  18. // @updateURL https://raw.githubusercontent.com/Sv443/BetterYTM/main/BetterYTM.user.js
  19. // ==/UserScript==
  20. /* Disclaimer: I am not affiliated with YouTube, Google, Alphabet or anyone else */
  21. /* C&D this, Susan 🖕 */
  22. /*
  23. █▀▀█ ▄▄▄ █ █ ▀ ▄▄▄ ▄▄▄▄ ▄▄▄
  24. ▀▀▄▄ █▄█ █▀ █▀ ▀█ █ █ █ ▄▄ █▄▄ ▀
  25. █▄▄█ █▄▄ █▄▄ █▄▄ ▄█▄ █ █ █▄▄█ ▄▄█ ▄
  26. */
  27. /**
  28. * This is where you can enable or disable features
  29. * If this userscript ever becomes something I might add like a menu to toggle these
  30. */
  31. const features = Object.freeze({
  32. /** Whether arrow keys should skip forwards and backwards by 10 seconds */
  33. arrowKeySupport: true,
  34. /** Whether to add a button or key combination (TODO) to switch between the YT and YTM sites on a video */
  35. switchBetweenSites: true,
  36. // /** The theme color - accepts any CSS color value - default is "#ff0000" */
  37. // themeColor: "#0f0",
  38. });
  39. //#MARKER types
  40. /** @typedef {"yt"|"ytm"} Domain */
  41. //#MARKER init
  42. const info = Object.freeze({
  43. name: GM.info.script.name, // eslint-disable-line no-undef
  44. version: GM.info.script.version, // eslint-disable-line no-undef
  45. namespace: GM.info.script.namespace, // eslint-disable-line no-undef
  46. });
  47. function init()
  48. {
  49. console.log(`${info.name} v${info.version} - ${info.namespace}`);
  50. document.addEventListener("DOMContentLoaded", onDomLoad);
  51. }
  52. //#MARKER events
  53. /**
  54. * Called when the DOM has finished loading (after `DOMContentLoaded` is emitted)
  55. */
  56. function onDomLoad()
  57. {
  58. const domain = getDomain();
  59. if(features.arrowKeySupport && domain === "ytm")
  60. document.addEventListener("keydown", onKeyDown);
  61. if(features.switchBetweenSites)
  62. initSiteSwitch(domain);
  63. // if(features.themeColor != "#f00" && features.themeColor != "#ff0000")
  64. // applyTheme();
  65. }
  66. //#MARKER features
  67. //#SECTION arrow key skip
  68. /**
  69. * Called when the user presses keys
  70. * @param {KeyboardEvent} evt
  71. */
  72. function onKeyDown(evt)
  73. {
  74. if(["ArrowLeft", "ArrowRight"].includes(evt.code))
  75. {
  76. // 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
  77. const defaultProps = {
  78. altKey: false,
  79. bubbles: true,
  80. cancelBubble: false,
  81. cancelable: true,
  82. charCode: 0,
  83. composed: true,
  84. ctrlKey: false,
  85. currentTarget: null,
  86. defaultPrevented: evt.defaultPrevented,
  87. explicitOriginalTarget: document.body,
  88. isTrusted: true,
  89. metaKey: false,
  90. originalTarget: document.body,
  91. repeat: false,
  92. shiftKey: false,
  93. srcElement: document.body,
  94. target: document.body,
  95. type: "keydown",
  96. view: window,
  97. };
  98. let invalidKey = false;
  99. let keyProps = {};
  100. switch(evt.code)
  101. {
  102. case "ArrowLeft":
  103. keyProps = {
  104. code: "KeyH",
  105. key: "h",
  106. keyCode: 72,
  107. which: 72,
  108. };
  109. break;
  110. case "ArrowRight":
  111. keyProps = {
  112. code: "KeyL",
  113. key: "l",
  114. keyCode: 76,
  115. which: 76,
  116. };
  117. break;
  118. default:
  119. // console.warn("BetterYTM - Unknown key", evt.code);
  120. invalidKey = true;
  121. break;
  122. }
  123. if(!invalidKey)
  124. document.body.dispatchEvent(new KeyboardEvent("keydown", { ...defaultProps, ...keyProps }));
  125. }
  126. }
  127. //#SECTION site switch
  128. /**
  129. * Initializes the site switch feature
  130. * @param {Domain} domain
  131. */
  132. function initSiteSwitch(domain)
  133. {
  134. // TODO:
  135. // extra features:
  136. // - keep video time
  137. document.addEventListener("keydown", (e) => {
  138. if(e.key === "F8") // TODO:
  139. switchSite(domain === "yt" ? "ytm" : "yt");
  140. });
  141. }
  142. /**
  143. * Switches to the other site (between YT and YTM)
  144. * @param {Domain} newDomain
  145. */
  146. function switchSite(newDomain)
  147. {
  148. let subdomain;
  149. if(newDomain === "ytm")
  150. subdomain = "music";
  151. else if(newDomain === "yt")
  152. subdomain = "www";
  153. if(!subdomain)
  154. throw new TypeError(`Unrecognized domain '${newDomain}'`);
  155. const { pathname, search, hash } = new URL(location.href);
  156. const newSearch = search.includes("?") ? `${search}&t=${getVideoTime()}` : `?t=${getVideoTime()}`;
  157. const url = `https://${subdomain}.youtube.com${pathname}${newSearch}${hash}`;
  158. console.info(`BetterYTM - switching to domain '${newDomain}' at ${url}`);
  159. location.href = url;
  160. }
  161. /**
  162. * Returns the current video time in seconds
  163. * @param {Domain} [domain]
  164. * @returns {number|null} Returns null if video time is unavailable
  165. */
  166. function getVideoTime(domain )
  167. {
  168. if(typeof domain !== "string")
  169. domain = getDomain();
  170. if(domain === "ytm")
  171. {
  172. const pbEl = document.querySelector("#progress-bar");
  173. return pbEl.value ?? null;
  174. }
  175. else if(domain === "yt") // YT doesn't update the progress bar when it's hidden (YTM doesn't hide it) so TODO: come up with some solution here
  176. return document.querySelector();
  177. return null;
  178. }
  179. //#MARKER other
  180. /**
  181. * Returns the current domain as a string representation
  182. * @returns {Domain}
  183. */
  184. function getDomain()
  185. {
  186. const { hostname } = new URL(location.href);
  187. return hostname.toLowerCase().includes("music") ? "ytm" : "yt"; // other cases are caught by the `@match`es at the top
  188. }
  189. (() => init())(); // call init() when file is loaded