Skip to content

Commit

Permalink
fix(#1543): fix song control doesn't work (#1637)
Browse files Browse the repository at this point in the history
Co-authored-by: Su-Yong <simssy2205@gmail.com>
  • Loading branch information
JellyBrick and Su-Yong authored Jan 13, 2024
1 parent e73584c commit 1a89fbe
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 59 deletions.
87 changes: 28 additions & 59 deletions src/providers/song-controls.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,35 @@
// This is used for to control the songs
import { BrowserWindow } 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,
});
};
import { BrowserWindow, ipcMain } from 'electron';

export default (win: BrowserWindow) => {
const commands = {
// Playback
previous: () => pressKey(win, 'k'),
next: () => pressKey(win, 'j'),
playPause: () => pressKey(win, ';'),
like: () => pressKey(win, '+'),
dislike: () => pressKey(win, '_'),
go10sBack: () => pressKey(win, 'h'),
go10sForward: () => pressKey(win, 'l'),
go1sBack: () => pressKey(win, 'h', ['shift']),
go1sForward: () => pressKey(win, 'l', ['shift']),
shuffle: () => pressKey(win, 's'),
switchRepeat(n = 1) {
for (let i = 0; i < n; i++) {
pressKey(win, 'r');
}
},
// General
volumeMinus10: () => pressKey(win, '-'),
volumePlus10: () => pressKey(win, '='),
fullscreen: () => pressKey(win, 'f'),
muteUnmute: () => pressKey(win, 'm'),
maximizeMinimisePlayer: () => pressKey(win, 'q'),
// Navigation
goToHome() {
pressKey(win, 'g');
pressKey(win, 'h');
},
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']),
// 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),
shuffle: () => win.webContents.send('ytmd:shuffle'),
switchRepeat: (n = 1) => 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');
},
volumePlus10: () => {
ipcMain.once('ytmd:get-volume-return', (_, volume) => {
win.webContents.send('ytmd:update-volume', volume + 10);
});
win.webContents.send('ytmd:get-volume');
},
fullscreen: () => win.webContents.send('ytmd:toggle-fullscreen'),
muteUnmute: () => win.webContents.send('ytmd:toggle-mute'),
};
return {
...commands,
Expand Down
33 changes: 33 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,41 @@ interface YouTubeMusicAppElement extends HTMLElement {
}

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-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 audioContext = new AudioContext();
Expand Down

0 comments on commit 1a89fbe

Please sign in to comment.