types.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import type * as consts from "./constants";
  2. import type { scriptInfo } from "./constants";
  3. import type { addSelectorListener } from "./observers";
  4. import type resources from "../assets/resources.json";
  5. import type langMapping from "../assets/locales.json";
  6. import type { getResourceUrl, getSessionId, getVideoTime, TrLocale, t, tp } from "./utils";
  7. import type { getFeatures, saveFeatures } from "./config";
  8. /** Custom CLI args passed to rollup */
  9. export type RollupArgs = Partial<{
  10. "config-mode": "development" | "production";
  11. "config-branch": "main" | "develop";
  12. "config-host": "greasyfork" | "github" | "openuserjs";
  13. "config-assetSource": "local" | "github";
  14. "config-suffix": string;
  15. }>;
  16. // I know TS enums are impure but it doesn't really matter here, plus they look cooler
  17. export enum LogLevel {
  18. Debug,
  19. Info,
  20. }
  21. /** Which domain this script is currently running on */
  22. export type Domain = "yt" | "ytm";
  23. /** A URL string that starts with "http://" or "https://" */
  24. export type HttpUrlString = `http://${string}` | `https://${string}`;
  25. /** Key of a resource in `assets/resources.json` and extra keys defined by `tools/post-build.ts` */
  26. export type ResourceKey = keyof typeof resources | `trans-${keyof typeof langMapping}` | "changelog";
  27. /** Describes a single hotkey */
  28. export type HotkeyObj = {
  29. code: string,
  30. shift: boolean,
  31. ctrl: boolean,
  32. alt: boolean,
  33. };
  34. export type ObserverName = "body" | "playerBar" | "playerBarInfo";
  35. export type LyricsCacheEntry = {
  36. artist: string;
  37. song: string;
  38. url: string;
  39. viewed: number;
  40. added: number;
  41. };
  42. /** All functions exposed by the interface on the global `BYTM` object */
  43. export type InterfaceFunctions = {
  44. /** Adds a listener to one of the already present SelectorObserver instances */
  45. addSelectorListener: typeof addSelectorListener;
  46. /**
  47. * Returns the URL of a resource as defined in `assets/resources.json`
  48. * There are also some resources like translation files that get added by `tools/post-build.ts`
  49. *
  50. * The returned URL is a `blob:` URL served up by the userscript extension
  51. * This makes the resource fast to fetch and also prevents CORS issues
  52. */
  53. getResourceUrl: typeof getResourceUrl;
  54. /** Returns the unique session ID for the current tab */
  55. getSessionId: typeof getSessionId;
  56. /**
  57. * Returns the current video time (on both YT and YTM)
  58. * In case it can't be determined on YT, mouse movement is simulated to bring up the video time
  59. * In order for that edge case not to error out, the function would need to be called in response to a user interaction event (e.g. click) due to the strict autoplay policy in browsers
  60. */
  61. getVideoTime: typeof getVideoTime;
  62. /** Returns the translation for the provided translation key and set locale (check the files in the folder `assets/translations`) */
  63. t: typeof t;
  64. /** Returns the translation for the provided translation key, including pluralization identifier and set locale (check the files in the folder `assets/translations`) */
  65. tp: typeof tp;
  66. /** Returns the current feature configuration */
  67. getFeatures: typeof getFeatures;
  68. /** Overwrites the feature configuration with the provided one */
  69. saveFeatures: typeof saveFeatures;
  70. };
  71. // shim for the BYTM interface properties
  72. export type BytmObject =
  73. {
  74. [key: string]: unknown;
  75. locale: TrLocale;
  76. logLevel: LogLevel;
  77. }
  78. // information from the userscript header
  79. & typeof scriptInfo
  80. // certain variables from `src/constants.ts`
  81. & Pick<typeof consts, "mode" | "branch" | "host" | "buildNumber" | "compressionFormat">
  82. // global functions exposed through the interface in `src/interface.ts`
  83. & InterfaceFunctions
  84. // others
  85. & {
  86. // the entire UserUtils library
  87. UserUtils: typeof import("@sv443-network/userutils");
  88. };
  89. declare global {
  90. interface Window {
  91. // to see the expanded type, install the VS Code extension "MylesMurphy.prettify-ts"
  92. // and hover over the property just below:
  93. BYTM: BytmObject;
  94. }
  95. }
  96. export type FeatureKey = keyof FeatureConfig;
  97. export type FeatureCategory =
  98. | "layout"
  99. | "songLists"
  100. | "behavior"
  101. | "input"
  102. | "lyrics"
  103. | "general";
  104. type SelectOption = {
  105. value: string | number;
  106. label: string;
  107. };
  108. type FeatureTypeProps = ({
  109. type: "toggle";
  110. default: boolean;
  111. } & FeatureFuncProps)
  112. | ({
  113. type: "number";
  114. default: number;
  115. min: number;
  116. max?: number;
  117. step?: number;
  118. unit?: string | ((val: number) => string);
  119. } & FeatureFuncProps)
  120. | ({
  121. type: "select";
  122. default: string | number;
  123. options: SelectOption[] | (() => SelectOption[]);
  124. } & FeatureFuncProps)
  125. | ({
  126. type: "slider";
  127. default: number;
  128. min: number;
  129. max: number;
  130. step?: number;
  131. unit?: string | ((val: number) => string);
  132. } & FeatureFuncProps)
  133. | ({
  134. type: "hotkey";
  135. default: HotkeyObj;
  136. } & FeatureFuncProps)
  137. | {
  138. type: "text";
  139. default: string;
  140. normalize?: (val: string) => string;
  141. }
  142. | {
  143. type: "button";
  144. default: undefined;
  145. click: () => void;
  146. }
  147. type FeatureFuncProps = {
  148. /** Called to instantiate the feature on the page */
  149. enable: (featCfg: FeatureConfig) => void,
  150. } & (
  151. {
  152. /** Called to remove all traces of the feature from the page and memory (includes event listeners) */
  153. disable?: (feats: FeatureConfig) => void,
  154. }
  155. | {
  156. /** Called to update the feature's behavior when the config changes */
  157. change?: (feats: FeatureConfig) => void,
  158. }
  159. )
  160. /**
  161. * The feature info object that contains all properties necessary to construct the config menu and the feature config object.
  162. * Values are loosely typed so try to only use this with the `satisfies` keyword.
  163. * Use `typeof featInfo` (from `src/features/index.ts`) instead for full type safety.
  164. */
  165. export type FeatureInfo = Record<
  166. keyof FeatureConfig,
  167. {
  168. category: FeatureCategory;
  169. /**
  170. * HTML string that will be the help text for this feature
  171. * Specifying a function is useful for pluralizing or inserting values into the translation at runtime
  172. */
  173. helpText?: string | (() => string);
  174. /** Whether the value should be hidden in the config menu and from plugins */
  175. valueHidden?: boolean;
  176. /**
  177. * HTML string that is appended to the end of a feature's text description
  178. * @deprecated TODO:FIXME: To be removed or changed in the big menu rework
  179. */
  180. textAdornment?: () => (Promise<string | undefined> | string | undefined);
  181. /** Whether to only show this feature when advanced mode is activated (default false) */
  182. advanced?: boolean;
  183. }
  184. & FeatureTypeProps
  185. >;
  186. /** Feature configuration */
  187. export interface FeatureConfig {
  188. //#SECTION layout
  189. /** Remove the \"Upgrade\" / YT Music Premium tab */
  190. removeUpgradeTab: boolean;
  191. /** Add a percentage label to the volume slider */
  192. volumeSliderLabel: boolean;
  193. /** The width of the volume slider in pixels */
  194. volumeSliderSize: number;
  195. /** Volume slider sensitivity - the smaller this number, the finer the volume control */
  196. volumeSliderStep: number;
  197. /** Volume slider scroll wheel sensitivity */
  198. volumeSliderScrollStep: number;
  199. /** Show a BetterYTM watermark under the YTM logo */
  200. watermarkEnabled: boolean;
  201. /** Remove the "si" tracking parameter from links in the share popup */
  202. removeShareTrackingParam: boolean;
  203. /** Enable skipping to a specific time in the video by pressing a number key (0-9) */
  204. numKeysSkipToTime: boolean;
  205. /** Fix spacing issues in the layout */
  206. fixSpacing: boolean;
  207. //#SECTION song lists
  208. /** Add a button to each song in the queue to quickly open its lyrics page */
  209. lyricsQueueButton: boolean;
  210. /** Add a button to each song in the queue to quickly remove it */
  211. deleteFromQueueButton: boolean;
  212. /** Where to place the buttons in the queue */
  213. listButtonsPlacement: "queueOnly" | "everywhere";
  214. /** Add a button to the queue to scroll to the currently playing song */
  215. scrollToActiveSongBtn: boolean;
  216. //#SECTION behavior
  217. /** Whether to completely disable the popup that sometimes appears before leaving the site */
  218. disableBeforeUnloadPopup: boolean;
  219. /** After how many milliseconds to close permanent toasts */
  220. closeToastsTimeout: number;
  221. /** Remember the last song's time when reloading or restoring the tab */
  222. rememberSongTime: boolean;
  223. /** Where to remember the song time */
  224. rememberSongTimeSites: Domain | "all";
  225. /** Lock the volume slider at a specific level */
  226. lockVolume: boolean;
  227. /** The volume level to lock the slider at */
  228. lockVolumeLevel: number;
  229. //#SECTION input
  230. /** Arrow keys skip forwards and backwards */
  231. arrowKeySupport: boolean;
  232. /** By how many seconds to skip when pressing the arrow keys */
  233. arrowKeySkipBy: number;
  234. /** Add a hotkey to switch between the YT and YTM sites on a video / song */
  235. switchBetweenSites: boolean;
  236. /** The hotkey that needs to be pressed to initiate the site switch */
  237. switchSitesHotkey: HotkeyObj;
  238. /** Make it so middle clicking a song to open it in a new tab (through thumbnail and song title) is easier */
  239. anchorImprovements: boolean;
  240. //#SECTION lyrics
  241. /** Add a button to the media controls to open the current song's lyrics on genius.com in a new tab */
  242. geniusLyrics: boolean;
  243. /** Base URL to use for GeniURL */
  244. geniUrlBase: string;
  245. /** Token to use for GeniURL */
  246. geniUrlToken: string;
  247. /** Max size of lyrics cache */
  248. lyricsCacheMaxSize: number;
  249. /** Max TTL of lyrics cache entries, in ms */
  250. lyricsCacheTTL: number;
  251. /** Button to clear lyrics cache */
  252. clearLyricsCache: undefined;
  253. /** Whether to use fuzzy filtering when searching for lyrics */
  254. lyricsFuzzyFilter: boolean;
  255. //#SECTION misc
  256. /** The locale to use for translations */
  257. locale: TrLocale;
  258. /** Whether to check for updates to the script */
  259. versionCheck: boolean;
  260. /** Button to check for updates */
  261. checkVersionNow: undefined;
  262. /** The console log level - 0 = Debug, 1 = Info */
  263. logLevel: LogLevel;
  264. /** Whether to show advanced settings in the config menu */
  265. advancedMode: boolean;
  266. }