Update based review comments, and show action only in http(s) pages

This commit is contained in:
Will Bamberg
2016-08-17 15:54:02 -07:00
parent c3eff787af
commit 9e6945676c
2 changed files with 27 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
const CSS = "body { border: 20px solid red; }";
const TITLE_APPLY = "Apply CSS";
const TITLE_REMOVE = "Remove CSS";
const APPLICABLE_PROTOCOLS = ["http:", "https:"];
/*
Toggle CSS: based on the current title, insert or remove the CSS.
@@ -24,27 +25,41 @@ function toggleCSS(tab) {
}
/*
Initialize the page action: set icon and title, then show.
Returns true only if the URL's protocol is in APPLICABLE_PROTOCOLS.
*/
function initializePageAction(tabId) {
chrome.pageAction.setIcon({tabId, path: "icons/off.svg"});
chrome.pageAction.setTitle({tabId, title: TITLE_APPLY});
chrome.pageAction.show(tabId);
function protocolIsApplicable(url) {
var anchor = document.createElement('a');
anchor.href = url;
return APPLICABLE_PROTOCOLS.includes(anchor.protocol);
}
/*
Initialize the page action: set icon and title, then show.
Only operates on tabs whose URL's protocol is applicable.
*/
function initializePageAction(tab) {
if (protocolIsApplicable(tab.url)) {
chrome.pageAction.setIcon({tabId: tab.id, path: "icons/off.svg"});
chrome.pageAction.setTitle({tabId: tab.id, title: TITLE_APPLY});
chrome.pageAction.show(tab.id);
}
}
/*
When first loaded, initialize the page action for all tabs.
*/
chrome.tabs.query({}, (tabs)=> {
chrome.tabs.query({}, (tabs) => {
for (tab of tabs) {
initializePageAction(tab.id);
initializePageAction(tab);
}
});
/*
Each time a tab is updated, reset the page action for that tab.
*/
chrome.tabs.onUpdated.addListener(initializePageAction);
chrome.tabs.onUpdated.addListener((id, changeInfo, tab) => {
initializePageAction(tab);
});
/*
Toggle CSS when the page action is clicked.

View File

@@ -18,11 +18,13 @@
},
"page_action": {
"default_icon": "icons/off.svg"
"default_icon": "icons/off.svg",
"browser_style": true
},
"permissions": [
"activeTab"
"activeTab",
"tabs"
]
}