mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
Update based review comments, and show action only in http(s) pages
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -18,11 +18,13 @@
|
||||
},
|
||||
|
||||
"page_action": {
|
||||
"default_icon": "icons/off.svg"
|
||||
"default_icon": "icons/off.svg",
|
||||
"browser_style": true
|
||||
},
|
||||
|
||||
"permissions": [
|
||||
"activeTab"
|
||||
"activeTab",
|
||||
"tabs"
|
||||
]
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user