Skip to content

Commit

Permalink
move download button
Browse files Browse the repository at this point in the history
  • Loading branch information
duzda committed Aug 31, 2022
1 parent f2fdb58 commit 89e1600
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 67 deletions.
88 changes: 24 additions & 64 deletions src/injections/downloads/add_download_injection.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/* global ipcRenderer, Bridge, path */

// Lock to prevent spawning multiple polling threads
let polling = false;
// To prevent adding the button to the old page that is just getting removed
let oldWrapper = null;

let pageAlerts;

// error is type of string with values SUCCESS, WARNING, ERROR
Expand Down Expand Up @@ -46,61 +41,8 @@ ipcRenderer.on('downloadFinished', (event, error, name) => {
});

function onUrlChange(url) {
setTimeout(() => {
if (url.includes('profile') || polling) {
return;
}
if (url.includes('album') ||
url.includes('playlist')) {
pollButtonWrapper();
} else if (url.includes('artist')) {
pollUnorderedList();
}
}, 100);
}

function pollButtonWrapper() {
poll('_2tIG4', addDownloadCommon);
}

function pollUnorderedList() {
poll('_1k3N9', addDownloadArtist);
}

function poll(className, executeFunction) {
polling = true;
let buttonwrapper = document.getElementsByClassName(className);
if (buttonwrapper.length == 0 || buttonwrapper[0] == oldWrapper) {
setTimeout(poll, 100, className, executeFunction);
return;
}

polling = false;

buttonwrapper = buttonwrapper[0];
oldWrapper = buttonwrapper;

executeFunction(buttonwrapper);
}

function addDownloadCommon(wrapper) {
addDownloadButton(wrapper, 'div', ['aLCv2']);
}

function addDownloadArtist(wrapper) {
addDownloadButton(wrapper.children[0], 'li', ['list-actions-item']);
}

function addDownloadButton(wrapper, element, ...classes) {
Bridge.LazyReader.get(path.join('injections', 'downloads', 'download_button.html'), (data) => {
let buttonListItem = document.createElement(element);
classes.forEach(c => {
buttonListItem.classList.add(c);
});
buttonListItem.innerHTML = data;
// Append download button right after play button
wrapper.insertBefore(buttonListItem, wrapper.children[1]);
});
document.getElementById('download-icon').classList.remove('color-primary');
document.getElementById('download-button-wrapper').hidden = !(url.includes('album') || url.includes('playlist') || url.includes('artist'));
}

// This is called via html
Expand All @@ -126,10 +68,6 @@ function download() {
} else {
ipcRenderer.send('download', location.href, name, type);
}
toggleButton();
}

function toggleButton() {
document.getElementById('download-icon').classList.add('color-primary');
}

Expand All @@ -144,5 +82,27 @@ function getPageAlerts() {
}
}

function injectDownload() {
let topbar = document.getElementById('page_topbar');
if (topbar != null) {
let poppers = topbar.getElementsByClassName('popper-wrapper');
if (topbar != null && poppers != null && poppers.length > 1 && poppers[1] != null) {
Bridge.LazyReader.getOnce(path.join('injections', 'downloads', 'download-button.html'), (data) => {
let div = document.createElement('div');
div.className = 'popper-wrapper topbar-action';
div.id = 'download-button-wrapper';
div.hidden = true;
div.innerHTML = data;
topbar.insertBefore(div, poppers[1]);
});
} else {
console.warn('There\'s nowhere to put downloads button');
}
} else {
setTimeout(injectDownload, 100);
}
}

getPageAlerts();
injectDownload();
Bridge.bindHistoryCallback(onUrlChange);
6 changes: 3 additions & 3 deletions src/injections/downloads/download-button.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<button type="button" aria-label="Download" onclick="download()" class="chakra-button css-uvjckh">
<button type="button" aria-label="Download" onclick="download()" class="topbar-notification">
<!-- Icon credits: https://www.svgrepo.com/svg/27716/download -->
<svg id="download-icon" class="chakra-icon css-1yk3h4a" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="24px" height="18px" viewBox="0 0 485 485" style="enable-background:new 0 0 485 485;" xml:space="preserve">
<svg id="download-icon" class="svg-icon" version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
width="18" height="18" viewBox="0 0 485 485" style="enable-background:new 0 0 485 485;" xml:space="preserve">
<g> <g> <path d="M426.5,458h-368C51,458,45,464,45,471.5S51,485,58.5,485h368c7.5,0,13.5-6,13.5-13.5S434,458,426.5,458z"/>
<path d="M233,378.7c2.5,2.5,6,4,9.5,4s7-1.4,9.5-4l107.5-107.5c5.3-5.3,5.3-13.8,0-19.1c-5.3-5.3-13.8-5.3-19.1,0L256,336.5v-323
C256,6,250,0,242.5,0S229,6,229,13.5v323l-84.4-84.4c-5.3-5.3-13.8-5.3-19.1,0s-5.3,13.8,0,19.1L233,378.7z"/>
Expand Down

0 comments on commit 89e1600

Please sign in to comment.