From 95fa3c3afa11ccaa36e0c3caae5df14ba88bb7d1 Mon Sep 17 00:00:00 2001 From: Kushagra Gour Date: Sat, 13 Jan 2018 10:00:24 +0530 Subject: [PATCH] refactor download file logic and move to utils --- src/script.js | 35 ++++++++--------------------------- src/utils.js | 44 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/script.js b/src/script.js index fff3cbc..27d7421 100644 --- a/src/script.js +++ b/src/script.js @@ -427,7 +427,7 @@ loginModal // Do not presist that on remote. if (key === 'code') { // No deferred required here as this gets called on unloadbefore - return; + return false; } return itemService.setItem(key || currentItem.id, currentItem).then(() => { alertsService.add('Item saved.'); @@ -1210,14 +1210,9 @@ loginModal fileName = currentItem.title; } - var a = document.createElement('a'); var blob = new Blob([fileContent], { type: 'text/html;charset=UTF-8' }); - a.href = window.URL.createObjectURL(blob); - a.download = fileName; - a.style.display = 'none'; - document.body.appendChild(a); - a.click(); - a.remove(); + utils.downloadFile(fileName, blob); + trackEvent('fn', 'saveFileComplete'); }); } @@ -1414,25 +1409,7 @@ loginModal type: 'application/json;charset=UTF-8' }); - chrome.downloads.download( - { - 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(); - } - } - ); + utils.downloadFile(fileName, blob); trackEvent('ui', 'exportBtnClicked'); }); @@ -1596,6 +1573,10 @@ loginModal function handleDownloadsPermission() { var d = deferred(); + if (!window.IS_EXTENSION) { + d.resolve(); + return d.promise; + } chrome.permissions.contains( { permissions: ['downloads'] diff --git a/src/utils.js b/src/utils.js index 80db01f..43c1042 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 = { - semverCompare: semverCompare, - generateRandomId: generateRandomId, - onButtonClick: onButtonClick, - addInfiniteLoopProtection: addInfiniteLoopProtection, - getHumanDate: getHumanDate, - log: log, - once: once + semverCompare, + generateRandomId, + onButtonClick, + addInfiniteLoopProtection, + getHumanDate, + log, + once, + downloadFile }; window.chrome = window.chrome || {};