types.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. import type * as consts from "./constants.js";
  2. import type { scriptInfo } from "./constants.js";
  3. import type { addSelectorListener } from "./observers.js";
  4. import type resources from "../assets/resources.json";
  5. import type locales from "../assets/locales.json";
  6. import type { getResourceUrl, getSessionId, getVideoTime, TrLocale, t, tp, NanoEmitter } from "./utils/index.js";
  7. import type { getFeatures, setFeatures } from "./config.js";
  8. import type { SiteEventsMap } from "./siteEvents.js";
  9. import type { InterfaceEventsMap } from "./interface.js";
  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 selection option between one of the supported domains, or all of them */
  26. export type SiteSelection = Domain | "all";
  27. /** A selection option between one of the supported domains, or none of them */
  28. export type SiteSelectionOrNone = SiteSelection | "none";
  29. /** Key of a resource in `assets/resources.json` and extra keys defined by `tools/post-build.ts` */
  30. export type ResourceKey = keyof typeof resources | `trans-${keyof typeof locales}` | "changelog" | "css-bundle";
  31. /** Describes a single hotkey */
  32. export type HotkeyObj = {
  33. code: string,
  34. shift: boolean,
  35. ctrl: boolean,
  36. alt: boolean,
  37. };
  38. export type LyricsCacheEntry = {
  39. artist: string;
  40. song: string;
  41. url: string;
  42. viewed: number;
  43. added: number;
  44. };
  45. //#region global
  46. // shim for the BYTM interface properties
  47. export type BytmObject =
  48. {
  49. [key: string]: unknown;
  50. locale: TrLocale;
  51. logLevel: LogLevel;
  52. }
  53. // information from the userscript header
  54. & typeof scriptInfo
  55. // certain variables from `src/constants.ts`
  56. & Pick<typeof consts, "mode" | "branch" | "host" | "buildNumber" | "compressionFormat">
  57. // global functions exposed through the interface in `src/interface.ts`
  58. & InterfaceFunctions
  59. // others
  60. & {
  61. // the entire UserUtils library
  62. UserUtils: typeof import("@sv443-network/userutils");
  63. };
  64. declare global {
  65. interface Window {
  66. // to see the expanded type, install the VS Code extension "MylesMurphy.prettify-ts" and hover over the property below
  67. // alternatively navigate with ctrl+click to find the types
  68. BYTM: BytmObject;
  69. }
  70. }
  71. //#region plugins
  72. /**
  73. * Intents (permissions) BYTM has to grant your plugin for it to be able to access certain features.
  74. * TODO: this feature is unfinished, but you should still specify the intents your plugin needs.
  75. * Never request more permissions than you need, as this is a bad practice and can lead to your plugin being rejected.
  76. */
  77. export enum PluginIntent {
  78. /** Plugin has access to hidden config values */
  79. HiddenConfigValues = 1,
  80. /** Plugin can write to the feature configuration */
  81. WriteFeatureConfig = 2,
  82. /** Plugin can write to the lyrics cache */
  83. WriteLyricsCache = 4,
  84. /** Plugin can add new translations and overwrite existing ones */
  85. WriteTranslations = 8,
  86. /** Plugin can create modal dialogs */
  87. CreateModalDialogs = 16,
  88. }
  89. /** Result of a plugin registration */
  90. export type PluginRegisterResult = {
  91. /** Public info about the registered plugin */
  92. info: PluginInfo;
  93. /** NanoEmitter instance for plugin events - see {@linkcode PluginEventMap} for a list of events */
  94. events: NanoEmitter<PluginEventMap>;
  95. /** Authentication token for the plugin to use in certain restricted function calls */
  96. token: string;
  97. }
  98. /** Minimal object that describes a plugin - this is all info the other installed plugins can see */
  99. export type PluginInfo = {
  100. /** Name of the plugin */
  101. name: string;
  102. /**
  103. * Adding the namespace and the name property makes the unique identifier for a plugin.
  104. * If one exists with the same name and namespace as this plugin, it may be overwritten at registration.
  105. * I recommend to set this value to a URL pointing to your homepage, or the author's username.
  106. */
  107. namespace: string;
  108. /** Version of the plugin as a semver-compliant string */
  109. version: string;
  110. };
  111. /** Minimum part of the PluginDef object needed to make up the resolvable plugin identifier */
  112. export type PluginDefResolvable = PluginDef | { plugin: Pick<PluginDef["plugin"], "name" | "namespace"> };
  113. /** An object that describes a BYTM plugin */
  114. export type PluginDef = {
  115. plugin: PluginInfo & {
  116. /**
  117. * Descriptions of at least en_US and optionally any other locale supported by BYTM.
  118. * When an untranslated locale is set, the description will default to the value of en_US
  119. */
  120. description: Partial<Record<keyof typeof locales, string>> & {
  121. en_US: string;
  122. };
  123. /** URL to the plugin's icon - recommended size: 48x48 to 128x128 */
  124. iconUrl?: string;
  125. license?: {
  126. /** License name */
  127. name: string;
  128. /** URL to the license text */
  129. url: string;
  130. };
  131. /** Homepage URLs for the plugin */
  132. homepage: {
  133. /** Any other homepage URL */
  134. other?: string;
  135. /** URL to the plugin's source code (i.e. Git repo) - closed source plugins are not officially accepted at the moment. */
  136. source: string;
  137. /** URL to the plugin's bug tracker page, like GitHub issues */
  138. bug?: string;
  139. /** URL to the plugin's GreasyFork page */
  140. greasyfork?: string;
  141. /** URL to the plugin's OpenUserJS page */
  142. openuserjs?: string;
  143. };
  144. };
  145. /** Intents (permissions) BYTM has to grant the plugin for it to work */
  146. intents?: Array<PluginIntent>;
  147. /** Info about the plugin contributors */
  148. contributors?: Array<{
  149. /** Name of this contributor */
  150. name: string;
  151. /** (optional) Email address of this contributor */
  152. email?: string;
  153. /** (optional) URL to this plugin contributor's homepage / GitHub profile */
  154. url?: string;
  155. }>;
  156. };
  157. /** All events that are dispatched to plugins individually, including everything in {@linkcode SiteEventsMap} and {@linkcode InterfaceEventsMap} - these don't have a prefix since they can't conflict with other events */
  158. export type PluginEventMap =
  159. // Emitted on each plugin individually:
  160. & {
  161. /** Emitted when the plugin is registered on BYTM's side */
  162. pluginRegistered: (info: PluginInfo) => void;
  163. }
  164. // Emitted on every plugin simultaneously:
  165. & SiteEventsMap
  166. & InterfaceEventsMap;
  167. /** A plugin in either the queue or registered map */
  168. export type PluginItem =
  169. & {
  170. def: PluginDef;
  171. }
  172. & Pick<PluginRegisterResult, "events">;
  173. /** All functions exposed by the interface on the global `BYTM` object */
  174. export type InterfaceFunctions = {
  175. /** Adds a listener to one of the already present SelectorObserver instances */
  176. addSelectorListener: typeof addSelectorListener;
  177. /**
  178. * Returns the URL of a resource as defined in `assets/resources.json`
  179. * There are also some resources like translation files that get added by `tools/post-build.ts`
  180. *
  181. * The returned URL is a `blob:` URL served up by the userscript extension
  182. * This makes the resource fast to fetch and also prevents CORS issues
  183. */
  184. getResourceUrl: typeof getResourceUrl;
  185. /** Returns the unique session ID for the current tab */
  186. getSessionId: typeof getSessionId;
  187. /**
  188. * Returns the current video time (on both YT and YTM)
  189. * In case it can't be determined on YT, mouse movement is simulated to bring up the video time
  190. * 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
  191. */
  192. getVideoTime: typeof getVideoTime;
  193. /** Returns the translation for the provided translation key and set locale (check the files in the folder `assets/translations`) */
  194. t: typeof t;
  195. /** Returns the translation for the provided translation key, including pluralization identifier and set locale (check the files in the folder `assets/translations`) */
  196. tp: typeof tp;
  197. /** Returns the current feature configuration */
  198. getFeatures: typeof getFeatures;
  199. /** Overwrites the feature configuration with the provided one */
  200. saveFeatures: typeof setFeatures;
  201. };
  202. //#region features
  203. export type FeatureKey = keyof FeatureConfig;
  204. export type FeatureCategory =
  205. | "layout"
  206. | "volume"
  207. | "songLists"
  208. | "behavior"
  209. | "input"
  210. | "lyrics"
  211. | "general";
  212. type SelectOption = {
  213. value: string | number;
  214. label: string;
  215. };
  216. type FeatureTypeProps = ({
  217. type: "toggle";
  218. default: boolean;
  219. } & FeatureFuncProps)
  220. | ({
  221. type: "number";
  222. default: number;
  223. min: number;
  224. max?: number;
  225. step?: number;
  226. unit?: string | ((val: number) => string);
  227. } & FeatureFuncProps)
  228. | ({
  229. type: "select";
  230. default: string | number;
  231. options: SelectOption[] | (() => SelectOption[]);
  232. } & FeatureFuncProps)
  233. | ({
  234. type: "slider";
  235. default: number;
  236. min: number;
  237. max: number;
  238. step?: number;
  239. unit?: string | ((val: number) => string);
  240. } & FeatureFuncProps)
  241. | ({
  242. type: "hotkey";
  243. default: HotkeyObj;
  244. } & FeatureFuncProps)
  245. | ({
  246. type: "text";
  247. default: string;
  248. normalize?: (val: string) => string;
  249. } & FeatureFuncProps)
  250. | {
  251. type: "button";
  252. default?: undefined;
  253. click: () => Promise<void | unknown> | void | unknown;
  254. }
  255. type FeatureFuncProps = (
  256. {
  257. /** Whether the feature requires a page reload to take effect */
  258. reloadRequired: false;
  259. /** Called to instantiate the feature on the page */
  260. enable: (featCfg: FeatureConfig) => void,
  261. }
  262. | {
  263. /** Whether the feature requires a page reload to take effect */
  264. reloadRequired?: true;
  265. /** Called to instantiate the feature on the page */
  266. enable?: undefined;
  267. }
  268. ) & (
  269. {
  270. /** Called to remove all traces of the feature from the page and memory (includes event listeners) */
  271. disable?: (feats: FeatureConfig) => void,
  272. }
  273. | {
  274. /** Called to update the feature's behavior when the config changes */
  275. change?: (key: FeatureKey, initialVal: number | boolean | Record<string, unknown>, newVal: number | boolean | Record<string, unknown>) => void,
  276. }
  277. );
  278. /**
  279. * The feature info object that contains all properties necessary to construct the config menu and the feature config object.
  280. * All values are loosely typed so try to only use this with the `satisfies` keyword.
  281. * Use `typeof featInfo` (from `src/features/index.ts`) instead for full type safety.
  282. */
  283. export type FeatureInfo = Record<
  284. keyof FeatureConfig,
  285. {
  286. category: FeatureCategory;
  287. /**
  288. * HTML string that will be the help text for this feature
  289. * Specifying a function is useful for pluralizing or inserting values into the translation at runtime
  290. */
  291. helpText?: string | (() => string);
  292. /** Whether the value should be hidden in the config menu and from plugins */
  293. valueHidden?: boolean;
  294. /** Transformation function called before the value is rendered in the config menu */
  295. renderValue?: (value: string) => string | Promise<string>;
  296. /** HTML string that is appended to the end of a feature's text description */
  297. textAdornment?: () => (Promise<string | undefined> | string | undefined);
  298. /** Whether to only show this feature when advanced mode is activated (default false) */
  299. advanced?: boolean;
  300. }
  301. & FeatureTypeProps
  302. >;
  303. /** Feature configuration */
  304. export interface FeatureConfig {
  305. //#region layout
  306. /** Show a BetterYTM watermark under the YTM logo */
  307. watermarkEnabled: boolean;
  308. /** Remove the "si" tracking parameter from links in the share menu? */
  309. removeShareTrackingParam: boolean;
  310. /** On which sites to remove the "si" tracking parameter from links in the share menu */
  311. removeShareTrackingParamSites: SiteSelection;
  312. /** Enable skipping to a specific time in the video by pressing a number key (0-9) */
  313. numKeysSkipToTime: boolean;
  314. /** Fix spacing issues in the layout */
  315. fixSpacing: boolean;
  316. /** Remove the \"Upgrade\" / YT Music Premium tab */
  317. removeUpgradeTab: boolean;
  318. /** Where to show a thumbnail overlay over the video element and whether to show it at all */
  319. thumbnailOverlayBehavior: "never" | "videosOnly" | "songsOnly" | "always";
  320. /** Whether to show a button to toggle the thumbnail overlay in the media controls */
  321. thumbnailOverlayToggleBtnShown: boolean;
  322. /** Whether to show an indicator on the thumbnail overlay when it is active */
  323. thumbnailOverlayShowIndicator: boolean;
  324. /** The opacity of the thumbnail overlay indicator element */
  325. thumbnailOverlayIndicatorOpacity: number;
  326. /** How to fit the thumbnail overlay image */
  327. thumbnailOverlayImageFit: "cover" | "contain" | "fill";
  328. /** Hide the cursor when it's idling on the video element for a while */
  329. hideCursorOnIdle: boolean;
  330. /** Delay in seconds after which the cursor should be hidden */
  331. hideCursorOnIdleDelay: number;
  332. /** Whether to fix various issues in the layout when HDR is supported and active */
  333. fixHdrIssues: boolean;
  334. /** On which sites to disable Dark Reader - does nothing if the extension is not installed */
  335. disableDarkReaderSites: SiteSelectionOrNone;
  336. /** Whether to show the like/dislike ratio on the currently playing song */
  337. showVotes: boolean;
  338. /** Whether to show a bar graph of the like/dislike ratio on the currently playing song and which design it should use */
  339. showVoteRatio: "disabled" | "redGreen" | "blueGray";
  340. //#region volume
  341. /** Add a percentage label to the volume slider */
  342. volumeSliderLabel: boolean;
  343. /** The width of the volume slider in pixels */
  344. volumeSliderSize: number;
  345. /** Volume slider sensitivity - the smaller this number, the finer the volume control */
  346. volumeSliderStep: number;
  347. /** Volume slider scroll wheel sensitivity */
  348. volumeSliderScrollStep: number;
  349. /** Whether the volume should be locked to the same level across all tabs (changing in one changes in all others too) */
  350. volumeSharedBetweenTabs: boolean;
  351. /** Whether to set an initial volume level for each new session */
  352. setInitialTabVolume: boolean;
  353. /** The initial volume level to set for each new session */
  354. initialTabVolumeLevel: number;
  355. //#region song lists
  356. /** Add a button to each song in the queue to quickly open its lyrics page */
  357. lyricsQueueButton: boolean;
  358. /** Add a button to each song in the queue to quickly remove it */
  359. deleteFromQueueButton: boolean;
  360. /** Where to place the buttons in the queue */
  361. listButtonsPlacement: "queueOnly" | "everywhere";
  362. /** Add a button above the queue to scroll to the currently playing song */
  363. scrollToActiveSongBtn: boolean;
  364. /** Add a button above the queue to clear it */
  365. clearQueueBtn: boolean;
  366. //#region behavior
  367. /** Whether to completely disable the popup that sometimes appears before leaving the site */
  368. disableBeforeUnloadPopup: boolean;
  369. /** After how many milliseconds to close permanent toasts */
  370. closeToastsTimeout: number;
  371. /** Remember the last song's time when reloading or restoring the tab */
  372. rememberSongTime: boolean;
  373. /** Where to remember the song time */
  374. rememberSongTimeSites: SiteSelection;
  375. /** Time in seconds to remember the song time for */
  376. rememberSongTimeDuration: number;
  377. /** Time in seconds to subtract from the remembered song time */
  378. rememberSongTimeReduction: number;
  379. /** Minimum time in seconds the song needs to be played before it is remembered */
  380. rememberSongTimeMinPlayTime: number;
  381. //#region input
  382. /** Arrow keys skip forwards and backwards */
  383. arrowKeySupport: boolean;
  384. /** By how many seconds to skip when pressing the arrow keys */
  385. arrowKeySkipBy: number;
  386. /** Add a hotkey to switch between the YT and YTM sites on a video / song */
  387. switchBetweenSites: boolean;
  388. /** The hotkey that needs to be pressed to initiate the site switch */
  389. switchSitesHotkey: HotkeyObj;
  390. /** Make it so middle clicking a song to open it in a new tab (through thumbnail and song title) is easier */
  391. anchorImprovements: boolean;
  392. /** Whether to auto-like all played videos of configured channels */
  393. autoLikeChannels: boolean;
  394. /** Whether to show toggle buttons on the channel page to enable/disable auto-liking for that channel */
  395. autoLikeChannelToggleBtn: boolean;
  396. // TODO(v2.2):
  397. // /** Whether to show a toggle button in the media controls to enable/disable auto-liking for those channel(s) */
  398. // autoLikePlayerBarToggleBtn: boolean;
  399. /** How long to wait after a video has started playing to auto-like it */
  400. autoLikeTimeout: number;
  401. /** Whether to show a toast when a video is auto-liked */
  402. autoLikeShowToast: boolean;
  403. /** Opens the auto-like channels management dialog */
  404. autoLikeOpenMgmtDialog: undefined;
  405. //#region lyrics
  406. /** Add a button to the media controls to open the current song's lyrics on genius.com in a new tab */
  407. geniusLyrics: boolean;
  408. /** Base URL to use for GeniURL */
  409. geniUrlBase: string;
  410. /** Token to use for GeniURL */
  411. geniUrlToken: string;
  412. /** Max size of lyrics cache */
  413. lyricsCacheMaxSize: number;
  414. /** Max TTL of lyrics cache entries, in ms */
  415. lyricsCacheTTL: number;
  416. /** Button to clear lyrics cache */
  417. clearLyricsCache: undefined;
  418. /** Whether to use advanced filtering when searching for lyrics (exact, exact-ish) */
  419. advancedLyricsFilter: boolean;
  420. //#region misc
  421. /** The locale to use for translations */
  422. locale: TrLocale;
  423. /** Whether to default to US-English if the translation for the set locale is missing */
  424. localeFallback: boolean;
  425. /** Whether to check for updates to the script */
  426. versionCheck: boolean;
  427. /** Button to check for updates */
  428. checkVersionNow: undefined;
  429. /** The console log level - 0 = Debug, 1 = Info */
  430. logLevel: LogLevel;
  431. /** Amount of seconds until the feature initialization times out */
  432. initTimeout: number;
  433. /** Amount of seconds to show BYTM's toasts for */
  434. toastDuration: number;
  435. /** Button that resets the config to the default state */
  436. resetConfig: undefined;
  437. /** Whether to show advanced settings in the config menu */
  438. advancedMode: boolean;
  439. }