diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 60afea12f8..fbba159057 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -198,31 +198,28 @@ class BrowserApi { } } - static downloadFile(win: Window, blob: Blob, fileName: string) { - if (win.navigator.msSaveOrOpenBlob) { - // Currently bugged in Edge. See - // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8178877/ - // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8477778/ - win.navigator.msSaveBlob(blob, fileName); - } else if (BrowserApi.isChromeApi) { - const a = win.document.createElement('a'); - a.href = win.URL.createObjectURL(blob); - a.download = fileName; - win.document.body.appendChild(a); - a.click(); - win.document.body.removeChild(a); - } else if (BrowserApi.isSafariApi) { + static downloadFile(win: Window, blobData: any, blobOptions: any, fileName: string) { + if (win.navigator.msSaveOrOpenBlob || BrowserApi.isSafariApi) { const tab = BrowserApi.createNewTab(BrowserApi.getAssetUrl('downloader/index.html')); const madeTab = BrowserApi.makeTabObject(tab); setTimeout(() => { BrowserApi.tabSendMessage(madeTab, { command: 'downloaderPageData', data: { - blob: blob, + blobData: blobData, + blobOptions: blobOptions, fileName: fileName, }, }); }, 1000); + } else { + const blob = new Blob([blobData], blobOptions); + const a = win.document.createElement('a'); + a.href = win.URL.createObjectURL(blob); + a.download = fileName; + win.document.body.appendChild(a); + a.click(); + win.document.body.removeChild(a); } } diff --git a/src/downloader/downloader.js b/src/downloader/downloader.js deleted file mode 100644 index 1b4049eca9..0000000000 --- a/src/downloader/downloader.js +++ /dev/null @@ -1,17 +0,0 @@ -document.addEventListener('DOMContentLoaded', function () { - if (typeof safari === 'undefined') { - return; - } - - safari.self.addEventListener('message', function (msgEvent) { - const msg = msgEvent.message; - if (msg.command === 'downloaderPageData' && msg.data) { - const a = document.createElement('a'); - a.href = URL.createObjectURL(msg.data.blob); - a.download = msg.data.fileName; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); - } - }, false); -}); diff --git a/src/downloader/downloader.ts b/src/downloader/downloader.ts new file mode 100644 index 0000000000..eb57e41c1e --- /dev/null +++ b/src/downloader/downloader.ts @@ -0,0 +1,32 @@ +document.addEventListener('DOMContentLoaded', () => { + const isSafari = (typeof safari !== 'undefined') && navigator.userAgent.indexOf('Safari') !== -1 && + navigator.userAgent.indexOf('Chrome') === -1; + + if (isSafari) { + safari.self.addEventListener('message', (msgEvent: any) => { + doDownload(msgEvent.message); + }, false); + } else if (navigator.userAgent.indexOf(' Edge/') !== -1) { + chrome.runtime.onMessage.addListener((msg: any, sender: any, sendResponse: any) => { + doDownload(msg); + }); + } + + function doDownload(msg: any) { + if (msg.command === 'downloaderPageData' && msg.data) { + const blob = new Blob([msg.data.blobData], msg.data.blobOptions); + if (navigator.msSaveOrOpenBlob) { + navigator.msSaveBlob(blob, msg.data.fileName); + } else { + const a = document.createElement('a'); + a.href = URL.createObjectURL(msg.data.blob); + a.download = msg.data.fileName; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + } + } + + document.querySelector('#dl-message').remove(); + } +}); diff --git a/src/downloader/index.html b/src/downloader/index.html index 5ecdc1a8e5..1c53c9570c 100644 --- a/src/downloader/index.html +++ b/src/downloader/index.html @@ -3,8 +3,8 @@