浏览代码

ref: clean up stuff

Sv443 1 年之前
父节点
当前提交
b6ab6966fa
共有 2 个文件被更改,包括 9 次插入3 次删除
  1. 1 1
      src/events.ts
  2. 8 2
      src/features/lyrics.ts

+ 1 - 1
src/events.ts

@@ -1,7 +1,7 @@
 import { Event as EventParam, EventEmitter, EventHandler } from "@billjs/event-emitter";
 import { error, info } from "./utils";
 
-export interface SiteEvents extends EventEmitter {
+interface SiteEvents extends EventEmitter {
   /** Emitted whenever child nodes are added to or removed from the song queue */
   on(event: "queueChanged", listener: EventHandler): boolean;
   /** Emitted whenever child nodes are added to or removed from the autoplay queue underneath the song queue */

+ 8 - 2
src/features/lyrics.ts

@@ -17,7 +17,7 @@ const thresholdParam = threshold ? `&threshold=${clamp(threshold, 0, 1)}` : "";
 
 //#MARKER cache
 
-/** Cache with key format `ARTIST - SONG` and lyrics URLs as values. Used to prevent extraneous requests to geniURL. */
+/** Cache with key format `ARTIST - SONG` (sanitized) and lyrics URLs as values. Used to prevent extraneous requests to geniURL. */
 const lyricsUrlCache = new Map<string, string>();
 /** How many cache entries can exist at a time - this is used to cap memory usage */
 const maxLyricsCacheSize = 100;
@@ -179,6 +179,12 @@ export async function getCurrentLyricsUrl() {
 /** Fetches the actual lyrics URL from geniURL - **the passed parameters need to be sanitized first!** */
 export async function getGeniusUrl(artist: string, song: string): Promise<string | undefined> {
   try {
+    const cacheEntry = getLyricsCacheEntry(artist, song);
+    if(cacheEntry) {
+      info(`Found lyrics URL in cache: ${cacheEntry}`);
+      return cacheEntry;
+    }
+
     const startTs = Date.now();
     const fetchUrl = `${geniURLSearchTopUrl}?artist=${encodeURIComponent(artist)}&song=${encodeURIComponent(song)}${thresholdParam}`;
 
@@ -208,7 +214,7 @@ export async function getGeniusUrl(artist: string, song: string): Promise<string
 export function createLyricsBtn(geniusUrl?: string, hideIfLoading = true): HTMLAnchorElement {
   const linkElem = document.createElement("a");
   linkElem.className = "ytmusic-player-bar bytm-generic-lyrics-btn";
-  linkElem.title = geniusUrl ? "Click to open this song's lyrics in a new tab" : "Loading...";
+  linkElem.title = geniusUrl ? "Click to open this song's lyrics in a new tab" : "Loading lyrics URL...";
   if(geniusUrl)
     linkElem.href = geniusUrl;
   linkElem.role = "button";