Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trashbin): add a working export button #3268

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading