123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- import { ConfigManager, compress, decompress, fetchAdvanced, insertAfter } from "@sv443-network/userutils";
- import { constructUrlString, error, getResourceUrl, info, log, onSelectorOld, warn, t, tp, compressionSupported } from "../utils";
- import { emitInterface } from "../interface";
- import { compressionFormat, scriptInfo } from "../constants";
- import type { LyricsCacheEntry } from "../types";
- /** Base URL of geniURL */
- export const geniUrlBase = "https://api.sv443.net/geniurl";
- /** GeniURL endpoint that gives song metadata when provided with a `?q` or `?artist` and `?song` parameter - [more info](https://api.sv443.net/geniurl) */
- const geniURLSearchTopUrl = `${geniUrlBase}/search/top`;
- /** Ratelimit budget timeframe in seconds - should reflect what's in geniURL's docs */
- const geniUrlRatelimitTimeframe = 30;
- //#MARKER new cache
- /** How many cache entries can exist at a time - this is used to cap memory usage */
- const maxLyricsCacheSize = 1000;
- /** Maximum time before a cache entry is force deleted */
- const cacheTTL = 1000 * 60 * 60 * 24 * 30; // 30 days
- export type LyricsCache = {
- cache: LyricsCacheEntry[];
- };
- let canCompress = true;
- const lyricsCache = new ConfigManager<LyricsCache>({
- id: "bytm-lyrics-cache",
- defaultConfig: {
- cache: [],
- },
- formatVersion: 1,
- encodeData: (data) => canCompress ? compress(data, compressionFormat, "string") : data,
- decodeData: (data) => canCompress ? decompress(data, compressionFormat, "string") : data,
- });
- export async function initLyricsCache() {
- canCompress = await compressionSupported();
- const data = await lyricsCache.loadData();
- log(`Loaded lyrics cache (${data.cache.length} entries):`, data);
- return data;
- }
- /**
- * Returns the cache entry for the passed artist and song, or undefined if it doesn't exist yet
- * {@linkcode artist} and {@linkcode song} need to be sanitized first!
- * @param refreshEntry If true, the timestamp of the entry will be set to the current time
- */
- export function getLyricsCacheEntry(artist: string, song: string, refreshEntry = true) {
- const { cache } = lyricsCache.getData();
- const entry = cache.find(e => e.artist === artist && e.song === song);
- if(entry && Date.now() - entry?.added > cacheTTL) {
- deleteLyricsCacheEntry(artist, song);
- return undefined;
- }
- // refresh timestamp of the entry by mutating cache
- if(entry && refreshEntry)
- updateLyricsCacheEntry(artist, song);
- return entry;
- }
- function updateLyricsCacheEntry(artist: string, song: string) {
- const { cache } = lyricsCache.getData();
- const idx = cache.findIndex(e => e.artist === artist && e.song === song);
- if(idx !== -1) {
- const newEntry = cache.splice(idx, 1)[0]!;
- newEntry.viewed = Date.now();
- lyricsCache.setData({ cache: [ newEntry, ...cache ] });
- }
- }
- function deleteLyricsCacheEntry(artist: string, song: string) {
- const { cache } = lyricsCache.getData();
- const idx = cache.findIndex(e => e.artist === artist && e.song === song);
- if(idx !== -1) {
- cache.splice(idx, 1);
- lyricsCache.setData({ cache });
- }
- }
- /** Returns the full lyrics cache array */
- export function getLyricsCache() {
- return lyricsCache.getData().cache;
- }
- /**
- * Adds the provided entry into the lyrics URL cache, synchronously to RAM and asynchronously to GM storage
- * {@linkcode artist} and {@linkcode song} need to be sanitized first!
- */
- export function addLyricsCacheEntry(artist: string, song: string, url: string) {
- const { cache } = lyricsCache.getData();
- cache.push({
- artist, song, url, viewed: Date.now(), added: Date.now(),
- } satisfies LyricsCacheEntry);
- cache.sort((a, b) => b.viewed - a.viewed);
- if(cache.length > maxLyricsCacheSize)
- cache.pop();
- return lyricsCache.setData({ cache });
- }
- //#MARKER media control bar
- let currentSongTitle = "";
- /** Adds a lyrics button to the media controls bar */
- export async function addMediaCtrlLyricsBtn() {
- onSelectorOld(".middle-controls-buttons ytmusic-like-button-renderer#like-button-renderer", { listener: addActualMediaCtrlLyricsBtn });
- }
- /** Actually adds the lyrics button after the like button renderer has been verified to exist */
- async function addActualMediaCtrlLyricsBtn(likeContainer: HTMLElement) {
- const songTitleElem = document.querySelector<HTMLDivElement>(".content-info-wrapper > yt-formatted-string");
- if(!songTitleElem)
- return warn("Couldn't find song title element");
- // run parallel without awaiting so the MutationObserver below can observe the title element in time
- (async () => {
- const gUrl = await getCurrentLyricsUrl();
- const linkElem = await createLyricsBtn(gUrl ?? undefined);
- linkElem.id = "betterytm-lyrics-button";
- log("Inserted lyrics button into media controls bar");
- insertAfter(likeContainer, linkElem);
- })();
- currentSongTitle = songTitleElem.title;
- const spinnerIconUrl = await getResourceUrl("img-spinner");
- const lyricsIconUrl = await getResourceUrl("img-lyrics");
- const errorIconUrl = await getResourceUrl("img-error");
- const onMutation = async (mutations: MutationRecord[]) => {
- for await(const mut of mutations) {
- const newTitle = (mut.target as HTMLElement).title;
- if(newTitle !== currentSongTitle && newTitle.length > 0) {
- const lyricsBtn = document.querySelector<HTMLAnchorElement>("#betterytm-lyrics-button");
- if(!lyricsBtn)
- continue;
- info(`Song title changed from '${currentSongTitle}' to '${newTitle}'`);
- lyricsBtn.style.cursor = "wait";
- lyricsBtn.style.pointerEvents = "none";
- const imgElem = lyricsBtn.querySelector<HTMLImageElement>("img")!;
- imgElem.src = spinnerIconUrl;
- imgElem.classList.add("bytm-spinner");
- currentSongTitle = newTitle;
- const url = await getCurrentLyricsUrl(); // can take a second or two
- imgElem.src = lyricsIconUrl;
- imgElem.classList.remove("bytm-spinner");
- if(!url) {
- let artist, song;
- if("mediaSession" in navigator && navigator.mediaSession.metadata) {
- artist = navigator.mediaSession.metadata.artist;
- song = navigator.mediaSession.metadata.title;
- }
- const query = artist && song ? "?q=" + encodeURIComponent(sanitizeArtists(artist) + " - " + sanitizeSong(song)) : "";
- imgElem.src = errorIconUrl;
- imgElem.ariaLabel = imgElem.title = t("lyrics_not_found_click_open_search");
- lyricsBtn.style.cursor = "pointer";
- lyricsBtn.style.pointerEvents = "all";
- lyricsBtn.style.display = "inline-flex";
- lyricsBtn.style.visibility = "visible";
- lyricsBtn.href = `https://genius.com/search${query}`;
- continue;
- }
- lyricsBtn.href = url;
- lyricsBtn.ariaLabel = lyricsBtn.title = t("open_current_lyrics");
- lyricsBtn.style.cursor = "pointer";
- lyricsBtn.style.visibility = "visible";
- lyricsBtn.style.display = "inline-flex";
- lyricsBtn.style.pointerEvents = "initial";
- }
- }
- };
- // since YT and YTM don't reload the page on video change, MutationObserver needs to be used to watch for changes in the video title
- const obs = new MutationObserver(onMutation);
- obs.observe(songTitleElem, { attributes: true, attributeFilter: [ "title" ] });
- }
- //#MARKER utils
- /** Removes everything in parentheses from the passed song name */
- export function sanitizeSong(songName: string) {
- const parensRegex = /\(.+\)/gmi;
- const squareParensRegex = /\[.+\]/gmi;
- // trim right after the song name:
- const sanitized = songName
- .replace(parensRegex, "")
- .replace(squareParensRegex, "");
- return sanitized.trim();
- }
- /** Removes the secondary artist (if it exists) from the passed artists string */
- export function sanitizeArtists(artists: string) {
- artists = artists.split(/\s*\u2022\s*/gmiu)[0]; // split at • [•] character
- if(artists.match(/&/))
- artists = artists.split(/\s*&\s*/gm)[0];
- if(artists.match(/,/))
- artists = artists.split(/,\s*/gm)[0];
- return artists.trim();
- }
- /** Returns the lyrics URL from genius for the currently selected song */
- export async function getCurrentLyricsUrl() {
- try {
- // In videos the video title contains both artist and song title, in "regular" YTM songs, the video title only contains the song title
- const isVideo = typeof document.querySelector("ytmusic-player")?.hasAttribute("video-mode");
- const songTitleElem = document.querySelector<HTMLElement>(".content-info-wrapper > yt-formatted-string");
- const songMetaElem = document.querySelector<HTMLElement>("span.subtitle > yt-formatted-string :first-child");
- if(!songTitleElem || !songMetaElem)
- return undefined;
- const songNameRaw = songTitleElem.title;
- let songName = songNameRaw;
- let artistName = songMetaElem.textContent;
- if(isVideo) {
- // for some fucking reason some music videos have YTM-like song title and artist separation, some don't
- if(songName.includes("-")) {
- const split = splitVideoTitle(songName);
- songName = split.song;
- artistName = split.artist;
- }
- }
- if(!artistName)
- return undefined;
- const url = await fetchLyricsUrl(sanitizeArtists(artistName), sanitizeSong(songName));
- if(url) {
- emitInterface("bytm:lyricsLoaded", {
- type: "current",
- artists: artistName,
- title: songName,
- url,
- });
- }
- return url;
- }
- catch(err) {
- error("Couldn't resolve lyrics URL:", err);
- return undefined;
- }
- }
- /** Fetches the actual lyrics URL from geniURL - **the passed parameters need to be sanitized first!** */
- export async function fetchLyricsUrl(artist: string, song: string): Promise<string | undefined> {
- try {
- const cacheEntry = getLyricsCacheEntry(artist, song);
- if(cacheEntry) {
- info(`Found lyrics URL in cache: ${cacheEntry.url}`);
- return cacheEntry.url;
- }
- const startTs = Date.now();
- const fetchUrl = constructUrlString(geniURLSearchTopUrl, {
- disableFuzzy: null,
- utm_source: "BetterYTM",
- utm_content: `v${scriptInfo.version}`,
- artist,
- song,
- });
- log(`Requesting URL from geniURL at '${fetchUrl}'`);
- const fetchRes = await fetchAdvanced(fetchUrl);
- if(fetchRes.status === 429) {
- const waitSeconds = Number(fetchRes.headers.get("retry-after") ?? geniUrlRatelimitTimeframe);
- alert(tp("lyrics_rate_limited", waitSeconds, waitSeconds));
- return undefined;
- }
- else if(fetchRes.status < 200 || fetchRes.status >= 300) {
- error(`Couldn't fetch lyrics URL from geniURL - status: ${fetchRes.status} - response: ${(await fetchRes.json()).message ?? await fetchRes.text() ?? "(none)"}`);
- return undefined;
- }
- const result = await fetchRes.json();
- if(typeof result === "object" && result.error) {
- error("Couldn't fetch lyrics URL:", result.message);
- return undefined;
- }
- const url = result.url;
- info(`Found lyrics URL (after ${Date.now() - startTs}ms): ${url}`);
- addLyricsCacheEntry(artist, song, url);
- return url;
- }
- catch(err) {
- error("Couldn't get lyrics URL due to error:", err);
- return undefined;
- }
- }
- /** Creates the base lyrics button element */
- export async function createLyricsBtn(geniusUrl?: string, hideIfLoading = true) {
- const linkElem = document.createElement("a");
- linkElem.className = "ytmusic-player-bar bytm-generic-btn";
- linkElem.ariaLabel = linkElem.title = geniusUrl ? t("open_lyrics") : t("lyrics_loading");
- if(geniusUrl)
- linkElem.href = geniusUrl;
- linkElem.role = "button";
- linkElem.target = "_blank";
- linkElem.rel = "noopener noreferrer";
- linkElem.style.visibility = hideIfLoading && geniusUrl ? "initial" : "hidden";
- linkElem.style.display = hideIfLoading && geniusUrl ? "inline-flex" : "none";
- const imgElem = document.createElement("img");
- imgElem.className = "bytm-generic-btn-img";
- imgElem.src = await getResourceUrl("img-lyrics");
- linkElem.appendChild(imgElem);
- return linkElem;
- }
- /** Splits a video title that contains a hyphen into an artist and song */
- export function splitVideoTitle(title: string) {
- const [artist, ...rest] = title.split("-").map((v, i) => i < 2 ? v.trim() : v);
- return { artist, song: rest.join("-") };
- }
|