spotifylookup/bits/frontend.js

35 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-01-04 18:19:03 +00:00
(function() {
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
const button = document.querySelector('#export');
const selectedTracks = [];
checkboxes.forEach((box) => {
const meta = [
box.getAttribute('data-file'), box.getAttribute('data-spotify')
];
function clickBox() {
if (box.checked) {
selectedTracks.push(meta);
} else {
const i = selectedTracks.indexOf(meta);
if (i > -1) {
selectedTracks.splice(i, 1);
}
}
}
box.addEventListener('change', ($ev) => clickBox());
box.parentElement.parentElement.addEventListener('click', () => box.click());
clickBox();
});
button.addEventListener('click', ($ev) => {
$ev.preventDefault();
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(selectedTracks));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "track-selection.json");
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
downloadAnchorNode.remove();
});
})();