Skip to content

Commit

Permalink
Implement download action outside of extension context
Browse files Browse the repository at this point in the history
Especially useful in development environment
  • Loading branch information
quentin-st committed Oct 25, 2022
1 parent ea0ca88 commit 395e474
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/helpers/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Super simple xhr request
*/
const request = (url: string, method = 'GET'): Promise<string> => {
export const request = (url: string, method = 'GET'): Promise<string> => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
Expand All @@ -27,4 +27,11 @@ const request = (url: string, method = 'GET'): Promise<string> => {
});
};

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)
}

0 comments on commit 395e474

Please sign in to comment.