fix(#1543): fix song control doesn't work (#1637)

Co-authored-by: Su-Yong <simssy2205@gmail.com>
This commit is contained in:
JellyBrick
2024-01-13 09:06:41 +09:00
committed by GitHub
parent e73584c2aa
commit 1a89fbe612
2 changed files with 61 additions and 59 deletions

View File

@ -1,66 +1,35 @@
// This is used for to control the songs // This is used for to control the songs
import { BrowserWindow } from 'electron'; import { BrowserWindow, ipcMain } from 'electron';
type Modifiers = (
| Electron.MouseInputEvent
| Electron.MouseWheelInputEvent
| Electron.KeyboardInputEvent
)['modifiers'];
export const pressKey = (
window: BrowserWindow,
key: string,
modifiers: Modifiers = [],
) => {
window.webContents.sendInputEvent({
type: 'keyDown',
modifiers,
keyCode: key,
});
};
export default (win: BrowserWindow) => { export default (win: BrowserWindow) => {
const commands = { const commands = {
// Playback // Playback
previous: () => pressKey(win, 'k'), previous: () => win.webContents.send('ytmd:previous-video'),
next: () => pressKey(win, 'j'), next: () => win.webContents.send('ytmd:next-video'),
playPause: () => pressKey(win, ';'), playPause: () => win.webContents.send('ytmd:toggle-play'),
like: () => pressKey(win, '+'), like: () => win.webContents.send('ytmd:update-like', 'LIKE'),
dislike: () => pressKey(win, '_'), dislike: () => win.webContents.send('ytmd:update-like', 'DISLIKE'),
go10sBack: () => pressKey(win, 'h'), go10sBack: () => win.webContents.send('ytmd:seek-by', -10),
go10sForward: () => pressKey(win, 'l'), go10sForward: () => win.webContents.send('ytmd:seek-by', 10),
go1sBack: () => pressKey(win, 'h', ['shift']), go1sBack: () => win.webContents.send('ytmd:seek-by', -1),
go1sForward: () => pressKey(win, 'l', ['shift']), go1sForward: () => win.webContents.send('ytmd:seek-by', 1),
shuffle: () => pressKey(win, 's'), shuffle: () => win.webContents.send('ytmd:shuffle'),
switchRepeat(n = 1) { switchRepeat: (n = 1) => win.webContents.send('ytmd:switch-repeat', n),
for (let i = 0; i < n; i++) { // General
pressKey(win, 'r'); volumeMinus10: () => {
} ipcMain.once('ytmd:get-volume-return', (_, volume) => {
}, win.webContents.send('ytmd:update-volume', volume - 10);
// General });
volumeMinus10: () => pressKey(win, '-'), win.webContents.send('ytmd:get-volume');
volumePlus10: () => pressKey(win, '='), },
fullscreen: () => pressKey(win, 'f'), volumePlus10: () => {
muteUnmute: () => pressKey(win, 'm'), ipcMain.once('ytmd:get-volume-return', (_, volume) => {
maximizeMinimisePlayer: () => pressKey(win, 'q'), win.webContents.send('ytmd:update-volume', volume + 10);
// Navigation });
goToHome() { win.webContents.send('ytmd:get-volume');
pressKey(win, 'g'); },
pressKey(win, 'h'); fullscreen: () => win.webContents.send('ytmd:toggle-fullscreen'),
}, muteUnmute: () => win.webContents.send('ytmd:toggle-mute'),
goToLibrary() {
pressKey(win, 'g');
pressKey(win, 'l');
},
goToSettings() {
pressKey(win, 'g');
pressKey(win, ',');
},
goToExplore() {
pressKey(win, 'g');
pressKey(win, 'e');
},
search: () => pressKey(win, '/'),
showShortcuts: () => pressKey(win, '/', ['shift']),
}; };
return { return {
...commands, ...commands,

View File

@ -37,8 +37,41 @@ interface YouTubeMusicAppElement extends HTMLElement {
} }
async function onApiLoaded() { async function onApiLoaded() {
window.ipcRenderer.on('ytmd:previous-video', () => {
document.querySelector<HTMLElement>('.previous-button.ytmusic-player-bar')?.click();
});
window.ipcRenderer.on('ytmd:next-video', () => {
document.querySelector<HTMLElement>('.next-button.ytmusic-player-bar')?.click();
});
window.ipcRenderer.on('ytmd:toggle-play', (_) => {
if (api?.getPlayerState() === 2) api?.playVideo();
else api?.pauseVideo();
});
window.ipcRenderer.on('ytmd:seek-to', (_, t: number) => api!.seekTo(t)); window.ipcRenderer.on('ytmd:seek-to', (_, t: number) => api!.seekTo(t));
window.ipcRenderer.on('ytmd:seek-by', (_, t: number) => api!.seekBy(t)); window.ipcRenderer.on('ytmd:seek-by', (_, t: number) => api!.seekBy(t));
window.ipcRenderer.on('ytmd:shuffle', () => {
document.querySelector<HTMLElement & { queue: { shuffle: () => void } }>('ytmusic-player-bar')?.queue.shuffle();
});
window.ipcRenderer.on('ytmd:update-like', (_, status: 'LIKE' | 'DISLIKE' = 'LIKE') => {
document.querySelector<HTMLElement & { updateLikeStatus: (status: string) => void }>('#like-button-renderer')?.updateLikeStatus(status);
});
window.ipcRenderer.on('ytmd:switch-repeat', (_, repeat = 1) => {
for (let i = 0; i < repeat; i++) {
document.querySelector<HTMLElement & { onRepeatButtonTap: () => void }>('ytmusic-player-bar')?.onRepeatButtonTap();
}
});
window.ipcRenderer.on('ytmd:update-volume', (_, volume: number) => {
document.querySelector<HTMLElement & { updateVolume: (volume: number) => void }>('ytmusic-player-bar')?.updateVolume(volume);
});
window.ipcRenderer.on('ytmd:get-volume', (event) => {
event.sender.emit('ytmd:get-volume-return', api?.getVolume());
});
window.ipcRenderer.on('ytmd:toggle-fullscreen', (_) => {
document.querySelector<HTMLElement & { toggleFullscreen: () => void }>('ytmusic-player-bar')?.toggleFullscreen();
});
window.ipcRenderer.on('ytmd:toggle-mute', (_) => {
document.querySelector<HTMLElement & { onVolumeTap: () => void }>('ytmusic-player-bar')?.onVolumeTap();
});
const video = document.querySelector('video')!; const video = document.querySelector('video')!;
const audioContext = new AudioContext(); const audioContext = new AudioContext();