Skip to content

Commit 11a0d39

Browse files
committed
fix(song-info-front): fix nullable issue
1 parent d5a5ed3 commit 11a0d39

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

src/providers/song-info-front.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { singleton } from './decorators';
22

33
import type { YoutubePlayer } from '@/types/youtube-player';
44
import type { GetState } from '@/types/datahost-get-state';
5-
import type { AlbumDetails, VideoDataChangeValue } from '@/types/player-api-events';
5+
import type { AlbumDetails, PlayerOverlays, VideoDataChangeValue } from '@/types/player-api-events';
66

77
import type { SongInfo } from './song-info';
88
import type { VideoDataChanged } from '@/types/video-data-changed';
@@ -208,28 +208,35 @@ export default (api: YoutubePlayer) => {
208208
list: playlistId
209209
} = api.getVideoData();
210210

211-
const { playerOverlays } = api.getWatchNextResponse();
211+
const watchNextResponse = api.getWatchNextResponse();
212212

213-
sendSongInfo(<VideoDataChangeValue>{
213+
sendSongInfo({
214214
title, author, videoId, playlistId,
215215

216216
isUpcoming: false,
217217
lengthSeconds: video.duration,
218218
loading: true,
219219

220-
uhhh: { playerOverlays }
221-
});
220+
ytmdWatchNextResponse: watchNextResponse,
221+
} satisfies VideoDataChangeValue);
222222
}
223223
}
224224

225225
function sendSongInfo(videoData: VideoDataChangeValue) {
226226
const data = api.getPlayerResponse();
227227

228-
data.videoDetails.album = (
229-
Object.entries(videoData).find(
230-
([, value]) => value && Object.hasOwn(value, 'playerOverlays'),
231-
) as [string, AlbumDetails | undefined]
232-
)?.[1]?.playerOverlays?.playerOverlayRenderer?.browserMediaSession?.browserMediaSessionRenderer?.album?.runs?.at(
228+
let playerOverlay: PlayerOverlays | undefined;
229+
230+
if (!videoData.ytmdWatchNextResponse) {
231+
playerOverlay = (
232+
Object.entries(videoData).find(
233+
([, value]) => value && Object.hasOwn(value, 'playerOverlays'),
234+
) as [string, AlbumDetails | undefined]
235+
)?.[1]?.playerOverlays;
236+
} else {
237+
playerOverlay = videoData.ytmdWatchNextResponse?.playerOverlays;
238+
}
239+
data.videoDetails.album = playerOverlay?.playerOverlayRenderer?.browserMediaSession?.browserMediaSessionRenderer?.album?.runs?.at(
233240
0,
234241
)?.text;
235242
data.videoDetails.elapsedSeconds = 0;

src/types/player-api-events.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { WatchNextResponse } from '@/types/youtube-music-desktop-internal';
2+
13
export interface AlbumDetails {
24
responseContext: ResponseContext;
35
contents: Contents;
@@ -252,6 +254,11 @@ export type VideoDataChangeValue = Record<string, unknown> & {
252254
loading: boolean;
253255

254256
lengthSeconds: number;
257+
258+
/**
259+
* YouTube Music Desktop internal variable (for album data)
260+
**/
261+
ytmdWatchNextResponse?: WatchNextResponse;
255262
};
256263

257264
export interface PlayerAPIEvents {
+2-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { QueueItem } from '@/types/datahost-get-state';
2+
import type { PlayerOverlays } from '@/types/player-api-events';
23

34
export interface QueueResponse {
45
items?: QueueItem[];
@@ -7,15 +8,5 @@ export interface QueueResponse {
78
}
89

910
export interface WatchNextResponse {
10-
playerOverlays: {
11-
playerOverlayRenderer: {
12-
browserMediaSession: {
13-
browserMediaSessionRenderer: {
14-
album: {
15-
runs: { text: string; }[]
16-
}
17-
}
18-
}
19-
}
20-
};
11+
playerOverlays?: PlayerOverlays;
2112
}

0 commit comments

Comments
 (0)