index.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. addMediaCtrlLyricsBtn, 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. /** Stuff that needs to be called ASAP, before anything async happens */
  21. function preInit() {
  22. setLogLevel(logLevel);
  23. if(domain === "ytm")
  24. initBeforeUnloadHook();
  25. init();
  26. }
  27. async function init() {
  28. await preInitLayout();
  29. try {
  30. document.addEventListener("DOMContentLoaded", onDomLoad);
  31. }
  32. catch(err) {
  33. console.error(`${scriptInfo.name} - General Error:`, err);
  34. }
  35. try {
  36. initMenu();
  37. }
  38. catch(err) {
  39. error("Couldn't initialize menu:", err);
  40. }
  41. }
  42. /** Called when the DOM has finished loading and can be queried and altered by the userscript */
  43. async function onDomLoad() {
  44. // post-build these double quotes are replaced by backticks
  45. addGlobalStyle("{{GLOBAL_STYLE}}", "global");
  46. const features = await loadFeatureConf();
  47. log(`Initializing features for domain '${domain}'`);
  48. try {
  49. if(domain === "ytm") {
  50. initSiteEvents();
  51. if(features.arrowKeySupport)
  52. initArrowKeySkip();
  53. if(features.removeUpgradeTab)
  54. removeUpgradeTab();
  55. if(features.watermarkEnabled)
  56. addWatermark();
  57. if(features.geniusLyrics)
  58. addMediaCtrlLyricsBtn();
  59. if(features.queueButtons)
  60. initQueueButtons();
  61. if(typeof features.volumeSliderSize === "number")
  62. setVolSliderSize();
  63. if(features.anchorImprovements)
  64. addAnchorImprovements();
  65. setVolSliderStep();
  66. }
  67. if(["ytm", "yt"].includes(domain)) {
  68. if(features.switchBetweenSites)
  69. initSiteSwitch(domain);
  70. try {
  71. addMenu(); // TODO: remove
  72. }
  73. catch(err) {
  74. error("Couldn't add menu:", err);
  75. }
  76. }
  77. }
  78. catch(err) {
  79. error("General error while executing feature:", err);
  80. }
  81. }
  82. preInit();