use downloader page to dl files on safari

This commit is contained in:
Kyle Spearrin 2018-01-13 23:57:58 -05:00
parent 00b2c47aae
commit 5d551c5d70
4 changed files with 62 additions and 16 deletions

View File

@ -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) { if (BrowserApi.isChromeApi) {
chrome.tabs.create({ url: url }); chrome.tabs.create({ url: url });
return null;
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
if (extensionPage && url.indexOf('/') === 0) { if (extensionPage && url.indexOf('/') === 0) {
url = BrowserApi.getAssetUrl(url); url = BrowserApi.getAssetUrl(url);
@ -155,6 +156,7 @@ class BrowserApi {
if (tab) { if (tab) {
tab.url = url; tab.url = url;
} }
return tab;
} else { } else {
return; 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) { if (BrowserApi.isChromeApi) {
return tab; return tab;
} }

View File

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

10
src/downloader/index.html Normal file
View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="downloader.js"></script>
</head>
<body>
</body>
</html>

View File

@ -2,6 +2,8 @@ import * as angular from 'angular';
import * as papa from 'papaparse'; import * as papa from 'papaparse';
import * as template from './export.component.html'; import * as template from './export.component.html';
import { BrowserApi } from '../../../browser/browserApi';
import { CipherType } from 'jslib/enums/cipherType'; import { CipherType } from 'jslib/enums/cipherType';
import { CipherService } from 'jslib/abstractions/cipher.service'; import { CipherService } from 'jslib/abstractions/cipher.service';
@ -133,20 +135,7 @@ export class ExportController {
private downloadFile(csv: string): void { private downloadFile(csv: string): void {
const csvBlob = new Blob([csv], { type: 'text/plain' }); const csvBlob = new Blob([csv], { type: 'text/plain' });
const fileName = this.makeFileName(); const fileName = this.makeFileName();
BrowserApi.downloadFile(this.$window, csvBlob, fileName);
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);
}
} }
private makeFileName(): string { private makeFileName(): string {