From b3aaf8063c2f0a1c378acb8122c44ffe337476a2 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 15 Jan 2018 10:01:25 -0500 Subject: [PATCH] downloader for edge and safari --- src/browser/browserApi.ts | 27 +++++++--------- src/downloader/downloader.js | 17 ---------- src/downloader/downloader.ts | 32 +++++++++++++++++++ src/downloader/index.html | 2 +- src/popup/app/tools/export.component.ts | 3 +- .../app/vault/vaultViewCipherController.js | 16 +--------- webpack.common.js | 6 ++++ 7 files changed, 53 insertions(+), 50 deletions(-) delete mode 100644 src/downloader/downloader.js create mode 100644 src/downloader/downloader.ts 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 @@ - +
Downloading...
diff --git a/src/popup/app/tools/export.component.ts b/src/popup/app/tools/export.component.ts index 601c5bfc57..fbaa767f1f 100644 --- a/src/popup/app/tools/export.component.ts +++ b/src/popup/app/tools/export.component.ts @@ -133,9 +133,8 @@ export class ExportController { } private downloadFile(csv: string): void { - const csvBlob = new Blob([csv], { type: 'text/plain' }); const fileName = this.makeFileName(); - BrowserApi.downloadFile(this.$window, csvBlob, fileName); + BrowserApi.downloadFile(this.$window, csv, { type: 'text/plain' }, fileName); } private makeFileName(): string { diff --git a/src/popup/app/vault/vaultViewCipherController.js b/src/popup/app/vault/vaultViewCipherController.js index 145cff2062..e46d688f1f 100644 --- a/src/popup/app/vault/vaultViewCipherController.js +++ b/src/popup/app/vault/vaultViewCipherController.js @@ -162,21 +162,7 @@ angular return cryptoService.decryptFromBytes(req.response, key); }).then(function (decBuf) { var blob = new Blob([decBuf]); - - if ($window.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/ - $window.navigator.msSaveBlob(csvBlob, attachment.fileName); - } - else { - var a = $window.document.createElement('a'); - a.href = $window.URL.createObjectURL(blob); - a.download = attachment.fileName; - $window.document.body.appendChild(a); - a.click(); - $window.document.body.removeChild(a); - } + BrowserApi.downloadFile($window, decBuf, null, attachment.fileName); $timeout(function () { attachment.downloading = false; diff --git a/webpack.common.js b/webpack.common.js index ed6bc48c39..4841981e6c 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -22,6 +22,7 @@ module.exports = { 'content/autofiller': './src/content/autofiller.js', 'content/notificationBar': './src/content/notificationBar.js', 'notification/bar': './src/notification/bar.js', + 'downloader/downloader': './src/downloader/downloader.ts', }, module: { rules: [ @@ -94,6 +95,11 @@ module.exports = { filename: 'notification/bar.html', chunks: ['notification/bar'] }), + new HtmlWebpackPlugin({ + template: './src/downloader/index.html', + filename: 'downloader/index.html', + chunks: ['downloader/downloader'] + }), new CopyWebpackPlugin([ './src/manifest.json', './src/Info.plist',