From a962c4ac16fb1e8b40ee7d2fb04253457cada046 Mon Sep 17 00:00:00 2001 From: wanhose Date: Thu, 8 Apr 2021 23:49:03 +0200 Subject: [PATCH] feat(scripts): create background script --- scripts/background.js | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 scripts/background.js diff --git a/scripts/background.js b/scripts/background.js new file mode 100644 index 0000000..6599c25 --- /dev/null +++ b/scripts/background.js @@ -0,0 +1,80 @@ +/** + * @function disableIcon + * @description Disables icon + * + * @param {string} [tabId] + */ + +const disableIcon = (tabId) => { + chrome.browserAction.setIcon({ + path: "assets/icon-disabled.png", + tabId: tabId, + }); +}; + +/** + * @function disablePopup + * @description Disables popup + * + * @param {string} [tabId] + */ + +const disablePopup = (tabId) => { + chrome.browserAction.setPopup({ + popup: "", + tabId: tabId, + }); +}; + +/** + * @function enableIcon + * @description Enables icon + * + * @param {string} [tabId] + */ + +const enableIcon = (tabId) => { + chrome.browserAction.setIcon({ + path: "assets/icon-enabled.png", + tabId: tabId, + }); +}; + +/** + * @function enablePopup + * @description Enables popup + * + * @param {string} [tabId] + */ + +const enablePopup = (tabId) => { + chrome.browserAction.setPopup({ + popup: "popup.html", + tabId: tabId, + }); +}; + +/** + * @description Listens to content messages + */ + +chrome.runtime.onMessage.addListener((request, sender) => { + sender.tab = { id: undefined }; + + switch (request.type) { + case "DISABLE_ICON": + disableIcon(sender.tab.id); + break; + case "DISABLE_POPUP": + disablePopup(sender.tab.id); + break; + case "ENABLE_ICON": + enableIcon(sender.tab.id); + break; + case "ENABLE_POPUP": + enablePopup(sender.tab.id); + break; + default: + break; + } +});