Skip to content

Commit

Permalink
Add one-click "Download as Zip" option #50
Browse files Browse the repository at this point in the history
Re-using the popup for the existing download progress UI.
  • Loading branch information
Rob--W committed Dec 7, 2017
1 parent 9ca34f8 commit 1b65a84
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions src/bg-contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
height: 10px;
width: 100%;
}
#download.downloading .idle,
#download:not(.downloading) .busy {
display: none;
}
</style>
</head>
<body>
<button id="download" title="Download extension in zip format">Download as zip</button>
<button id="download" title="Download extension in zip format">
<span class="idle">Download as zip</span>
<span class="busy">Downloading...</span>
</button>
<button id="view-source" title="View source of extension in a new tab">View source</button>
<!--#if OPERA-->
<button id="install-as-nex" title="install the extension">Install</button>
Expand Down
5 changes: 5 additions & 0 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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({
Expand Down

0 comments on commit 1b65a84

Please sign in to comment.