Skip to content

Commit f342697

Browse files
committed
feat: remove spotify built in lyrics
1 parent 83e2b99 commit f342697

File tree

6 files changed

+126
-135
lines changed

6 files changed

+126
-135
lines changed

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spotify-lyrics",
3-
"version": "1.6.3",
3+
"version": "1.6.4",
44
"description": "Desktop Spotify Web Player Instant Synchronized Lyrics",
55
"scripts": {
66
"lint": "tsc --noEmit && eslint --ext .ts --fix src/",

Diff for: public/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/extend-chrome/manifest-json-schema/main/schema/manifest.schema.json",
33
"name": "__MSG_extensionName__",
4-
"version": "1.6.3",
4+
"version": "1.6.4",
55
"manifest_version": 3,
66
"description": "__MSG_extensionDescription__",
77
"default_locale": "en",

Diff for: src/page/lyrics.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import { captureException } from './utils';
1010
export interface Query {
1111
name: string;
1212
artists: string;
13-
/**sec */
14-
duration?: number;
1513
}
1614

1715
export interface Artist {
@@ -177,7 +175,7 @@ async function fetchSongList(s: string, fetchOptions?: RequestInit): Promise<Son
177175

178176
interface MatchingLyricsOptions {
179177
onlySearchName?: boolean;
180-
getAudioElement?: () => HTMLAudioElement | Promise<HTMLAudioElement>;
178+
getDuration?: () => Promise<number>;
181179
fetchData?: (s: string, fetchOptions?: RequestInit) => Promise<Song[]>;
182180
fetchTransName?: (s: string, fetchOptions?: RequestInit) => Promise<Record<string, string>>;
183181
fetchOptions?: RequestInit;
@@ -188,21 +186,14 @@ export async function matchingLyrics(
188186
): Promise<{ list: Song[]; id: number; score: number }> {
189187
const { name = '', artists = '' } = query;
190188
const {
191-
getAudioElement,
189+
getDuration,
192190
onlySearchName = false,
193191
fetchData = fetchSongList,
194192
fetchTransName = fetchChineseName,
195193
fetchOptions,
196194
} = options;
197195

198-
let duration = query.duration || 0;
199-
if (getAudioElement && !duration) {
200-
const audio = await getAudioElement();
201-
if (!audio.duration) {
202-
await new Promise((res) => audio.addEventListener('loadedmetadata', res, { once: true }));
203-
duration = audio.duration;
204-
}
205-
}
196+
const duration = (await getDuration?.()) || 0;
206197

207198
const queryName = normalize(name);
208199
const queryName1 = queryName.toLowerCase();
@@ -361,7 +352,7 @@ export async function matchingLyrics(
361352
list: listForMissingName,
362353
score: scoreForMissingName,
363354
} = await matchingLyrics(query, {
364-
getAudioElement,
355+
getDuration,
365356
onlySearchName: true,
366357
fetchData,
367358
fetchTransName: async () => singerAlias,

Diff for: src/page/observer.ts

+32-31
Original file line numberDiff line numberDiff line change
@@ -130,42 +130,43 @@ const originFetch = globalThis.fetch;
130130

131131
let latestHeader = new Headers();
132132

133-
// Priority to detect track switching through API
134-
// Priority to use build-in lyrics through API
135133
globalThis.fetch = async (...rest) => {
136134
const res = await originFetch(...rest);
137135
const url = new URL(rest[0] instanceof Request ? rest[0].url : rest[0], location.origin);
138-
latestHeader = new Headers(rest[0] instanceof Request ? rest[0].headers : rest[1]?.headers);
139136
const spotifyAPI = 'https://spclient.wg.spotify.com';
140137
if (url.origin === spotifyAPI && url.pathname.startsWith('/metadata/4/track/')) {
141-
const metadata: SpotifyTrackMetadata = await res.clone().json();
142-
const { name = '', artist = [], duration = 0, canonical_uri, has_lyrics } = metadata || {};
143-
const trackId = canonical_uri?.match(/spotify:track:([^:]*)/)?.[1];
144-
// match artists element textContent
145-
const artists = artist?.map((e) => e?.name).join(', ');
146-
sharedData.cacheTrackAndLyrics({
147-
name,
148-
artists,
149-
duration: duration / 1000,
150-
getLyrics: has_lyrics
151-
? async () => {
152-
const res = await fetch(`${spotifyAPI}/lyrics/v1/track/${trackId}?market=from_token`, {
153-
headers: latestHeader,
154-
});
155-
const spLyrics: SpotifyTrackLyrics = await res.json();
156-
if (spLyrics.kind === 'LINE') {
157-
return spLyrics.lines
158-
.map(({ time, words }) =>
159-
words.map(({ string }) => ({
160-
startTime: time / 1000,
161-
text: string,
162-
})),
163-
)
164-
.flat();
165-
}
166-
}
167-
: undefined,
168-
});
138+
latestHeader = new Headers(rest[0] instanceof Request ? rest[0].headers : rest[1]?.headers);
139+
140+
// const metadata: SpotifyTrackMetadata = await res.clone().json();
141+
// const { name = '', artist = [], duration = 0, canonical_uri, has_lyrics } = metadata || {};
142+
// const trackId = canonical_uri?.match(/spotify:track:([^:]*)/)?.[1];
143+
// // match artists element textContent
144+
// const artists = artist?.map((e) => e?.name).join(', ');
145+
// sharedData.cacheTrackAndLyrics({
146+
// name,
147+
// artists,
148+
// duration: duration / 1000,
149+
// getLyrics: has_lyrics
150+
// ? async ({ signal }) => {
151+
// const res = await fetch(`${spotifyAPI}/lyrics/v1/track/${trackId}?market=from_token`, {
152+
// headers: latestHeader,
153+
// signal,
154+
// });
155+
// if (!res.ok) return '';
156+
// const spLyrics: SpotifyTrackLyrics = await res.json();
157+
// if (spLyrics.kind !== 'LINE') return '';
158+
// return spLyrics.lines
159+
// .map(({ time, words }) =>
160+
// words.map(({ string }) => {
161+
// const sec = time / 1000;
162+
// return `[${Math.floor(sec / 60)}:${sec % 60}]\n${string}`;
163+
// }),
164+
// )
165+
// .flat()
166+
// .join('\n');
167+
// }
168+
// : undefined,
169+
// });
169170
}
170171
return res;
171172
};

Diff for: src/page/rate.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,16 @@ const key = 'spotify.lyrics.test';
267267

268268
const listPromise = new Promise<Query[]>((res, rej) => {
269269
const fn = () => {
270-
const querys = [...document.querySelectorAll('.tracklist li')].map((item) => {
270+
const queryList = [...document.querySelectorAll('.tracklist li')].map((item) => {
271271
return {
272272
name: item.querySelector('.tracklist-name')?.textContent || '',
273273
artists: item.querySelector('.TrackListRow__artists')?.textContent || '',
274274
};
275275
});
276-
if (!querys.length) {
276+
if (!queryList.length) {
277277
setTimeout(fn, 100);
278278
} else {
279-
res(querys);
279+
res(queryList);
280280
}
281281
};
282282
setTimeout(rej, 5000);
@@ -300,14 +300,14 @@ window.addEventListener('load', async () => {
300300
detail: [],
301301
};
302302

303-
const querys = await listPromise;
303+
const queryList = await listPromise;
304304

305305
// test
306-
querys.length = 1;
306+
queryList.length = 1;
307307

308308
await Promise.all(
309-
querys.map(async (query, i) => {
310-
console.log(`${i + 1}/${querys.length} matching: `, query);
309+
queryList.map(async (query, i) => {
310+
console.log(`${i + 1}/${queryList.length} matching: `, query);
311311
const { id } = await matchingLyrics(query);
312312
if (id === 0) {
313313
data[location.pathname].noMatch++;

0 commit comments

Comments
 (0)