diff --git a/plugins/downloader/front.js b/plugins/downloader/front.js index 77485d4a..17b2846b 100644 --- a/plugins/downloader/front.js +++ b/plugins/downloader/front.js @@ -35,7 +35,7 @@ const reinit = () => { // contextBridge.exposeInMainWorld("downloader", { // download: () => { global.download = () => { - const videoUrl = window.location.href; + const videoUrl = global.songInfo.url || window.location.href; downloadVideoToMP3( videoUrl, diff --git a/plugins/downloader/menu.js b/plugins/downloader/menu.js index 06a3c124..b4800808 100644 --- a/plugins/downloader/menu.js +++ b/plugins/downloader/menu.js @@ -7,71 +7,84 @@ const is = require("electron-is"); const ytpl = require("ytpl"); const { setOptions } = require("../../config/plugins"); +const getSongInfo = require("../../providers/song-info"); const { sendError } = require("./back"); const { defaultMenuDownloadLabel, getFolder } = require("./utils"); let downloadLabel = defaultMenuDownloadLabel; +let metadataURL = undefined; +let callbackIsRegistered = false; -module.exports = (win, options, refreshMenu) => [ - { - label: downloadLabel, - click: async () => { - const currentURL = win.webContents.getURL(); - const playlistID = new URL(currentURL).searchParams.get("list"); - if (!playlistID) { - sendError(win, new Error("No playlist ID found")); - return; - } +module.exports = (win, options, refreshMenu) => { + if (!callbackIsRegistered) { + const registerCallback = getSongInfo(win); + registerCallback((info) => { + metadataURL = info.url; + }); + callbackIsRegistered = true; + } - const playlist = await ytpl(playlistID); - const playlistTitle = playlist.title; + return [ + { + label: downloadLabel, + click: async () => { + const currentURL = metadataURL || win.webContents.getURL(); + const playlistID = new URL(currentURL).searchParams.get("list"); + if (!playlistID) { + sendError(win, new Error("No playlist ID found")); + return; + } - const folder = getFolder(options.downloadFolder); - const playlistFolder = join(folder, playlistTitle); - if (existsSync(playlistFolder)) { - sendError( - win, - new Error(`The folder ${playlistFolder} already exists`) - ); - return; - } - mkdirSync(playlistFolder, { recursive: true }); + const playlist = await ytpl(playlistID); + const playlistTitle = playlist.title; - ipcMain.on("downloader-feedback", (_, feedback) => { - downloadLabel = feedback; + const folder = getFolder(options.downloadFolder); + const playlistFolder = join(folder, playlistTitle); + if (existsSync(playlistFolder)) { + sendError( + win, + new Error(`The folder ${playlistFolder} already exists`) + ); + return; + } + mkdirSync(playlistFolder, { recursive: true }); + + ipcMain.on("downloader-feedback", (_, feedback) => { + downloadLabel = feedback; + refreshMenu(); + }); + + downloadLabel = `Downloading "${playlistTitle}"`; refreshMenu(); - }); - downloadLabel = `Downloading "${playlistTitle}"`; - refreshMenu(); + if (is.dev()) { + console.log( + `Downloading playlist "${playlistTitle}" (${playlist.items.length} songs)` + ); + } - if (is.dev()) { - console.log( - `Downloading playlist "${playlistTitle}" (${playlist.items.length} songs)` - ); - } - - playlist.items.slice(0, options.playlistMaxItems).forEach((song) => { - win.webContents.send( - "downloader-download-playlist", - song, - playlistTitle, - options - ); - }); + playlist.items.slice(0, options.playlistMaxItems).forEach((song) => { + win.webContents.send( + "downloader-download-playlist", + song, + playlistTitle, + options + ); + }); + }, }, - }, - { - label: "Choose download folder", - click: () => { - let result = dialog.showOpenDialogSync({ - properties: ["openDirectory", "createDirectory"], - defaultPath: getFolder(options.downloadFolder), - }); - if (result) { - options.downloadFolder = result[0]; - setOptions("downloader", options); - } // else = user pressed cancel + { + label: "Choose download folder", + click: () => { + let result = dialog.showOpenDialogSync({ + properties: ["openDirectory", "createDirectory"], + defaultPath: getFolder(options.downloadFolder), + }); + if (result) { + options.downloadFolder = result[0]; + setOptions("downloader", options); + } // else = user pressed cancel + }, }, - }, -]; + ]; +};