index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { addGlobalStyle, initOnSelector, onSelector } from "@sv443-network/userutils";
  2. import { loadFeatureConf } from "./config";
  3. import { logLevel, scriptInfo } from "./constants";
  4. import { error, getDomain, log, setLogLevel } from "./utils";
  5. import { initSiteEvents } from "./events";
  6. import {
  7. // layout
  8. initQueueButtons, addWatermark,
  9. preInitLayout, removeUpgradeTab,
  10. initVolumeFeatures,
  11. // lyrics
  12. addMediaCtrlLyricsBtn, geniUrlBase,
  13. // input
  14. initArrowKeySkip, initSiteSwitch, addAnchorImprovements,
  15. // menu
  16. initMenu, addMenu, initBeforeUnloadHook, addConfigMenuOption,
  17. } from "./features/index";
  18. {
  19. // console watermark with sexy gradient
  20. const styleGradient = "background: rgba(165, 38, 38, 1); background: linear-gradient(90deg, rgb(154, 31, 103) 0%, rgb(135, 31, 31) 40%, rgb(184, 64, 41) 100%);";
  21. const styleCommon = "color: #fff; font-size: 1.5em; padding-left: 6px; padding-right: 6px;";
  22. console.log();
  23. console.log(
  24. `%c${scriptInfo.name}%cv${scriptInfo.version}%c\n\nBuild #${scriptInfo.lastCommit} ─ ${scriptInfo.namespace}`,
  25. `font-weight: bold; ${styleCommon} ${styleGradient}`,
  26. `background-color: #333; ${styleCommon}`,
  27. "padding: initial;",
  28. );
  29. console.log([
  30. "Powered by:",
  31. "─ lots of ambition",
  32. `─ my song metadata API: ${geniUrlBase}`,
  33. "─ my userscript utility library: https://github.com/Sv443-Network/UserUtils",
  34. "─ this tiny event listener library: https://github.com/billjs/event-emitter",
  35. ].join("\n"));
  36. console.log();
  37. }
  38. const domain = getDomain();
  39. /** Stuff that needs to be called ASAP, before anything async happens */
  40. function preInit() {
  41. setLogLevel(logLevel);
  42. if(domain === "ytm")
  43. initBeforeUnloadHook();
  44. init();
  45. }
  46. async function init() {
  47. await preInitLayout();
  48. try {
  49. document.addEventListener("DOMContentLoaded", onDomLoad);
  50. }
  51. catch(err) {
  52. error("General Error:", err);
  53. }
  54. try {
  55. void ["TODO(v1.1):", initMenu];
  56. // initMenu();
  57. }
  58. catch(err) {
  59. error("Couldn't initialize menu:", err);
  60. }
  61. }
  62. /** Called when the DOM has finished loading and can be queried and altered by the userscript */
  63. async function onDomLoad() {
  64. // post-build these double quotes are replaced by backticks (because if backticks are used here, webpack converts them to double quotes)
  65. addGlobalStyle("{{GLOBAL_STYLE}}");
  66. initOnSelector();
  67. const features = await loadFeatureConf();
  68. log(`Initializing features for domain "${domain}"...`);
  69. try {
  70. if(domain === "ytm") {
  71. try {
  72. addMenu(); // TODO(v1.1): remove
  73. }
  74. catch(err) {
  75. error("Couldn't add menu:", err);
  76. }
  77. initSiteEvents();
  78. onSelector("tp-yt-iron-dropdown #contentWrapper ytd-multi-page-menu-renderer #container.menu-container", { listener: addConfigMenuOption });
  79. if(features.arrowKeySupport)
  80. initArrowKeySkip();
  81. if(features.removeUpgradeTab)
  82. removeUpgradeTab();
  83. if(features.watermarkEnabled)
  84. addWatermark();
  85. if(features.geniusLyrics)
  86. addMediaCtrlLyricsBtn();
  87. if(features.queueButtons)
  88. initQueueButtons();
  89. if(features.anchorImprovements)
  90. addAnchorImprovements();
  91. initVolumeFeatures();
  92. }
  93. if(["ytm", "yt"].includes(domain)) {
  94. if(features.switchBetweenSites)
  95. initSiteSwitch(domain);
  96. }
  97. }
  98. catch(err) {
  99. error("General error while executing feature:", err);
  100. }
  101. }
  102. preInit();