From 395e4744056f018bec10f1549217a03d0e732e33 Mon Sep 17 00:00:00 2001 From: Quentin Stoeckel Date: Tue, 25 Oct 2022 15:39:30 +0200 Subject: [PATCH] Implement download action outside of extension context Especially useful in development environment --- src/App.vue | 14 +++++++++----- src/helpers/request.ts | 11 +++++++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/App.vue b/src/App.vue index 46cc3ac..3182089 100644 --- a/src/App.vue +++ b/src/App.vue @@ -276,7 +276,7 @@ import 'vue-virtual-scroller/dist/vue-virtual-scroller.css' import OverflowMenu from './components/OverflowMenu.vue'; import SettingSwitch from './components/SettingSwitch.vue'; import IconsRow from './components/IconsRow.vue'; -import {request} from '@/helpers/request'; +import {download, request} from '@/helpers/request'; import {getScrollbarWidth} from '@/helpers/dom'; import {Icon} from '@/types'; import {Action, Copy} from '@/enums'; @@ -542,10 +542,14 @@ export default defineComponent({ const filename = `${this.activeIcon.name}.${format}`; const browserApi = getBrowserInstance(); - browserApi && browserApi.downloads.download({ - url, - filename, - }); + if (browserApi) { + browserApi && browserApi.downloads.download({ + url, + filename, + }); + } else { + download(url, filename); + } this.toast(`Downloaded ${filename}`); // Close overflow menu once done diff --git a/src/helpers/request.ts b/src/helpers/request.ts index 98f2fbf..bd717d0 100644 --- a/src/helpers/request.ts +++ b/src/helpers/request.ts @@ -6,7 +6,7 @@ /** * Super simple xhr request */ -const request = (url: string, method = 'GET'): Promise => { +export const request = (url: string, method = 'GET'): Promise => { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open(method, url); @@ -27,4 +27,11 @@ const request = (url: string, method = 'GET'): Promise => { }); }; -export { request }; +export const download = (href: string, filename: string): void => { + const a = document.createElement('a') + a.href = href + a.download = filename; + document.body.appendChild(a) + a.click() + document.body.removeChild(a) +}