Skip to content

Commit

Permalink
feat: add a working export btn to trashbin.js
Browse files Browse the repository at this point in the history
  • Loading branch information
eye-wave committed Jan 13, 2025
1 parent 01f5cc5 commit 774c947
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions Extensions/trashbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
header.innerText = "Local Storage";
content.appendChild(header);

content.appendChild(createButton("Export", "Copy all items in trashbin to clipboard, manually save to a .json file.", exportItems));
content.appendChild(createButton("Copy", "Copy all items in trashbin to clipboard.", copyItems));
content.appendChild(createButton("Export", "Save all items in trashbin to a .json file.", exportItems));
content.appendChild(createButton("Import", "Overwrite all items in trashbin via .json file.", importItems));
content.appendChild(
createButton("Clear ", "Clear all items from trashbin (cannot be reverted).", () => {
Expand Down Expand Up @@ -361,7 +362,7 @@
Spicetify.LocalStorage.set("TrashArtistList", JSON.stringify(trashArtistList));
}

function exportItems() {
function copyItems() {
const data = {
songs: trashSongList,
artists: trashArtistList,
Expand All @@ -370,6 +371,35 @@
Spicetify.showNotification("Copied to clipboard");
}

async function exportItems() {
const data = {
songs: trashSongList,
artists: trashArtistList,
};

try {
const handle = await window.showSaveFilePicker({
suggestedName: "spicetify-trashbin.json",
types: [
{
description: "Spicetify trashbin backup",
accept: {
"application/json": [".json"],
},
},
],
});

const writable = await handle.createWritable();
await writable.write(JSON.stringify(data));
await writable.close();

Spicetify.showNotification("Backup saved succesfully.");
} catch {
Spicetify.showNotification("Failed to save, try copying trashbin contents to clipboard and creating a backup manually.");
}
}

function importItems() {
const input = document.createElement("input");
input.type = "file";
Expand Down

0 comments on commit 774c947

Please sign in to comment.