From 235ccbc344ebdec066dc07c37bd50f067c132e91 Mon Sep 17 00:00:00 2001 From: wanhose Date: Sun, 27 Jun 2021 13:16:38 +0200 Subject: [PATCH] refactor(scripts): add chrome.runtime.sendMessage shortcut to popup script --- scripts/popup.js | 89 +++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/scripts/popup.js b/scripts/popup.js index 7b76b41..4ae1531 100644 --- a/scripts/popup.js +++ b/scripts/popup.js @@ -7,6 +7,14 @@ const chromeUrl = "https://chrome.google.com/webstore/detail/do-not-consent/djcbfpkdhdkaflcigibkbpboflaplabg"; +/** + * @constant dispatch + * @description Shortcut to send messages to background script + * @type {void} + */ + +const dispatch = chrome.runtime.sendMessage; + /** * @constant firefoxUrl * @description Firefox Add-ons link @@ -25,43 +33,37 @@ const firefoxUrl = const isChromium = chrome.runtime.getURL("").startsWith("chrome-extension://"); /** - * @async * @function handlePowerChange * @description Disables or enables extension on current page */ -const handlePowerChange = async () => { - chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ hostname, id }) => { - chrome.runtime.sendMessage( - { hostname, type: "GET_CACHE" }, - null, - ({ enabled }) => { - const power = document.getElementById("power"); +const handlePowerChange = () => { + dispatch({ type: "GET_TAB" }, null, ({ hostname, id }) => { + dispatch({ hostname, type: "GET_CACHE" }, null, ({ enabled }) => { + const power = document.getElementById("power"); - chrome.runtime.sendMessage({ - hostname, - state: { enabled: !enabled }, - type: "UPDATE_CACHE", - }); - chrome.runtime.sendMessage({ - type: !enabled === true ? "ENABLE_ICON" : "DISABLE_ICON", - }); - if (!enabled === false) power.removeAttribute("checked"); - if (!enabled === true) power.setAttribute("checked", "checked"); - chrome.tabs.reload(id, { bypassCache: true }); - } - ); + dispatch({ + hostname, + state: { enabled: !enabled }, + type: "UPDATE_CACHE", + }); + dispatch({ + type: !enabled === true ? "ENABLE_ICON" : "DISABLE_ICON", + }); + if (!enabled === false) power.removeAttribute("checked"); + if (!enabled === true) power.setAttribute("checked", "checked"); + chrome.tabs.reload(id, { bypassCache: true }); + }); }); }; /** - * @async * @function handleReload * @description Reload current page */ -const handleReload = async () => { - chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ id }) => { +const handleReload = () => { + dispatch({ type: "GET_TAB" }, null, ({ id }) => { chrome.tabs.reload(id, { bypassCache: true }); }); }; @@ -92,33 +94,28 @@ const handleRate = (event) => { }; /** - * @async * @function handleContentLoaded * @description Setup stars handlers and result message links */ -const handleContentLoaded = async () => { - chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ hostname, id }) => { - chrome.runtime.sendMessage( - { hostname, type: "GET_CACHE" }, - null, - ({ enabled }) => { - const host = document.getElementById("host"); - const like = document.getElementById("like"); - const power = document.getElementById("power"); - const reload = document.getElementById("reload"); - const store = document.getElementById("store"); - const unlike = document.getElementById("unlike"); +const handleContentLoaded = () => { + dispatch({ type: "GET_TAB" }, null, ({ hostname, id }) => { + dispatch({ hostname, type: "GET_CACHE" }, null, ({ enabled }) => { + const host = document.getElementById("host"); + const like = document.getElementById("like"); + const power = document.getElementById("power"); + const reload = document.getElementById("reload"); + const store = document.getElementById("store"); + const unlike = document.getElementById("unlike"); - like.addEventListener("click", handleRate); - power.addEventListener("change", handlePowerChange); - reload.addEventListener("click", handleReload); - store.setAttribute("href", isChromium ? chromeUrl : firefoxUrl); - unlike.addEventListener("click", handleRate); - if (location) host.innerText = hostname.replace("www.", ""); - if (!enabled) power.removeAttribute("checked"); - } - ); + like.addEventListener("click", handleRate); + power.addEventListener("change", handlePowerChange); + reload.addEventListener("click", handleReload); + store.setAttribute("href", isChromium ? chromeUrl : firefoxUrl); + unlike.addEventListener("click", handleRate); + if (location) host.innerText = hostname.replace("www.", ""); + if (!enabled) power.removeAttribute("checked"); + }); }); };