From 9e6945676c8c117615799b7b9dd199fd5a8c0db1 Mon Sep 17 00:00:00 2001 From: Will Bamberg Date: Wed, 17 Aug 2016 15:54:02 -0700 Subject: [PATCH] Update based review comments, and show action only in http(s) pages --- apply-css/background.js | 31 +++++++++++++++++++++++-------- apply-css/manifest.json | 6 ++++-- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/apply-css/background.js b/apply-css/background.js index 6528f4e..8c76e1d 100644 --- a/apply-css/background.js +++ b/apply-css/background.js @@ -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. diff --git a/apply-css/manifest.json b/apply-css/manifest.json index 9030f95..522016b 100644 --- a/apply-css/manifest.json +++ b/apply-css/manifest.json @@ -18,11 +18,13 @@ }, "page_action": { - "default_icon": "icons/off.svg" + "default_icon": "icons/off.svg", + "browser_style": true }, "permissions": [ - "activeTab" + "activeTab", + "tabs" ] }