瀏覽代碼

fix: resolve song names with hyphens better

Sven 1 年之前
父節點
當前提交
97fc174def
共有 2 個文件被更改,包括 17 次插入7 次删除
  1. 6 3
      src/features/layout.ts
  2. 11 4
      src/features/lyrics.ts

+ 6 - 3
src/features/layout.ts

@@ -5,7 +5,7 @@ import { scriptInfo } from "../constants";
 import { error, getResourceUrl, log } from "../utils";
 import { getEvtData, siteEvents } from "../events";
 import { openMenu } from "./menu/menu_old";
-import { getGeniusUrl, createLyricsBtn, sanitizeArtists, sanitizeSong, getLyricsCacheEntry } from "./lyrics";
+import { getGeniusUrl, createLyricsBtn, sanitizeArtists, sanitizeSong, getLyricsCacheEntry, splitVideoTitle } from "./lyrics";
 import "./layout.css";
 import { featInfo } from ".";
 
@@ -338,7 +338,10 @@ async function addQueueButtons(queueItem: HTMLElement) {
       let lyricsUrl: string | undefined;
       const artistsSan = sanitizeArtists(artist);
       const songSan = sanitizeSong(song);
-      const cachedLyricsUrl = getLyricsCacheEntry(artistsSan, songSan);
+      const splitTitle = splitVideoTitle(songSan);
+      const cachedLyricsUrl = songSan.includes("-")
+        ? getLyricsCacheEntry(splitTitle.artist, splitTitle.song)
+        : getLyricsCacheEntry(artistsSan, songSan);
 
       if(cachedLyricsUrl)
         lyricsUrl = cachedLyricsUrl;
@@ -368,7 +371,7 @@ async function addQueueButtons(queueItem: HTMLElement) {
         if(!lyricsUrl) {
           resetImgElem();
           if(confirm("Couldn't find a lyrics page for this song.\nDo you want to open genius.com to manually search for it?"))
-            openInNewTab("https://genius.com/search");
+            openInNewTab(`https://genius.com/search?q=${encodeURIComponent(`${artistsSan} ${songSan}`)}`);
           return;
         }
       }

+ 11 - 4
src/features/lyrics.ts

@@ -148,7 +148,7 @@ export function sanitizeArtists(artists: string) {
 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")?.getAttribute("video-mode_") === "string";
+    const isVideo = typeof document.querySelector("ytmusic-player")?.hasAttribute("video-mode");
 
     const songTitleElem = document.querySelector(".content-info-wrapper > yt-formatted-string") as HTMLElement;
     const songMetaElem = document.querySelector("span.subtitle > yt-formatted-string:first-child") as HTMLElement;
@@ -165,10 +165,10 @@ export async function getCurrentLyricsUrl() {
     const getGeniusUrlVideo = async () => {
       if(!songName.includes("-")) // for some fucking reason some music videos have YTM-like song title and artist separation, some don't
         return await getGeniusUrl(artistName, songName);
+    
+      const { artist, song } = splitVideoTitle(songName);
 
-      const [artist, ...rest] = songName.split("-").map(v => v.trim());
-
-      return await getGeniusUrl(artist, rest.join(" "));
+      return await getGeniusUrl(artist, song);
     };
 
     // TODO: artist might need further splitting before comma or ampersand
@@ -247,3 +247,10 @@ export async function createLyricsBtn(geniusUrl?: string, hideIfLoading = true)
 
   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("-") };
+}