From 5b7f99c9e0eac3fd54fa4d47105b837284464abe Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 2 Aug 2022 10:14:03 +0200 Subject: [PATCH] cleanup --- src/components/PlaylistPage.vue | 9 --------- src/components/SubscriptionsPage.vue | 9 +-------- src/main.js | 18 ++++++++++-------- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/components/PlaylistPage.vue b/src/components/PlaylistPage.vue index 550f29b6..7c320ffe 100644 --- a/src/components/PlaylistPage.vue +++ b/src/components/PlaylistPage.vue @@ -143,15 +143,6 @@ export default { }); this.download(data, this.playlist.name + ".txt"); }, - download(text, filename) { - var element = document.createElement("a"); - element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(text)); - element.setAttribute("download", filename); - element.style.display = "none"; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element); - }, }, }; diff --git a/src/components/SubscriptionsPage.vue b/src/components/SubscriptionsPage.vue index 50c452a7..592efdf2 100644 --- a/src/components/SubscriptionsPage.vue +++ b/src/components/SubscriptionsPage.vue @@ -97,14 +97,7 @@ export default { subscriptions: subscriptions, }); - var file = new Blob([json], { type: "application/json" }); - - const elem = document.createElement("a"); - - elem.href = URL.createObjectURL(file); - elem.download = "subscriptions.json"; - elem.click(); - elem.remove(); + this.download(json, "subscriptions.json", "application/json"); }, }, }; diff --git a/src/main.js b/src/main.js index d24b03b3..788a96f7 100644 --- a/src/main.js +++ b/src/main.js @@ -220,14 +220,16 @@ const mixin = { const localSubscriptions = this.getLocalSubscriptions(); return localSubscriptions != null ? localSubscriptions.join(",") : ""; }, - download(text, filename) { - var element = document.createElement("a"); - element.setAttribute("href", "data:text/plain;charset=utf-8," + encodeURIComponent(text)); - element.setAttribute("download", filename); - element.style.display = "none"; - document.body.appendChild(element); - element.click(); - document.body.removeChild(element); + /* generate a temporary file and ask the user to download it */ + download(text, filename, type) { + var file = new Blob([text], { type: type }); + + const elem = document.createElement("a"); + + elem.href = URL.createObjectURL(file); + elem.download = filename; + elem.click(); + elem.remove(); }, }, computed: {