mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 19:31:46 +00:00
fix: fix bugs in MPRIS, and improve MPRIS (#1760)
Co-authored-by: JellyBrick <shlee1503@naver.com> Co-authored-by: Totto <32566573+Totto16@users.noreply.github.com>
This commit is contained in:
@ -6,7 +6,7 @@ import getSongControls from './song-controls';
|
||||
|
||||
export const APP_PROTOCOL = 'youtubemusic';
|
||||
|
||||
let protocolHandler: ((cmd: string) => void) | undefined;
|
||||
let protocolHandler: ((cmd: string, args: string[] | undefined) => void) | undefined;
|
||||
|
||||
export function setupProtocolHandler(win: BrowserWindow) {
|
||||
if (process.defaultApp && process.argv.length >= 2) {
|
||||
@ -19,18 +19,18 @@ export function setupProtocolHandler(win: BrowserWindow) {
|
||||
|
||||
const songControls = getSongControls(win);
|
||||
|
||||
protocolHandler = ((cmd: keyof typeof songControls) => {
|
||||
protocolHandler = ((cmd: keyof typeof songControls, args: string[] | undefined = undefined) => {
|
||||
if (Object.keys(songControls).includes(cmd)) {
|
||||
songControls[cmd]();
|
||||
songControls[cmd](args as never);
|
||||
}
|
||||
}) as (cmd: string) => void;
|
||||
}
|
||||
|
||||
export function handleProtocol(cmd: string) {
|
||||
protocolHandler?.(cmd);
|
||||
export function handleProtocol(cmd: string, args: string[] | undefined) {
|
||||
protocolHandler?.(cmd, args);
|
||||
}
|
||||
|
||||
export function changeProtocolHandler(f: (cmd: string) => void) {
|
||||
export function changeProtocolHandler(f: (cmd: string, args: string[] | undefined) => void) {
|
||||
protocolHandler = f;
|
||||
}
|
||||
|
||||
|
||||
@ -1,43 +1,82 @@
|
||||
// This is used for to control the songs
|
||||
import { BrowserWindow, ipcMain } from 'electron';
|
||||
import { BrowserWindow } from 'electron';
|
||||
|
||||
// see protocol-handler.ts
|
||||
type ArgsType<T> = T | string[] | undefined;
|
||||
|
||||
const parseNumberFromArgsType = (args: ArgsType<number>) => {
|
||||
if (typeof args === 'number') {
|
||||
return args;
|
||||
} else if (Array.isArray(args)) {
|
||||
return Number(args[0]);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const parseBooleanFromArgsType = (args: ArgsType<boolean>) => {
|
||||
if (typeof args === 'boolean') {
|
||||
return args;
|
||||
} else if (Array.isArray(args)) {
|
||||
return args[0] === 'true';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export default (win: BrowserWindow) => {
|
||||
const commands = {
|
||||
return {
|
||||
// Playback
|
||||
previous: () => win.webContents.send('ytmd:previous-video'),
|
||||
next: () => win.webContents.send('ytmd:next-video'),
|
||||
playPause: () => win.webContents.send('ytmd:toggle-play'),
|
||||
like: () => win.webContents.send('ytmd:update-like', 'LIKE'),
|
||||
dislike: () => win.webContents.send('ytmd:update-like', 'DISLIKE'),
|
||||
go10sBack: () => win.webContents.send('ytmd:seek-by', -10),
|
||||
go10sForward: () => win.webContents.send('ytmd:seek-by', 10),
|
||||
go1sBack: () => win.webContents.send('ytmd:seek-by', -1),
|
||||
go1sForward: () => win.webContents.send('ytmd:seek-by', 1),
|
||||
goBack: (seconds: ArgsType<number>) => {
|
||||
const secondsNumber = parseNumberFromArgsType(seconds);
|
||||
if (secondsNumber !== null) {
|
||||
win.webContents.send('ytmd:seek-by', -secondsNumber);
|
||||
}
|
||||
},
|
||||
goForward: (seconds: ArgsType<number>) => {
|
||||
const secondsNumber = parseNumberFromArgsType(seconds);
|
||||
if (secondsNumber !== null) {
|
||||
win.webContents.send('ytmd:seek-by', seconds);
|
||||
}
|
||||
},
|
||||
shuffle: () => win.webContents.send('ytmd:shuffle'),
|
||||
switchRepeat: (n = 1) => win.webContents.send('ytmd:switch-repeat', n),
|
||||
switchRepeat: (n: ArgsType<number> = 1) => {
|
||||
const repeat = parseNumberFromArgsType(n);
|
||||
if (repeat !== null) {
|
||||
win.webContents.send('ytmd:switch-repeat', n);
|
||||
}
|
||||
},
|
||||
// General
|
||||
volumeMinus10: () => {
|
||||
ipcMain.once('ytmd:get-volume-return', (_, volume) => {
|
||||
win.webContents.send('ytmd:update-volume', volume - 10);
|
||||
});
|
||||
win.webContents.send('ytmd:get-volume');
|
||||
setVolume: (volume: ArgsType<number>) => {
|
||||
const volumeNumber = parseNumberFromArgsType(volume);
|
||||
if (volumeNumber !== null) {
|
||||
win.webContents.send('ytmd:update-volume', volume);
|
||||
}
|
||||
},
|
||||
volumePlus10: () => {
|
||||
ipcMain.once('ytmd:get-volume-return', (_, volume) => {
|
||||
win.webContents.send('ytmd:update-volume', volume + 10);
|
||||
});
|
||||
win.webContents.send('ytmd:get-volume');
|
||||
setFullscreen: (isFullscreen: ArgsType<boolean>) => {
|
||||
const isFullscreenValue = parseBooleanFromArgsType(isFullscreen);
|
||||
if (isFullscreenValue !== null) {
|
||||
win.setFullScreen(isFullscreenValue);
|
||||
win.webContents.send('ytmd:click-fullscreen-button', isFullscreenValue);
|
||||
}
|
||||
},
|
||||
requestFullscreenInformation: () => {
|
||||
win.webContents.send('ytmd:get-fullscreen');
|
||||
},
|
||||
requestQueueInformation: () => {
|
||||
win.webContents.send('ytmd:get-queue');
|
||||
},
|
||||
fullscreen: () => win.webContents.send('ytmd:toggle-fullscreen'),
|
||||
muteUnmute: () => win.webContents.send('ytmd:toggle-mute'),
|
||||
search: () => win.webContents.sendInputEvent({
|
||||
type: 'keyDown',
|
||||
keyCode: '/',
|
||||
}),
|
||||
};
|
||||
return {
|
||||
...commands,
|
||||
play: commands.playPause,
|
||||
pause: commands.playPause,
|
||||
search: () => {
|
||||
win.webContents.sendInputEvent({
|
||||
type: 'keyDown',
|
||||
keyCode: '/',
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@ -62,11 +62,13 @@ export const setupRepeatChangedListener = singleton(() => {
|
||||
// provided by YouTube Music
|
||||
window.ipcRenderer.send(
|
||||
'ytmd:repeat-changed',
|
||||
document.querySelector<
|
||||
HTMLElement & {
|
||||
getState: () => GetState;
|
||||
}
|
||||
>('ytmusic-player-bar')?.getState().queue.repeatMode,
|
||||
document
|
||||
.querySelector<
|
||||
HTMLElement & {
|
||||
getState: () => GetState;
|
||||
}
|
||||
>('ytmusic-player-bar')
|
||||
?.getState().queue.repeatMode,
|
||||
);
|
||||
});
|
||||
|
||||
@ -78,6 +80,46 @@ export const setupVolumeChangedListener = singleton((api: YoutubePlayer) => {
|
||||
window.ipcRenderer.send('ytmd:volume-changed', api.getVolume());
|
||||
});
|
||||
|
||||
export const setupFullScreenChangedListener = singleton(() => {
|
||||
const playerBar = document.querySelector('ytmusic-player-bar');
|
||||
|
||||
if (!playerBar) {
|
||||
window.ipcRenderer.send('ytmd:fullscreen-changed-supported', false);
|
||||
return;
|
||||
}
|
||||
|
||||
const observer = new MutationObserver(() => {
|
||||
window.ipcRenderer.send(
|
||||
'ytmd:fullscreen-changed',
|
||||
(
|
||||
playerBar?.attributes.getNamedItem('player-fullscreened') ?? null
|
||||
) !== null,
|
||||
);
|
||||
});
|
||||
|
||||
observer.observe(playerBar, {
|
||||
attributes: true,
|
||||
childList: false,
|
||||
subtree: false,
|
||||
});
|
||||
});
|
||||
|
||||
export const setupAutoPlayChangedListener = singleton(() => {
|
||||
const autoplaySlider = document.querySelector<HTMLInputElement>(
|
||||
'.autoplay > tp-yt-paper-toggle-button',
|
||||
);
|
||||
|
||||
const observer = new MutationObserver(() => {
|
||||
window.ipcRenderer.send('ytmd:autoplay-changed');
|
||||
});
|
||||
|
||||
observer.observe(autoplaySlider!, {
|
||||
attributes: true,
|
||||
childList: false,
|
||||
subtree: false,
|
||||
});
|
||||
});
|
||||
|
||||
export default (api: YoutubePlayer) => {
|
||||
window.ipcRenderer.on('ytmd:setup-time-changed-listener', () => {
|
||||
setupTimeChangedListener();
|
||||
@ -91,6 +133,14 @@ export default (api: YoutubePlayer) => {
|
||||
setupVolumeChangedListener(api);
|
||||
});
|
||||
|
||||
window.ipcRenderer.on('ytmd:setup-fullscreen-changed-listener', () => {
|
||||
setupFullScreenChangedListener();
|
||||
});
|
||||
|
||||
window.ipcRenderer.on('ytmd:setup-autoplay-changed-listener', () => {
|
||||
setupAutoPlayChangedListener();
|
||||
});
|
||||
|
||||
window.ipcRenderer.on('ytmd:setup-seeked-listener', () => {
|
||||
setupSeekedListener();
|
||||
});
|
||||
@ -155,13 +205,13 @@ export default (api: YoutubePlayer) => {
|
||||
function sendSongInfo(videoData: VideoDataChangeValue) {
|
||||
const data = api.getPlayerResponse();
|
||||
|
||||
data.videoDetails.album =
|
||||
(
|
||||
Object.entries(videoData)
|
||||
.find(([, value]) => value && Object.hasOwn(value, 'playerOverlays')) as [string, AlbumDetails | undefined]
|
||||
)?.[1]?.playerOverlays?.playerOverlayRenderer?.browserMediaSession?.browserMediaSessionRenderer?.album?.runs?.at(
|
||||
0,
|
||||
)?.text;
|
||||
data.videoDetails.album = (
|
||||
Object.entries(videoData).find(
|
||||
([, value]) => value && Object.hasOwn(value, 'playerOverlays'),
|
||||
) as [string, AlbumDetails | undefined]
|
||||
)?.[1]?.playerOverlays?.playerOverlayRenderer?.browserMediaSession?.browserMediaSessionRenderer?.album?.runs?.at(
|
||||
0,
|
||||
)?.text;
|
||||
data.videoDetails.elapsedSeconds = 0;
|
||||
data.videoDetails.isPaused = false;
|
||||
|
||||
|
||||
@ -120,7 +120,9 @@ const handleData = async (
|
||||
songInfo.mediaType = MediaType.PodcastEpisode;
|
||||
// HACK: Podcast's participant is not the artist
|
||||
if (!config.get('options.usePodcastParticipantAsArtist')) {
|
||||
songInfo.artist = cleanupName(data.microformat.microformatDataRenderer.pageOwnerDetails.name);
|
||||
songInfo.artist = cleanupName(
|
||||
data.microformat.microformatDataRenderer.pageOwnerDetails.name,
|
||||
);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -128,14 +130,13 @@ const handleData = async (
|
||||
// HACK: This is a workaround for "podcast" types where "musicVideoType" doesn't exist. Google :facepalm:
|
||||
if (
|
||||
!config.get('options.usePodcastParticipantAsArtist') &&
|
||||
(
|
||||
data.responseContext.serviceTrackingParams
|
||||
?.at(0)
|
||||
?.params
|
||||
?.find((it) => it.key === 'ipcc')?.value ?? '1'
|
||||
) != '0'
|
||||
(data.responseContext.serviceTrackingParams
|
||||
?.at(0)
|
||||
?.params?.find((it) => it.key === 'ipcc')?.value ?? '1') != '0'
|
||||
) {
|
||||
songInfo.artist = cleanupName(data.microformat.microformatDataRenderer.pageOwnerDetails.name);
|
||||
songInfo.artist = cleanupName(
|
||||
data.microformat.microformatDataRenderer.pageOwnerDetails.name,
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -165,10 +166,12 @@ const registerProvider = (win: BrowserWindow) => {
|
||||
|
||||
// This will be called when the song-info-front finds a new request with song data
|
||||
ipcMain.on('ytmd:video-src-changed', async (_, data: GetPlayerResponse) => {
|
||||
const tempSongInfo = await dataMutex.runExclusive<SongInfo | null>(async () => {
|
||||
songInfo = await handleData(data, win);
|
||||
return songInfo;
|
||||
});
|
||||
const tempSongInfo = await dataMutex.runExclusive<SongInfo | null>(
|
||||
async () => {
|
||||
songInfo = await handleData(data, win);
|
||||
return songInfo;
|
||||
},
|
||||
);
|
||||
|
||||
if (tempSongInfo) {
|
||||
for (const c of callbacks) {
|
||||
|
||||
Reference in New Issue
Block a user