types.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import type { Emitter } from "nanoevents";
  2. import type * as consts from "./constants";
  3. import type { scriptInfo } from "./constants";
  4. import type { addSelectorListener } from "./observers";
  5. import type resources from "../assets/resources.json";
  6. import type locales from "../assets/locales.json";
  7. import type { getResourceUrl, getSessionId, getVideoTime, TrLocale, t, tp } from "./utils";
  8. import type { getFeatures, saveFeatures } from "./config";
  9. import type { SiteEventsMap } from "./siteEvents";
  10. /** Custom CLI args passed to rollup */
  11. export type RollupArgs = Partial<{
  12. "config-mode": "development" | "production";
  13. "config-branch": "main" | "develop";
  14. "config-host": "greasyfork" | "github" | "openuserjs";
  15. "config-assetSource": "local" | "github";
  16. "config-suffix": string;
  17. }>;
  18. // I know TS enums are impure but it doesn't really matter here, plus they look cooler
  19. export enum LogLevel {
  20. Debug,
  21. Info,
  22. }
  23. /** Which domain this script is currently running on */
  24. export type Domain = "yt" | "ytm";
  25. /** A URL string that starts with "http://" or "https://" */
  26. export type HttpUrlString = `http://${string}` | `https://${string}`;
  27. /** Key of a resource in `assets/resources.json` and extra keys defined by `tools/post-build.ts` */
  28. export type ResourceKey = keyof typeof resources | `trans-${keyof typeof locales}` | "changelog";
  29. /** Describes a single hotkey */
  30. export type HotkeyObj = {
  31. code: string,
  32. shift: boolean,
  33. ctrl: boolean,
  34. alt: boolean,
  35. };
  36. export type ObserverName = "body" | "playerBar" | "playerBarInfo";
  37. export type LyricsCacheEntry = {
  38. artist: string;
  39. song: string;
  40. url: string;
  41. viewed: number;
  42. added: number;
  43. };
  44. //#MARKER global
  45. // shim for the BYTM interface properties
  46. export type BytmObject =
  47. {
  48. [key: string]: unknown;
  49. locale: TrLocale;
  50. logLevel: LogLevel;
  51. }
  52. // information from the userscript header
  53. & typeof scriptInfo
  54. // certain variables from `src/constants.ts`
  55. & Pick<typeof consts, "mode" | "branch" | "host" | "buildNumber" | "compressionFormat">
  56. // global functions exposed through the interface in `src/interface.ts`
  57. & InterfaceFunctions
  58. // others
  59. & {
  60. // the entire UserUtils library
  61. UserUtils: typeof import("@sv443-network/userutils");
  62. };
  63. declare global {
  64. interface Window {
  65. // to see the expanded type, install the VS Code extension "MylesMurphy.prettify-ts" and hover over the property below
  66. // alternatively navigate with ctrl+click to find the types
  67. BYTM: BytmObject;
  68. }
  69. }
  70. //#MARKER plugins
  71. /**
  72. * Intents (permissions) BYTM has to grant your plugin for it to be able to access certain features.
  73. * TODO: this feature is unfinished, but you should still specify the intents your plugin needs.
  74. */
  75. export enum PluginIntent {
  76. /** Plugin has access to hidden config values */
  77. HiddenConfigValues = 1,
  78. /** Plugin can write to the feature configuration */
  79. WriteFeatureConfig = 2,
  80. /** Plugin can write to the lyrics cache */
  81. WriteLyricsCache = 4,
  82. /** Plugin can add new translations and overwrite existing ones */
  83. WriteTranslations = 8,
  84. /** Plugin can create modal dialogs */
  85. CreateModalDialogs = 16,
  86. }
  87. /** Result of a plugin registration */
  88. export type PluginRegisterResult = {
  89. /** Public info about the registered plugin */
  90. info: PluginInfo;
  91. /** Emitter for plugin events - see {@linkcode PluginEventMap} for a list of events */
  92. events: Emitter<PluginEventMap>;
  93. }
  94. /** Minimal object that describes a plugin - this is all info the other installed plugins can see */
  95. export type PluginInfo = {
  96. /** Name of the plugin */
  97. name: string;
  98. /**
  99. * Adding the namespace and the name property makes the unique identifier for a plugin.
  100. * If one exists with the same name and namespace as this plugin, it may be overwritten at registration.
  101. * I recommend to set this value to a URL pointing to your homepage, or the author's username.
  102. */
  103. namespace: string;
  104. /** Version of the plugin as an array containing three whole numbers: `[major_version, minor_version, patch_version]` */
  105. version: [major: number, minor: number, patch: number];
  106. };
  107. /** Minimum part of the PluginDef object needed to make up the resolvable plugin identifier */
  108. export type PluginDefResolvable = PluginDef | { plugin: Pick<PluginDef["plugin"], "name" | "namespace"> };
  109. /** An object that describes a BYTM plugin */
  110. export type PluginDef = {
  111. plugin: PluginInfo & {
  112. /**
  113. * Descriptions of at least en_US and optionally any other locale supported by BYTM.
  114. * When an untranslated locale is set, the description will default to the value of en_US
  115. */
  116. description: Partial<Record<keyof typeof locales, string>> & {
  117. en_US: string;
  118. };
  119. /** URL to the plugin's icon - recommended size: 48x48 to 128x128 */
  120. iconUrl?: string;
  121. /** Homepage URLs for the plugin */
  122. homepage?: {
  123. /** URL to the plugin's GitHub repo */
  124. github?: string;
  125. /** URL to the plugin's GreasyFork page */
  126. greasyfork?: string;
  127. /** URL to the plugin's OpenUserJS page */
  128. openuserjs?: string;
  129. };
  130. };
  131. /** Intents (permissions) BYTM has to grant the plugin for it to work */
  132. intents?: Array<PluginIntent>;
  133. /** Info about the plugin contributors */
  134. contributors?: Array<{
  135. /** Name of this contributor */
  136. name: string;
  137. /** (optional) Email address of this contributor */
  138. email?: string;
  139. /** (optional) URL to this plugin contributor's homepage / GitHub profile */
  140. url?: string;
  141. }>;
  142. };
  143. /** All events that are dispatched to plugins individually, including everything in {@linkcode SiteEventsMap} - these don't have a prefix since they can't conflict with other events */
  144. export type PluginEventMap =
  145. & {
  146. /** Called when the plugin is registered on BYTM's side */
  147. pluginRegistered: (info: PluginInfo) => void;
  148. }
  149. & SiteEventsMap;
  150. /** A plugin in either the queue or registered map */
  151. export type PluginItem =
  152. & {
  153. def: PluginDef;
  154. }
  155. & Pick<PluginRegisterResult, "events">;
  156. /** All functions exposed by the interface on the global `BYTM` object */
  157. export type InterfaceFunctions = {
  158. /** Adds a listener to one of the already present SelectorObserver instances */
  159. addSelectorListener: typeof addSelectorListener;
  160. /**
  161. * Returns the URL of a resource as defined in `assets/resources.json`
  162. * There are also some resources like translation files that get added by `tools/post-build.ts`
  163. *
  164. * The returned URL is a `blob:` URL served up by the userscript extension
  165. * This makes the resource fast to fetch and also prevents CORS issues
  166. */
  167. getResourceUrl: typeof getResourceUrl;
  168. /** Returns the unique session ID for the current tab */
  169. getSessionId: typeof getSessionId;
  170. /**
  171. * Returns the current video time (on both YT and YTM)
  172. * In case it can't be determined on YT, mouse movement is simulated to bring up the video time
  173. * 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
  174. */
  175. getVideoTime: typeof getVideoTime;
  176. /** Returns the translation for the provided translation key and set locale (check the files in the folder `assets/translations`) */
  177. t: typeof t;
  178. /** Returns the translation for the provided translation key, including pluralization identifier and set locale (check the files in the folder `assets/translations`) */
  179. tp: typeof tp;
  180. /** Returns the current feature configuration */
  181. getFeatures: typeof getFeatures;
  182. /** Overwrites the feature configuration with the provided one */
  183. saveFeatures: typeof saveFeatures;
  184. };
  185. //#MARKER features
  186. export type FeatureKey = keyof FeatureConfig;
  187. export type FeatureCategory =
  188. | "layout"
  189. | "volume"
  190. | "songLists"
  191. | "behavior"
  192. | "input"
  193. | "lyrics"
  194. | "general";
  195. type SelectOption = {
  196. value: string | number;
  197. label: string;
  198. };
  199. type FeatureTypeProps = ({
  200. type: "toggle";
  201. default: boolean;
  202. } & FeatureFuncProps)
  203. | ({
  204. type: "number";
  205. default: number;
  206. min: number;
  207. max?: number;
  208. step?: number;
  209. unit?: string | ((val: number) => string);
  210. } & FeatureFuncProps)
  211. | ({
  212. type: "select";
  213. default: string | number;
  214. options: SelectOption[] | (() => SelectOption[]);
  215. } & FeatureFuncProps)
  216. | ({
  217. type: "slider";
  218. default: number;
  219. min: number;
  220. max: number;
  221. step?: number;
  222. unit?: string | ((val: number) => string);
  223. } & FeatureFuncProps)
  224. | ({
  225. type: "hotkey";
  226. default: HotkeyObj;
  227. } & FeatureFuncProps)
  228. | {
  229. type: "text";
  230. default: string;
  231. normalize?: (val: string) => string;
  232. }
  233. | {
  234. type: "button";
  235. default: undefined;
  236. click: () => void;
  237. }
  238. type FeatureFuncProps = {
  239. /** Called to instantiate the feature on the page */
  240. enable: (featCfg: FeatureConfig) => void,
  241. } & (
  242. {
  243. /** Called to remove all traces of the feature from the page and memory (includes event listeners) */
  244. disable?: (feats: FeatureConfig) => void,
  245. }
  246. | {
  247. /** Called to update the feature's behavior when the config changes */
  248. change?: (feats: FeatureConfig) => void,
  249. }
  250. )
  251. /**
  252. * The feature info object that contains all properties necessary to construct the config menu and the feature config object.
  253. * Values are loosely typed so try to only use this with the `satisfies` keyword.
  254. * Use `typeof featInfo` (from `src/features/index.ts`) instead for full type safety.
  255. */
  256. export type FeatureInfo = Record<
  257. keyof FeatureConfig,
  258. {
  259. category: FeatureCategory;
  260. /**
  261. * HTML string that will be the help text for this feature
  262. * Specifying a function is useful for pluralizing or inserting values into the translation at runtime
  263. */
  264. helpText?: string | (() => string);
  265. /** Whether the value should be hidden in the config menu and from plugins */
  266. valueHidden?: boolean;
  267. /**
  268. * HTML string that is appended to the end of a feature's text description
  269. * @deprecated TODO:FIXME: To be removed or changed in the big menu rework
  270. */
  271. textAdornment?: () => (Promise<string | undefined> | string | undefined);
  272. /** Whether to only show this feature when advanced mode is activated (default false) */
  273. advanced?: boolean;
  274. }
  275. & FeatureTypeProps
  276. >;
  277. /** Feature configuration */
  278. export interface FeatureConfig {
  279. //#SECTION layout
  280. /** Show a BetterYTM watermark under the YTM logo */
  281. watermarkEnabled: boolean;
  282. /** Remove the "si" tracking parameter from links in the share popup */
  283. removeShareTrackingParam: boolean;
  284. /** Enable skipping to a specific time in the video by pressing a number key (0-9) */
  285. numKeysSkipToTime: boolean;
  286. /** Fix spacing issues in the layout */
  287. fixSpacing: boolean;
  288. /** Remove the \"Upgrade\" / YT Music Premium tab */
  289. removeUpgradeTab: boolean;
  290. //#SECTION volume
  291. /** Add a percentage label to the volume slider */
  292. volumeSliderLabel: boolean;
  293. /** The width of the volume slider in pixels */
  294. volumeSliderSize: number;
  295. /** Volume slider sensitivity - the smaller this number, the finer the volume control */
  296. volumeSliderStep: number;
  297. /** Volume slider scroll wheel sensitivity */
  298. volumeSliderScrollStep: number;
  299. /** Whether the volume should be locked to the same level across all tabs (changing in one changes in all others too) */
  300. volumeSharedBetweenTabs: boolean;
  301. /** Whether to set an initial volume level for each new session */
  302. setInitialTabVolume: boolean;
  303. /** The initial volume level to set for each new session */
  304. initialTabVolumeLevel: number;
  305. //#SECTION song lists
  306. /** Add a button to each song in the queue to quickly open its lyrics page */
  307. lyricsQueueButton: boolean;
  308. /** Add a button to each song in the queue to quickly remove it */
  309. deleteFromQueueButton: boolean;
  310. /** Where to place the buttons in the queue */
  311. listButtonsPlacement: "queueOnly" | "everywhere";
  312. /** Add a button to the queue to scroll to the currently playing song */
  313. scrollToActiveSongBtn: boolean;
  314. //#SECTION behavior
  315. /** Whether to completely disable the popup that sometimes appears before leaving the site */
  316. disableBeforeUnloadPopup: boolean;
  317. /** After how many milliseconds to close permanent toasts */
  318. closeToastsTimeout: number;
  319. /** Remember the last song's time when reloading or restoring the tab */
  320. rememberSongTime: boolean;
  321. /** Where to remember the song time */
  322. rememberSongTimeSites: Domain | "all";
  323. /** Time in seconds to remember the song time for */
  324. rememberSongTimeDuration: number;
  325. /** Time in seconds to subtract from the remembered song time */
  326. rememberSongTimeReduction: number;
  327. /** Minimum time in seconds the song needs to be played before it is remembered */
  328. rememberSongTimeMinPlayTime: number;
  329. //#SECTION input
  330. /** Arrow keys skip forwards and backwards */
  331. arrowKeySupport: boolean;
  332. /** By how many seconds to skip when pressing the arrow keys */
  333. arrowKeySkipBy: number;
  334. /** Add a hotkey to switch between the YT and YTM sites on a video / song */
  335. switchBetweenSites: boolean;
  336. /** The hotkey that needs to be pressed to initiate the site switch */
  337. switchSitesHotkey: HotkeyObj;
  338. /** Make it so middle clicking a song to open it in a new tab (through thumbnail and song title) is easier */
  339. anchorImprovements: boolean;
  340. //#SECTION lyrics
  341. /** Add a button to the media controls to open the current song's lyrics on genius.com in a new tab */
  342. geniusLyrics: boolean;
  343. /** Base URL to use for GeniURL */
  344. geniUrlBase: string;
  345. /** Token to use for GeniURL */
  346. geniUrlToken: string;
  347. /** Max size of lyrics cache */
  348. lyricsCacheMaxSize: number;
  349. /** Max TTL of lyrics cache entries, in ms */
  350. lyricsCacheTTL: number;
  351. /** Button to clear lyrics cache */
  352. clearLyricsCache: undefined;
  353. /** Whether to use advanced filtering when searching for lyrics (exact, exact-ish) */
  354. advancedLyricsFilter: boolean;
  355. //#SECTION misc
  356. /** The locale to use for translations */
  357. locale: TrLocale;
  358. /** Whether to check for updates to the script */
  359. versionCheck: boolean;
  360. /** Button to check for updates */
  361. checkVersionNow: undefined;
  362. /** The console log level - 0 = Debug, 1 = Info */
  363. logLevel: LogLevel;
  364. /** Whether to show advanced settings in the config menu */
  365. advancedMode: boolean;
  366. }