From 1b65a8449004d64e429d9c443cd4fb8f0e68127a Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Thu, 7 Dec 2017 13:13:25 +0100 Subject: [PATCH] Add one-click "Download as Zip" option #50 Re-using the popup for the existing download progress UI. --- src/background.js | 27 ++++++++++++++++++++------- src/bg-contextmenu.js | 4 +--- src/popup.html | 9 ++++++++- src/popup.js | 5 +++++ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/background.js b/src/background.js index 7a8d8b3..dfb4146 100644 --- a/src/background.js +++ b/src/background.js @@ -63,6 +63,7 @@ chrome.storage.sync.get({ chrome.pageAction.onClicked.addListener(function(tab) { if (gActionClickAction === 'popup') return; + if (gActionClickAction === 'download') return; var crx_url = get_crx_url(tab.url); var filename = get_zip_name(crx_url); if (!crx_url) { @@ -77,16 +78,17 @@ chrome.pageAction.onClicked.addListener(function(tab) { }); return; } - if (gActionClickAction === 'download') { - console.error('not implemented yet'); - return; - } console.error('Unexpected gActionClickAction: ' + gActionClickAction); }); function setActionClickAction(actionClickAction) { - if (actionClickAction) { + if (actionClickAction && gActionClickAction !== actionClickAction) { gActionClickAction = actionClickAction; + chrome.tabs.query({ + active: true, + }, function(tabs) { + tabs.forEach(showPageActionIfNeeded); + }); } } @@ -194,10 +196,21 @@ function dwr_onMessage(details) { } //#endif function showPageAction(tabId, url) { - var params = url ? encodeQueryString({crx: url}) : ''; + var popup; + if (gActionClickAction === 'view-source') { + // Let pageAction.onClicked handle this. + popup = ''; + } else if (gActionClickAction === 'popup' || + gActionClickAction === 'download') { + var params = {}; + if (url) params.crx = url; + if (gActionClickAction === 'download') params.doDownload = 1; + popup = 'popup.html?' + encodeQueryString(params); + } + chrome.pageAction.setPopup({ tabId: tabId, - popup: gActionClickAction === 'popup' ? 'popup.html?' + params : '', + popup: popup, }); chrome.pageAction.show(tabId); } diff --git a/src/bg-contextmenu.js b/src/bg-contextmenu.js index ad2430a..7ac47f1 100644 --- a/src/bg-contextmenu.js +++ b/src/bg-contextmenu.js @@ -83,9 +83,7 @@ chrome.contextMenus.create({ id: MENU_ID_ACTION_MENU_DOWNLOAD, parentId: MENU_ID_ACTION_MENU, - // TODO: Support this and enable the option. - title: 'Download as zip (not supported yet)', - enabled: false, + title: 'Download as zip', type: 'radio', contexts: ['page_action'], //#if FIREFOX diff --git a/src/popup.html b/src/popup.html index fdfb0ea..fe7727f 100644 --- a/src/popup.html +++ b/src/popup.html @@ -20,10 +20,17 @@ height: 10px; width: 100%; } + #download.downloading .idle, + #download:not(.downloading) .busy { + display: none; + } - + diff --git a/src/popup.js b/src/popup.js index e6502ce..9f73d93 100644 --- a/src/popup.js +++ b/src/popup.js @@ -38,6 +38,9 @@ function ready() { //#if OPERA document.getElementById('install-as-nex').onclick = doInstall; //#endif + if (getParam('doDownload')) { + doDownload(); + } } var hasDownloadedOnce = false; function doDownload() { @@ -49,10 +52,12 @@ function doDownload() { tryTriggerDownload(blob, filename); }, function(errorMessage) { hasDownloadedOnce = false; + document.getElementById('download').classList.toggle('downloading', hasDownloadedOnce); console.error(errorMessage); alert('Error in CRX Viewer:\n\n' + errorMessage); }, onXHRprogress.bind(null, document.getElementById('download'))); hasDownloadedOnce = true; + document.getElementById('download').classList.toggle('downloading', hasDownloadedOnce); } function doViewSource() { chrome.tabs.create({