refactor(scripts): add chrome.runtime.sendMessage shortcut to popup script

This commit is contained in:
wanhose 2021-06-27 13:16:38 +02:00
parent 835233570b
commit 235ccbc344

View File

@ -7,6 +7,14 @@
const chromeUrl = const chromeUrl =
"https://chrome.google.com/webstore/detail/do-not-consent/djcbfpkdhdkaflcigibkbpboflaplabg"; "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 * @constant firefoxUrl
* @description Firefox Add-ons link * @description Firefox Add-ons link
@ -25,43 +33,37 @@ const firefoxUrl =
const isChromium = chrome.runtime.getURL("").startsWith("chrome-extension://"); const isChromium = chrome.runtime.getURL("").startsWith("chrome-extension://");
/** /**
* @async
* @function handlePowerChange * @function handlePowerChange
* @description Disables or enables extension on current page * @description Disables or enables extension on current page
*/ */
const handlePowerChange = async () => { const handlePowerChange = () => {
chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ hostname, id }) => { dispatch({ type: "GET_TAB" }, null, ({ hostname, id }) => {
chrome.runtime.sendMessage( dispatch({ hostname, type: "GET_CACHE" }, null, ({ enabled }) => {
{ hostname, type: "GET_CACHE" },
null,
({ enabled }) => {
const power = document.getElementById("power"); const power = document.getElementById("power");
chrome.runtime.sendMessage({ dispatch({
hostname, hostname,
state: { enabled: !enabled }, state: { enabled: !enabled },
type: "UPDATE_CACHE", type: "UPDATE_CACHE",
}); });
chrome.runtime.sendMessage({ dispatch({
type: !enabled === true ? "ENABLE_ICON" : "DISABLE_ICON", type: !enabled === true ? "ENABLE_ICON" : "DISABLE_ICON",
}); });
if (!enabled === false) power.removeAttribute("checked"); if (!enabled === false) power.removeAttribute("checked");
if (!enabled === true) power.setAttribute("checked", "checked"); if (!enabled === true) power.setAttribute("checked", "checked");
chrome.tabs.reload(id, { bypassCache: true }); chrome.tabs.reload(id, { bypassCache: true });
} });
);
}); });
}; };
/** /**
* @async
* @function handleReload * @function handleReload
* @description Reload current page * @description Reload current page
*/ */
const handleReload = async () => { const handleReload = () => {
chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ id }) => { dispatch({ type: "GET_TAB" }, null, ({ id }) => {
chrome.tabs.reload(id, { bypassCache: true }); chrome.tabs.reload(id, { bypassCache: true });
}); });
}; };
@ -92,17 +94,13 @@ const handleRate = (event) => {
}; };
/** /**
* @async
* @function handleContentLoaded * @function handleContentLoaded
* @description Setup stars handlers and result message links * @description Setup stars handlers and result message links
*/ */
const handleContentLoaded = async () => { const handleContentLoaded = () => {
chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ hostname, id }) => { dispatch({ type: "GET_TAB" }, null, ({ hostname, id }) => {
chrome.runtime.sendMessage( dispatch({ hostname, type: "GET_CACHE" }, null, ({ enabled }) => {
{ hostname, type: "GET_CACHE" },
null,
({ enabled }) => {
const host = document.getElementById("host"); const host = document.getElementById("host");
const like = document.getElementById("like"); const like = document.getElementById("like");
const power = document.getElementById("power"); const power = document.getElementById("power");
@ -117,8 +115,7 @@ const handleContentLoaded = async () => {
unlike.addEventListener("click", handleRate); unlike.addEventListener("click", handleRate);
if (location) host.innerText = hostname.replace("www.", ""); if (location) host.innerText = hostname.replace("www.", "");
if (!enabled) power.removeAttribute("checked"); if (!enabled) power.removeAttribute("checked");
} });
);
}); });
}; };