Skip to content

Commit

Permalink
Add one-click View source option #50
Browse files Browse the repository at this point in the history
Also added a disabled UI for download, because downloading is a bit
trickier to implement.
  • Loading branch information
Rob--W committed Dec 7, 2017
1 parent 2be4f03 commit 9ca34f8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 7 deletions.
60 changes: 54 additions & 6 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
/* globals chrome, cws_match_pattern, ows_match_pattern, amo_match_patterns,
amo_file_version_match_pattern,
cws_pattern, ows_pattern, amo_pattern, amo_file_version_pattern,
get_crx_url, get_zip_name, console,
URL, document, alert, localStorage */
/* globals encodeQueryString */
/* exported tryTriggerDownload */

'use strict';

// See bg-contextmenu for potential values, at MENU_ID_ACTION_MENU.
var gActionClickAction = 'popup';

//#if FIREFOX
/* globals browser */
//// Note: This feature may be unnecessary once bugzil.la/1395387 lands.
function tabsOnUpdatedCheckPageAction(tabId, changeInfo, tab) {
showPageActionIfNeeded(tab);
}
Expand All @@ -32,17 +37,60 @@ function togglePageAction(isEnabled) {
}
});
}
//#endif

//// Note: This feature may be unnecessary once bugzil.la/1395387 lands.
chrome.storage.onChanged.addListener(function(changes) {
if (!changes.showPageAction) return;
togglePageAction(changes.showPageAction.newValue);
function callOnChange(key, callback) {
var valueInfo = key in changes && changes[key];
if (valueInfo) callback(valueInfo.newValue);
}
//#if FIREFOX
callOnChange('showPageAction', togglePageAction);
//#endif
callOnChange('actionClickAction', setActionClickAction);
});
chrome.storage.sync.get({showPageAction: true}, function(items) {
chrome.storage.sync.get({
//#if FIREFOX
showPageAction: true,
//#endif
actionClickAction: gActionClickAction,
}, function(items) {
//#if FIREFOX
togglePageAction(items.showPageAction);
//#endif
setActionClickAction(items.actionClickAction);
});

//#else
chrome.pageAction.onClicked.addListener(function(tab) {
if (gActionClickAction === 'popup') return;
var crx_url = get_crx_url(tab.url);
var filename = get_zip_name(crx_url);
if (!crx_url) {
console.warn('Cannot find extension URL');
return;
}
if (gActionClickAction === 'view-source') {
chrome.tabs.create({
url: chrome.extension.getURL('crxviewer.html') +
'?' + encodeQueryString({crx: crx_url, zipname: filename}),
active: true
});
return;
}
if (gActionClickAction === 'download') {
console.error('not implemented yet');
return;
}
console.error('Unexpected gActionClickAction: ' + gActionClickAction);
});

function setActionClickAction(actionClickAction) {
if (actionClickAction) {
gActionClickAction = actionClickAction;
}
}

//#if !FIREFOX
if (chrome.declarativeWebRequest) {
chrome.runtime.onInstalled.addListener(setupDeclarativeWebRequest);
chrome.declarativeWebRequest.onMessage.addListener(dwr_onMessage);
Expand Down Expand Up @@ -149,7 +197,7 @@ function showPageAction(tabId, url) {
var params = url ? encodeQueryString({crx: url}) : '';
chrome.pageAction.setPopup({
tabId: tabId,
popup: 'popup.html?' + params
popup: gActionClickAction === 'popup' ? 'popup.html?' + params : '',
});
chrome.pageAction.show(tabId);
}
Expand Down
66 changes: 65 additions & 1 deletion src/bg-contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@
//#if FIREFOX
var MENU_ID_PAGE_ACTION = 'nl.robwu.contextmenu.pageaction';
//#endif
var MENU_ID_ACTION_MENU = 'nl.robwu.contextmenu.actionmenu.';
var MENU_ID_ACTION_MENU_POPUP = MENU_ID_ACTION_MENU + 'popup';
var MENU_ID_ACTION_MENU_VIEW_SOURCE = MENU_ID_ACTION_MENU + 'view-source';
var MENU_ID_ACTION_MENU_DOWNLOAD = MENU_ID_ACTION_MENU + 'download';

chrome.storage.onChanged.addListener(function(changes) {
if (changes.actionClickAction) {
setActionClickAction(changes.actionClickAction.newValue);
return;
}
if (!changes.showContextMenu) return;
if (changes.showContextMenu.newValue) show();
else hide();
Expand All @@ -35,8 +44,12 @@
//#endif
function checkContextMenuPref() {
var storageArea = chrome.storage.sync;
storageArea.get({showContextMenu:true}, function(items) {
storageArea.get({
showContextMenu: true,
actionClickAction: 'popup',
}, function(items) {
if (items.showContextMenu) show();
setActionClickAction(items.actionClickAction);
});
//#if FIREFOX
chrome.contextMenus.create({
Expand All @@ -50,9 +63,60 @@
},
});
//#endif
chrome.contextMenus.create({
id: MENU_ID_ACTION_MENU,
title: 'Primary action on click',
contexts: ['page_action'],
});
chrome.contextMenus.create({
id: MENU_ID_ACTION_MENU_POPUP,
parentId: MENU_ID_ACTION_MENU,
title: 'Show popup (default)',
type: 'radio',
checked: true,
contexts: ['page_action'],
//#if FIREFOX
onclick: contextMenusOnClicked,
//#endif
});
// Note: Keep the same order as in popup.html for consistency.
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,
type: 'radio',
contexts: ['page_action'],
//#if FIREFOX
onclick: contextMenusOnClicked,
//#endif
});
chrome.contextMenus.create({
id: MENU_ID_ACTION_MENU_VIEW_SOURCE,
parentId: MENU_ID_ACTION_MENU,
title: 'View source',
type: 'radio',
contexts: ['page_action'],
//#if FIREFOX
onclick: contextMenusOnClicked,
//#endif
});
}

function setActionClickAction(actionClickAction) {
if (!actionClickAction) return;
chrome.contextMenus.update(MENU_ID_ACTION_MENU + actionClickAction, {
checked: true,
});
}

function contextMenusOnClicked(info, tab) {
if (info.menuItemId.startsWith(MENU_ID_ACTION_MENU)) {
var choice = info.menuItemId.slice(MENU_ID_ACTION_MENU.length);
chrome.storage.sync.set({actionClickAction: choice});
return;
}
var url = info.menuItemId == MENU_ID_PAGE ||
info.menuItemId == MENU_ID_AMO_APPROVED_PAGE ? info.pageUrl : info.linkUrl;
url = get_crx_url(url);
Expand Down

0 comments on commit 9ca34f8

Please sign in to comment.