From 5d551c5d70f8e6756c08aded44688563eeed4369 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Sat, 13 Jan 2018 23:57:58 -0500 Subject: [PATCH] use downloader page to dl files on safari --- src/browser/browserApi.ts | 34 +++++++++++++++++++++++-- src/downloader/downloader.js | 17 +++++++++++++ src/downloader/index.html | 10 ++++++++ src/popup/app/tools/export.component.ts | 17 +++---------- 4 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 src/downloader/downloader.js create mode 100644 src/downloader/index.html diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 29a7bd2ab7..60afea12f8 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -144,9 +144,10 @@ class BrowserApi { } } - static createNewTab(url: string, extensionPage: boolean = false): void { + static createNewTab(url: string, extensionPage: boolean = false): any { if (BrowserApi.isChromeApi) { chrome.tabs.create({ url: url }); + return null; } else if (BrowserApi.isSafariApi) { if (extensionPage && url.indexOf('/') === 0) { url = BrowserApi.getAssetUrl(url); @@ -155,6 +156,7 @@ class BrowserApi { if (tab) { tab.url = url; } + return tab; } else { return; } @@ -196,7 +198,35 @@ class BrowserApi { } } - private static makeTabObject(tab: any) { + 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) { + const tab = BrowserApi.createNewTab(BrowserApi.getAssetUrl('downloader/index.html')); + const madeTab = BrowserApi.makeTabObject(tab); + setTimeout(() => { + BrowserApi.tabSendMessage(madeTab, { + command: 'downloaderPageData', + data: { + blob: blob, + fileName: fileName, + }, + }); + }, 1000); + } + } + + private static makeTabObject(tab: any): any { if (BrowserApi.isChromeApi) { return tab; } diff --git a/src/downloader/downloader.js b/src/downloader/downloader.js new file mode 100644 index 0000000000..1b4049eca9 --- /dev/null +++ b/src/downloader/downloader.js @@ -0,0 +1,17 @@ +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/index.html b/src/downloader/index.html new file mode 100644 index 0000000000..5ecdc1a8e5 --- /dev/null +++ b/src/downloader/index.html @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/popup/app/tools/export.component.ts b/src/popup/app/tools/export.component.ts index c9608be965..601c5bfc57 100644 --- a/src/popup/app/tools/export.component.ts +++ b/src/popup/app/tools/export.component.ts @@ -2,6 +2,8 @@ import * as angular from 'angular'; import * as papa from 'papaparse'; import * as template from './export.component.html'; +import { BrowserApi } from '../../../browser/browserApi'; + import { CipherType } from 'jslib/enums/cipherType'; import { CipherService } from 'jslib/abstractions/cipher.service'; @@ -133,20 +135,7 @@ export class ExportController { private downloadFile(csv: string): void { const csvBlob = new Blob([csv], { type: 'text/plain' }); const fileName = this.makeFileName(); - - if (this.$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/ - this.$window.navigator.msSaveBlob(csvBlob, fileName); - } else { - const a = this.$window.document.createElement('a'); - a.href = this.$window.URL.createObjectURL(csvBlob); - a.download = fileName; - this.$window.document.body.appendChild(a); - a.click(); - this.$window.document.body.removeChild(a); - } + BrowserApi.downloadFile(this.$window, csvBlob, fileName); } private makeFileName(): string {