Thank you for your interest in contributing to BetterYTM!
This guide will help you get started with contributing to the project.
If you have any questions or need help, feel free to contact me, see my homepage for contact info.
Thank you so much for your interest in translating BetterYTM!
Before submitting a translation, please check on this document if the language you want to translate to has already been translated and how many strings are still missing.
To submit a translation, please follow these steps:
assets/translations/en_US.json
en_US
part of the file name with the language code and locale code of the language you want to translate toREADME-summary.md
file for display on the userscript distribution sitesREADME-summary.md
and call it README-summary-languageCode_localeCode.md
and place it in the assets/translations/
folder.en_US.json
file in the folder assets/translations/
by keeping the format languageCode_localeCode.json
assets/locales.json
by copying the english one and editing it (please make sure it's alphabetically ordered)authors
property in assets/locales.json
assets/locales.json
To edit an existing translation, please follow these steps:
assets/translations/
npm run tr-format -- -p -o=languageCode_localeCode
, where languageCode_localeCode
is the part of the file name before the .json
extensionen_US.json
npm run tr-format -- -o=languageCode_localeCode
to make sure the file is formatted correctlynpm run tr-progress
assets/translations/README.md
to see if you're still missing any untranslated keys (you don't have to translate them all, but it would of course be nice)compare:
dropdown to your forkdevelop
branch by running git checkout develop
in the project root.npm i
.env.template
to .env
and modify the variables inside to your needs.npm run dev
to build the userscript and host it on a development server or check out the other commands belownpm i
npm run dev
.env
and src/tools/serve.ts
, just make sure to restart the dev server after changing anything.npm run build-prod
package.json
file.npm run build -- <arguments>
--config-mode=<value>
- The mode to build in. Can be either production
or development
(default)--config-branch=<value>
- The GitHub branch to target. Can be any branch name, but should be main
for production and develop
for development (default)--config-host=<value>
- The host to build for. Can be either github
(default), greasyfork
or openuserjs
--config-assetSource=<value>
- Where to get the resource files from. Can be either local
or github
(default)--config-suffix=<value>
- Suffix to add just before the .user.js
extension. Defaults to an empty string
Shorthand commands:
npm run build-prod-base
- Sets --config-mode=production
and --config-branch=main
npm run build-develop
- Sets --config-mode=development
, --config-branch=develop
and --config-assetSource=github
npm run lint
npm run gen-readme
npm run tr-progress
assets/translations/README.md
npm run tr-format -- <arguments>
en_US.json
--prep
or -p
- Prepares the files for translation via GitHub Copilot by providing the missing key once in English and once without any value--only="<value>"
or -o="<value>"
- Only applies formatting to the files of the specified locales. Has to be a comma separated list (e.g. -o="fr_FR,de_DE"
)--include-based
or -b
- Also includes files which have a base locale specifiednpm run --silent invisible -- "<command>"
--silent
to see npm's info and error messages.npm run node-ts -- <path>
When using ViolentMonkey, after letting the command npm run dev
run in the background, open http://localhost:8710/BetterYTM.user.js
and select the Track local file
option.
This makes it so the userscript automatically updates when the code changes.
Note: the tab needs to stay open on Firefox or the script will not update itself.
BetterYTM has a built-in interface based on events and exposed global constants and functions that allows other userscripts to benefit from its features.
If you want your plugin to be displayed in the readme and possibly inside the userscript itself, please submit an issue using the plugin submission template
These are the ways to interact with BetterYTM; constants, events and global functions:
Static interaction is done through constants that are exposed through the global BYTM
object, which is available on the window
object.
These read-only properties tell you more about how BetterYTM is currently being run.
You can find all properties that are available and their types in the declare global
block of src/types.ts
Dynamic interaction is done through events that are dispatched on the window
object.
They all have the prefix bytm:eventName
and are all dispatched with the CustomEvent
interface, meaning their data can be read using the detail
property.
You can find all events that are available and their types in src/interface.ts
Additionally BetterYTM has an internal system called SiteEvents. They are dispatched using the format bytm:siteEvent:eventName
You may find all SiteEvents that are available and their types in src/siteEvents.ts
Note that the detail
property will be an array of the arguments that can be found in the event handler at the top of src/siteEvents.ts
BYTM
object.InterfaceFunctions
type in src/types.ts
BYTM.UserUtils
is also exposed, which contains all functions from the UserUtils library.All of these interactions require the use of unsafeWindow
, as the regular window object is pretty sandboxed in userscript managers.
If you need specific events to be added or modified, please submit an issue.
For global function examples see below.
In order for TypeScript to not throw errors while creating a plugin, you need to shim the types for BYTM.
To do this, create a .d.ts file (for example bytm.d.ts
) and add the following code:
declare global {
interface Window {
BYTM: {
// add types here
};
}
}
You may specify all types that you need in this file.
To find which types BetterYTM exposes, check out the declare global
block in src/types.ts
You may also just copy it entirely, as long as all the imports also exist in your project.
An easy way to do this might be to include BetterYTM as a Git submodule, as long as you stick to only using type imports
These are the global functions that are exposed by BetterYTM through the unsafeWindow.BYTM
object.
The usage and example blocks on each are written in TypeScript but can be used in JavaScript as well, after removing all type annotations.
blob:
URL provided by the local userscript extension for the specified BYTM resource filegetResourceUrl()
Usage:
unsafeWindow.BYTM.getResourceUrl(): Promise<string>
Description:
Returns ablob:
URL for the specified BYTM resource file.
You can find a list of them by looking at the@resource
directives in the userscript header or in the filesassets/resources.json
andsrc/tools/post-build.ts
The resource and its URL are provided by the userscript extension and it is locally cached for quicker fetching.Should a resource not be defined, the function will return the equivalent URL from the GitHub repository instead.
Should that also fail, it will try to return a base64-encodeddata:
URI version of the resource.Arguments:
resourceName
- The name of the resource to get the URL for.Example (click to expand)
```ts const deleteButtonImg = document.createElement("img"); deleteButtonImg.src = await unsafeWindow.BYTM.getResourceUrl("delete"); myElement.appendChild(deleteButtonImg); ```
getSessionId()
Usage:
unsafeWindow.BYTM.getSessionId(): string | null
Description:
Returns the unique session ID that is generated on every page load.
It should persist between history navigations, but not between page reloads.⚠️ On privacy-focused browsers or if cookies are disabled, this function will return null since sessionStorage is not available.
Example (click to expand)
```ts const sessionId = unsafeWindow.BYTM.getSessionId(); if(await GM.getValue("_myPlugin-sesId") !== sessionId) { console.log("New session started"); // do something that should only be done once per session // or store values persistently that should be unique per session: await GM.setValue("_myPlugin-sesId", sessionId); } ```
addSelectorListener()
Usage:
unsafeWindow.BYTM.addSelectorListener<TElem extends Element>(observerName: ObserverName, selector: string, options: SelectorListenerOptions<TElem>): void
Description:
Adds a listener to the specified SelectorObserver instance that gets called when the element(s) behind the passed selector change.
These instances are created by BetterYTM to observe the DOM for changes.
See the UserUtils SelectorObserver documentation for more info.Arguments:
observerName
- The name of the SelectorObserver instance to add the listener to. You can find all available instances and which parent element they observe in the filesrc/observers.ts
.selector
- The CSS selector to observe for changes.options
- The options for the listener. See the UserUtils SelectorObserver documentationExample (click to expand)
```ts // wait for the observers to exist unsafeWindow.addEventListener("bytm:observersReady", () => { // use the "lowest" possible SelectorObserver (playerBar) // and check if the lyrics button gets added or removed unsafeWindow.BYTM.addSelectorListener("playerBar", "#betterytm-lyrics-button", { listener: (elem) => { console.log("The BYTM lyrics button changed"); }, }); }); ```
getVideoTime()
Usage:
unsafeWindow.BYTM.getVideoTime(): Promise<number | null>
Description:
Returns the current video time (on both YT and YTM).
In case it can't be determined on YT, mouse movement is simulated to bring up the video time element and read it.
In order for that edge case not to throw an error, the function would need to be called in response to a user interaction event (e.g. click) due to the strict automated interaction policy in browsers.
Resolves with a number of seconds ornull
if the time couldn't be determined.Example (click to expand)
```ts try { const videoTime = await unsafeWindow.BYTM.getVideoTime(); console.log(`The video time is ${videoTime}s`); } catch(err) { console.error("Couldn't get the video time, probably due to automated interaction restrictions"); } ```
setLocale()
Usage:
unsafeWindow.BYTM.setLocale(locale: string): void
Description:
Sets the locale for BetterYTM's translations.
The new locale is used for all translations after this function is called.Arguments:
locale
- The locale to set. Refer to the fileassets/locales.json
for a list of available locales.Example (click to expand)
```ts unsafeWindow.BYTM.setLocale("en_UK"); ```
getLocale()
Usage:
unsafeWindow.BYTM.getLocale(): string
Description:
Returns the currently set locale.Example (click to expand)
```ts unsafeWindow.BYTM.getLocale(); // "en_US" unsafeWindow.BYTM.setLocale("en_UK"); unsafeWindow.BYTM.getLocale(); // "en_UK" ```
hasKey()
Usage:
unsafeWindow.BYTM.hasKey(key: string): boolean
Description:
Returns true if the specified translation key exists in the currently set locale.Arguments:
key
- The key of the translation to check for.Example (click to expand)
```ts unsafeWindow.BYTM.hasKey("lyrics_rate_limited"); // true unsafeWindow.BYTM.hasKey("some_key_that_doesnt_exist"); // false ```
hasKeyFor()
Usage:
unsafeWindow.BYTM.hasKeyFor(locale: string, key: string): boolean
Description:
Returns true if the specified translation key exists in the specified locale.Arguments:
locale
- The locale to check for the translation key in.key
- The key of the translation to check for.Example (click to expand)
```ts unsafeWindow.BYTM.hasKeyFor("en_UK", "lyrics_rate_limited"); // true unsafeWindow.BYTM.hasKeyFor("en_UK", "some_key_that_doesnt_exist"); // false ```
t()
Usage:
unsafeWindow.BYTM.t(key: TFuncKey, ...values: Stringifiable[]): string
Description:
Returns the translation for the provided translation key and currently set locale.
To see a list of translations, check the fileassets/translations/en_US.json
Arguments:
translationKey
- The key of the translation to get....values
- A spread parameter of values that can be converted to strings to replace the numbered placeholders in the translation with.Example (click to expand)
```ts const customConfigMenuTitle = document.createElement("div"); customConfigMenuTitle.textContent = unsafeWindow.BYTM.t("config_menu_title", "My cool BYTM Plugin"); // translated text: "My cool BYTM Plugin - Configuration" (if locale is en_US or en_UK) ```
tp()
Usage:
unsafeWindow.BYTM.tp(key: TFuncKey, num: number | unknown[] | NodeList, ...values: Stringifiable[]): string
Description:
Returns the translation for the provided translation key, including pluralization identifier and currently set locale.
To see a list of translations, check the fileassets/translations/en_US.json
The pluralization identifier is determined by the number of items in the second argument.
It can be either "1" or "n" and will be appended to the translation key separated by a hyphen.Arguments:
translationKey
- The key of the translation to get.num
- The number of items to determine the pluralization identifier from. Can also be an array or NodeList....values
- A spread parameter of values that can be converted to strings to replace the numbered placeholders in the translation with.Example (click to expand)
```ts try { const lyrics = await unsafeWindow.BYTM.fetchLyricsUrl("Michael Jackson", "Thriller"); } catch(err) { if(err instanceof Error && err.status === 429) { // rate limited const retryAfter = err.response.headers["retry-after"]; const retryAfterSeconds = retryAfter ? parseInt(retryAfter) : 60; const errorText = unsafeWindow.BYTM.tp("lyrics_rate_limited", retryAfterSeconds); // translation key: "lyrics_rate_limited-n" // translated text: "You are being rate limited.\nPlease wait 23 seconds before requesting more lyrics." alert(errorText); } } ```
getFeatures()
Usage:
unsafeWindow.BYTM.getFeatures(): FeatureConfig
Description:
Returns the current feature configuration object synchronously from memory.
To see the structure of the object, check out the typeFeatureConfig
in the filesrc/types.ts
Example (click to expand)
```ts const features = unsafeWindow.BYTM.getFeatures(); console.log(`The volume slider step is currently set to ${features.volumeSliderStep}`); ```
saveFeatures()
Usage:
unsafeWindow.BYTM.saveFeatures(config: FeatureConfig): Promise<void>
Description:
Overwrites the current feature configuration object with the provided one.
The object in memory is updated synchronously, while the one in GM storage is updated asynchronously once the Promise resolves.Arguments:
config
- The full config object to save. If properties are missing, BYTM will break!
To see the structure of the object, check out the typeFeatureConfig
in the filesrc/types.ts
Example (click to expand)
```ts async function updateVolSliderStep() { const oldConfig = unsafeWindow.BYTM.getFeatures(); const newConfig = { ...oldConfig, volumeSliderStep: 1 }; const promise = unsafeWindow.BYTM.saveFeatures(newConfig); // new config is now saved in memory, but not yet in GM storage // so this already returns the updated config: console.log(unsafeWindow.BYTM.getFeatures()); await promise; // now the data is saved persistently in GM storage and the page can // safely be reloaded without losing the updated config data } updateVolSliderStep(); ```
fetchLyricsUrlTop()
Usage:
unsafeWindow.BYTM.fetchLyricsUrlTop(artist: string, song: string): Promise<string | undefined>
Description:
Fetches the top result's URL to the lyrics page for the specified song.
If there is already an entry in the in-memory cache for the song, it will be returned without fetching anything new.
URLs that are returned by this function are added to the cache automatically.
Returns undefined if there was an error while fetching the URL.Arguments:
artist
- The main artist of the song to fetch the lyrics URL for.
The value needs to be sanitized withsanitizeArtists()
before being passed to this function.song
- The title of the song to fetch the lyrics URL for.
The value needs to be sanitized withsanitizeSong()
before being passed to this function.Example (click to expand)
```ts async function getLyricsUrl() { const lyricsUrl = await unsafeWindow.BYTM.fetchLyricsUrlTop("Michael Jackson", "Thriller"); if(lyricsUrl) console.log(`The lyrics URL for Michael Jackson's Thriller is '${lyricsUrl}'`); else console.log("Couldn't find the lyrics URL for this song"); } getLyricsUrl(); ```
getLyricsCacheEntry()
Usage:
unsafeWindow.BYTM.getLyricsCacheEntry(artists: string, song: string): LyricsCacheEntry | undefined
Description:
Tries to find an entry in the in-memory cache for the specified song.
You can find the structure of theLyricsCacheEntry
type in the filesrc/types.ts
Contrary tofetchLyricsUrlTop()
, this function does not fetch anything new if there is no entry in the cache.Arguments:
artist
- The main artist of the song to grab the lyrics URL for.
The value needs to be sanitized withsanitizeArtists()
before being passed to this function.song
- The title of the song to grab the lyrics URL for.
The value needs to be sanitized withsanitizeSong()
before being passed to this function.Example (click to expand)
```ts function tryToGetLyricsUrl() { const lyricsEntry = unsafeWindow.BYTM.getLyricsCacheEntry("Michael Jackson", "Thriller"); if(lyricsEntry) console.log(`The lyrics URL for Michael Jackson's Thriller is '${lyricsEntry.url}'`); else console.log("Couldn't find the lyrics URL for this song in cache"); } tryToGetLyricsUrl(); ```
sanitizeArtists()
Usage:
unsafeWindow.BYTM.sanitizeArtists(artists: string): string
Description:
Sanitizes the specified artist string to be used in fetching a lyrics URL.
This tries to strip out special characters and co-artist names, separated by a comma or ampersand.
Returns (hopefully) a single artist name with leading and trailing whitespaces trimmed.Arguments:
artists
- The string of artist name(s) to sanitize.Example (click to expand)
```ts // usually artist strings will only have one of these characters but this is just an example const sanitizedArtists = unsafeWindow.BYTM.sanitizeArtists(" Michael Jackson • Paul McCartney & Freddy Mercury, Frank Sinatra"); console.log(sanitizedArtists); // "Michael Jackson" ```
sanitizeSong()
Usage:
unsafeWindow.BYTM.sanitizeSong(songName: string): string
Description:
Sanitizes the specified song title string to be used in fetching a lyrics URL.
This tries to strip out special characters and everything inside regular and square parentheses like(Foo Remix)
.
Returns (hopefully) a song title with leading and trailing whitespaces trimmed.Arguments:
songName
- The string of the song title to sanitize.Example (click to expand)
```ts const sanitizedSong = unsafeWindow.BYTM.sanitizeSong(" Thriller (Freddy Mercury Cover) [Tommy Cash Remix]"); console.log(sanitizedSong); // "Thriller" ```