From 774c947090169cfb166aeeb5291d16f03a10ec17 Mon Sep 17 00:00:00 2001 From: _eyewave <89079979+eye-wave@users.noreply.github.com> Date: Mon, 13 Jan 2025 13:24:42 +0100 Subject: [PATCH] feat: add a working export btn to trashbin.js --- Extensions/trashbin.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/Extensions/trashbin.js b/Extensions/trashbin.js index a624bdcd10..68d0ab34fa 100644 --- a/Extensions/trashbin.js +++ b/Extensions/trashbin.js @@ -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).", () => { @@ -361,7 +362,7 @@ Spicetify.LocalStorage.set("TrashArtistList", JSON.stringify(trashArtistList)); } - function exportItems() { + function copyItems() { const data = { songs: trashSongList, artists: trashArtistList, @@ -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";