allow downloading playlists from popup menu

This commit is contained in:
Araxeus
2022-01-11 22:44:06 +02:00
parent ec4c2e92af
commit 5e68d2487f
3 changed files with 111 additions and 82 deletions

View File

@ -1,4 +1,4 @@
const { contextBridge } = require("electron");
const { ipcRenderer } = require("electron");
const { defaultConfig } = require("../../config");
const { getSongMenu } = require("../../providers/dom-elements");
@ -13,15 +13,17 @@ const downloadButton = ElementFromFile(
);
let pluginOptions = {};
const observer = new MutationObserver((mutations, observer) => {
const observer = new MutationObserver(() => {
if (!menu) {
menu = getSongMenu();
if (!menu) return;
}
if (menu.contains(downloadButton)) return;
const menuUrl = document.querySelector('tp-yt-paper-listbox [tabindex="0"] #navigation-endpoint')?.href;
if (menuUrl && !menuUrl.includes('watch?')) return;
if (menu && !menu.contains(downloadButton)) {
menu.prepend(downloadButton);
progress = document.querySelector("#ytmcustom-download");
}
menu.prepend(downloadButton);
progress = document.querySelector("#ytmcustom-download");
});
const reinit = () => {
@ -46,7 +48,13 @@ global.download = () => {
?.querySelector('ytmusic-menu-navigation-item-renderer.iron-selected[tabindex="0"] #navigation-endpoint')
?.getAttribute("href");
if (videoUrl) {
videoUrl = baseUrl + "/" + videoUrl;
if (videoUrl.startsWith('watch?')) {
videoUrl = baseUrl + "/" + videoUrl;
}
if (videoUrl.includes('?playlist=')) {
ipcRenderer.send('download-playlist-request', videoUrl);
return;
}
metadata = null;
} else {
metadata = global.songInfo;
@ -78,10 +86,13 @@ global.download = () => {
function observeMenu(options) {
pluginOptions = { ...pluginOptions, ...options };
observer.observe(document, {
childList: true,
subtree: true,
});
document.addEventListener('apiLoaded', () => {
observer.observe(document.querySelector('ytmusic-popup-container'), {
childList: true,
subtree: true,
});
}, { once: true, passive: true })
}
module.exports = observeMenu;