BetterYTM.user.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { loadFeatureConf } from "./config";
  2. import { logLevel, scriptInfo } from "./constants";
  3. import { addGlobalStyle, error, getDomain, initSiteEvents, log, setLogLevel } from "./utils";
  4. import {
  5. // layout
  6. initQueueButtons, addWatermark,
  7. preInitLayout, removeUpgradeTab, setVolSliderSize,
  8. setVolSliderStep,
  9. // lyrics
  10. addMediaCtrlGeniusBtn, geniUrlBase,
  11. // input
  12. initArrowKeySkip, initSiteSwitch, addAnchorImprovements,
  13. // menu
  14. initMenu, addMenu, initBeforeUnloadHook,
  15. } from "./features/index";
  16. // TODO: add some style
  17. console.log(`${scriptInfo.name} v${scriptInfo.version} (${scriptInfo.lastCommit}) - ${scriptInfo.namespace}`);
  18. console.log(`Powered by lots of ambition and my song metadata API: ${geniUrlBase}`);
  19. const domain = getDomain();
  20. async function init() {
  21. await preInitLayout();
  22. try {
  23. document.addEventListener("DOMContentLoaded", onDomLoad);
  24. }
  25. catch(err) {
  26. console.error(`${scriptInfo.name} - General Error:`, err);
  27. }
  28. try {
  29. initMenu();
  30. }
  31. catch(err) {
  32. error("Couldn't initialize menu:", err);
  33. }
  34. }
  35. /** Called when the DOM has finished loading and can be queried and altered by the userscript */
  36. async function onDomLoad() {
  37. // post-build these double quotes are replaced by backticks
  38. addGlobalStyle("{{GLOBAL_STYLE}}", "global");
  39. const features = await loadFeatureConf();
  40. log(`Initializing features for domain '${domain}'`);
  41. try {
  42. if(domain === "ytm") {
  43. initSiteEvents();
  44. if(features.arrowKeySupport)
  45. initArrowKeySkip();
  46. if(features.removeUpgradeTab)
  47. removeUpgradeTab();
  48. if(features.watermarkEnabled)
  49. addWatermark();
  50. if(features.geniusLyrics)
  51. addMediaCtrlGeniusBtn();
  52. if(features.queueButtons)
  53. initQueueButtons();
  54. if(typeof features.volumeSliderSize === "number")
  55. setVolSliderSize();
  56. if(features.anchorImprovements)
  57. addAnchorImprovements();
  58. setVolSliderStep();
  59. }
  60. if(["ytm", "yt"].includes(domain)) {
  61. if(features.switchBetweenSites)
  62. initSiteSwitch(domain);
  63. try {
  64. addMenu(); // TODO: remove
  65. }
  66. catch(err) {
  67. error("Couldn't add menu:", err);
  68. }
  69. }
  70. }
  71. catch(err) {
  72. error("General error while executing feature:", err);
  73. }
  74. }
  75. // stuff that needs to be called ASAP, before anything async happens
  76. setLogLevel(logLevel);
  77. if(domain === "ytm")
  78. initBeforeUnloadHook();
  79. init();