1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-18 04:21:12 +02:00

refactor download file logic and move to utils

This commit is contained in:
Kushagra Gour
2018-01-13 10:00:24 +05:30
parent 9f2ead1f69
commit 95fa3c3afa
2 changed files with 45 additions and 34 deletions

View File

@@ -427,7 +427,7 @@ loginModal
// Do not presist that on remote. // Do not presist that on remote.
if (key === 'code') { if (key === 'code') {
// No deferred required here as this gets called on unloadbefore // No deferred required here as this gets called on unloadbefore
return; return false;
} }
return itemService.setItem(key || currentItem.id, currentItem).then(() => { return itemService.setItem(key || currentItem.id, currentItem).then(() => {
alertsService.add('Item saved.'); alertsService.add('Item saved.');
@@ -1210,14 +1210,9 @@ loginModal
fileName = currentItem.title; fileName = currentItem.title;
} }
var a = document.createElement('a');
var blob = new Blob([fileContent], { type: 'text/html;charset=UTF-8' }); var blob = new Blob([fileContent], { type: 'text/html;charset=UTF-8' });
a.href = window.URL.createObjectURL(blob); utils.downloadFile(fileName, blob);
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
trackEvent('fn', 'saveFileComplete'); trackEvent('fn', 'saveFileComplete');
}); });
} }
@@ -1414,25 +1409,7 @@ loginModal
type: 'application/json;charset=UTF-8' type: 'application/json;charset=UTF-8'
}); });
chrome.downloads.download( utils.downloadFile(fileName, blob);
{
url: window.URL.createObjectURL(blob),
filename: fileName,
saveAs: true
},
function() {
// If there was an error, just download the file using ANCHOR method.
if (chrome.runtime.lastError) {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
}
}
);
trackEvent('ui', 'exportBtnClicked'); trackEvent('ui', 'exportBtnClicked');
}); });
@@ -1596,6 +1573,10 @@ loginModal
function handleDownloadsPermission() { function handleDownloadsPermission() {
var d = deferred(); var d = deferred();
if (!window.IS_EXTENSION) {
d.resolve();
return d.promise;
}
chrome.permissions.contains( chrome.permissions.contains(
{ {
permissions: ['downloads'] permissions: ['downloads']

View File

@@ -171,14 +171,44 @@
}); });
} }
function downloadFile(fileName, blob) {
function downloadWithAnchor() {
var a = document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
}
if (window.IS_EXTENSION) {
chrome.downloads.download(
{
url: window.URL.createObjectURL(blob),
filename: fileName,
saveAs: true
},
() => {
// If there was an error, just download the file using ANCHOR method.
if (chrome.runtime.lastError) {
downloadWithAnchor();
}
}
);
} else {
downloadWithAnchor();
}
}
window.utils = { window.utils = {
semverCompare: semverCompare, semverCompare,
generateRandomId: generateRandomId, generateRandomId,
onButtonClick: onButtonClick, onButtonClick,
addInfiniteLoopProtection: addInfiniteLoopProtection, addInfiniteLoopProtection,
getHumanDate: getHumanDate, getHumanDate,
log: log, log,
once: once once,
downloadFile
}; };
window.chrome = window.chrome || {}; window.chrome = window.chrome || {};