1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-15 20:11:30 +01:00

downloader for edge and safari

This commit is contained in:
Kyle Spearrin 2018-01-15 10:01:25 -05:00
parent 3cee5970bf
commit b3aaf8063c
7 changed files with 53 additions and 50 deletions

View File

@ -198,31 +198,28 @@ class BrowserApi {
} }
} }
static downloadFile(win: Window, blob: Blob, fileName: string) { static downloadFile(win: Window, blobData: any, blobOptions: any, fileName: string) {
if (win.navigator.msSaveOrOpenBlob) { if (win.navigator.msSaveOrOpenBlob || BrowserApi.isSafariApi) {
// 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 tab = BrowserApi.createNewTab(BrowserApi.getAssetUrl('downloader/index.html'));
const madeTab = BrowserApi.makeTabObject(tab); const madeTab = BrowserApi.makeTabObject(tab);
setTimeout(() => { setTimeout(() => {
BrowserApi.tabSendMessage(madeTab, { BrowserApi.tabSendMessage(madeTab, {
command: 'downloaderPageData', command: 'downloaderPageData',
data: { data: {
blob: blob, blobData: blobData,
blobOptions: blobOptions,
fileName: fileName, fileName: fileName,
}, },
}); });
}, 1000); }, 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);
} }
} }

View File

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

View File

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

View File

@ -3,8 +3,8 @@
<head> <head>
<title></title> <title></title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<script src="downloader.js"></script>
</head> </head>
<body> <body>
<div id="dl-message">Downloading...</div>
</body> </body>
</html> </html>

View File

@ -133,9 +133,8 @@ export class ExportController {
} }
private downloadFile(csv: string): void { private downloadFile(csv: string): void {
const csvBlob = new Blob([csv], { type: 'text/plain' });
const fileName = this.makeFileName(); const fileName = this.makeFileName();
BrowserApi.downloadFile(this.$window, csvBlob, fileName); BrowserApi.downloadFile(this.$window, csv, { type: 'text/plain' }, fileName);
} }
private makeFileName(): string { private makeFileName(): string {

View File

@ -162,21 +162,7 @@ angular
return cryptoService.decryptFromBytes(req.response, key); return cryptoService.decryptFromBytes(req.response, key);
}).then(function (decBuf) { }).then(function (decBuf) {
var blob = new Blob([decBuf]); var blob = new Blob([decBuf]);
BrowserApi.downloadFile($window, decBuf, null, attachment.fileName);
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);
}
$timeout(function () { $timeout(function () {
attachment.downloading = false; attachment.downloading = false;

View File

@ -22,6 +22,7 @@ module.exports = {
'content/autofiller': './src/content/autofiller.js', 'content/autofiller': './src/content/autofiller.js',
'content/notificationBar': './src/content/notificationBar.js', 'content/notificationBar': './src/content/notificationBar.js',
'notification/bar': './src/notification/bar.js', 'notification/bar': './src/notification/bar.js',
'downloader/downloader': './src/downloader/downloader.ts',
}, },
module: { module: {
rules: [ rules: [
@ -94,6 +95,11 @@ module.exports = {
filename: 'notification/bar.html', filename: 'notification/bar.html',
chunks: ['notification/bar'] chunks: ['notification/bar']
}), }),
new HtmlWebpackPlugin({
template: './src/downloader/index.html',
filename: 'downloader/index.html',
chunks: ['downloader/downloader']
}),
new CopyWebpackPlugin([ new CopyWebpackPlugin([
'./src/manifest.json', './src/manifest.json',
'./src/Info.plist', './src/Info.plist',