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:
REWHEX
2025-09-05 18:59:39 +04:00
committed by GitHub
parent 96ea114335
commit 8b10872e83
7 changed files with 117 additions and 23 deletions

View File

@ -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();
});