mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 11:21:46 +00:00
feat(api-server): Improved api-server volume and like/dislike state (#3592)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: rewhex <gitea@cluser.local> Co-authored-by: JellyBrick <shlee1503@naver.com>
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
// This is used for to control the songs
|
||||
import { type BrowserWindow, ipcMain } from 'electron';
|
||||
import { LikeType } from '@/types/datahost-get-state';
|
||||
|
||||
// see protocol-handler.ts
|
||||
type ArgsType<T> = T | string[] | undefined;
|
||||
@ -42,8 +43,8 @@ export default (win: BrowserWindow) => {
|
||||
play: () => win.webContents.send('ytmd:play'),
|
||||
pause: () => win.webContents.send('ytmd:pause'),
|
||||
playPause: () => win.webContents.send('ytmd:toggle-play'),
|
||||
like: () => win.webContents.send('ytmd:update-like', 'LIKE'),
|
||||
dislike: () => win.webContents.send('ytmd:update-like', 'DISLIKE'),
|
||||
like: () => win.webContents.send('ytmd:update-like', LikeType.Like),
|
||||
dislike: () => win.webContents.send('ytmd:update-like', LikeType.Dislike),
|
||||
seekTo: (seconds: ArgsType<number>) => {
|
||||
const secondsNumber = parseNumberFromArgsType(seconds);
|
||||
if (secondsNumber !== null) {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { singleton } from './decorators';
|
||||
|
||||
import type { YoutubePlayer } from '@/types/youtube-player';
|
||||
import type { GetState } from '@/types/datahost-get-state';
|
||||
import { LikeType, type GetState } from '@/types/datahost-get-state';
|
||||
import type {
|
||||
AlbumDetails,
|
||||
PlayerOverlays,
|
||||
@ -79,12 +79,52 @@ export const setupRepeatChangedListener = singleton(() => {
|
||||
);
|
||||
});
|
||||
|
||||
const mapLikeStatus = (status: string | null): LikeType =>
|
||||
Object.values(LikeType).includes(status as LikeType)
|
||||
? (status as LikeType)
|
||||
: LikeType.Indifferent;
|
||||
|
||||
const LIKE_STATUS_ATTRIBUTE = 'like-status';
|
||||
|
||||
export const setupLikeChangedListener = singleton(() => {
|
||||
const likeDislikeObserver = new MutationObserver((mutations) => {
|
||||
window.ipcRenderer.send(
|
||||
'ytmd:like-changed',
|
||||
mapLikeStatus(
|
||||
(mutations[0].target as HTMLElement)?.getAttribute?.(
|
||||
LIKE_STATUS_ATTRIBUTE,
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
const likeButtonRenderer = document.querySelector('#like-button-renderer');
|
||||
if (likeButtonRenderer) {
|
||||
likeDislikeObserver.observe(likeButtonRenderer, {
|
||||
attributes: true,
|
||||
attributeFilter: [LIKE_STATUS_ATTRIBUTE],
|
||||
});
|
||||
|
||||
// Emit the initial value as well; as it's persistent between launches.
|
||||
window.ipcRenderer.send(
|
||||
'ytmd:like-changed',
|
||||
mapLikeStatus(likeButtonRenderer.getAttribute?.(LIKE_STATUS_ATTRIBUTE)),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export const setupVolumeChangedListener = singleton((api: YoutubePlayer) => {
|
||||
document.querySelector('video')?.addEventListener('volumechange', () => {
|
||||
window.ipcRenderer.send('ytmd:volume-changed', api.getVolume());
|
||||
window.ipcRenderer.send('ytmd:volume-changed', {
|
||||
state: api.getVolume(),
|
||||
isMuted: api.isMuted(),
|
||||
});
|
||||
});
|
||||
|
||||
// Emit the initial value as well; as it's persistent between launches.
|
||||
window.ipcRenderer.send('ytmd:volume-changed', api.getVolume());
|
||||
window.ipcRenderer.send('ytmd:volume-changed', {
|
||||
state: api.getVolume(),
|
||||
isMuted: api.isMuted(),
|
||||
});
|
||||
});
|
||||
|
||||
export const setupShuffleChangedListener = singleton(() => {
|
||||
@ -153,6 +193,10 @@ export default (api: YoutubePlayer) => {
|
||||
setupTimeChangedListener();
|
||||
});
|
||||
|
||||
window.ipcRenderer.on('ytmd:setup-like-changed-listener', () => {
|
||||
setupLikeChangedListener();
|
||||
});
|
||||
|
||||
window.ipcRenderer.on('ytmd:setup-repeat-changed-listener', () => {
|
||||
setupRepeatChangedListener();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user