From c280221a44608777a69038306a6ea8e92953fb9a Mon Sep 17 00:00:00 2001 From: pan93412 Date: Mon, 27 Dec 2021 19:27:01 +0800 Subject: [PATCH] feat(ipcMain/unm): allow passing customized source --- src/electron/ipcMain.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/electron/ipcMain.js b/src/electron/ipcMain.js index dc1fed6..64e5168 100644 --- a/src/electron/ipcMain.js +++ b/src/electron/ipcMain.js @@ -48,8 +48,18 @@ async function getBiliVideoFile(url) { return `data:application/octet-stream;base64,${encodedData}`; } +/** + * Parse the source string (`a, b`) to source list `['a', 'b']`. + * + * @param {string} sourceString The source string. + * @returns {string[]} The source list. + */ +function parseSourceStringToList(sourceString) { + return sourceString.split(',').map(s => s.trim()); +} + export function initIpcMain(win, store) { - ipcMain.handle('unblock-music', async (_, track) => { + ipcMain.handle('unblock-music', async (_, track, source) => { // 兼容 unblockneteasemusic 所使用的 api 字段 track.alias = track.alia || []; track.duration = track.dt || 0; @@ -62,11 +72,15 @@ export function initIpcMain(win, store) { }, 5000); }); + const sourceList = + typeof source === 'string' ? parseSourceStringToList(source) : null; + log(`[UNM] using source: ${sourceList || ''}`); + try { const matchedAudio = await Promise.race([ // TODO: tell users to install yt-dlp. // we passed "null" to source, to let UNM choose the default source. - match(track.id, null, track), + match(track.id, sourceList, track), timeoutPromise, ]);