refactor(scripts): replace popup old methods by new ones

This commit is contained in:
wanhose 2021-06-23 13:26:29 +02:00
parent b0966d62dd
commit c8ba15d392

View File

@ -24,42 +24,6 @@ const firefoxUrl =
const isChromium = chrome.runtime.getURL("").startsWith("chrome-extension://"); const isChromium = chrome.runtime.getURL("").startsWith("chrome-extension://");
/**
* @async
* @function currentTab
* @description Returns current tab state
*
* @returns {Promise<{ id: string, location: URL }>}
*/
const currentTab = () =>
new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
resolve({
id: tabs[0].id,
location: new URL(tabs[0].url),
});
});
});
/**
* @async
* @function currentState
* @description Returns current extension state
*
* @returns {Promise<{ enabled: boolean, matches: string[] }>}>}
*/
const currentState = async () => {
const tab = await currentTab();
return new Promise((resolve) => {
chrome.storage.local.get(null, (store) => {
resolve(store[tab.location.hostname]);
});
});
};
/** /**
* @async * @async
* @function handlePowerChange * @function handlePowerChange
@ -67,30 +31,27 @@ const currentState = async () => {
*/ */
const handlePowerChange = async () => { const handlePowerChange = async () => {
const state = await currentState(); chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ hostname, id }) => {
const tab = await currentTab(); chrome.runtime.sendMessage(
{ hostname, type: "GET_CACHE" },
null,
({ enabled }) => {
const power = document.getElementById("power");
chrome.storage.local.set( chrome.runtime.sendMessage({
{ hostname,
[tab.location.hostname]: { state: { enabled: !enabled },
...state, type: "UPDATE_CACHE",
enabled: !state.enabled, });
}, chrome.runtime.sendMessage({
}, type: !enabled === true ? "ENABLE_ICON" : "DISABLE_ICON",
() => { });
const power = document.getElementById("power"); if (!enabled === false) power.removeAttribute("checked");
if (!enabled === true) power.setAttribute("checked", "checked");
if (!state.enabled === true) { chrome.tabs.reload(id, { bypassCache: true });
power.setAttribute("checked", "checked");
chrome.runtime.sendMessage({ type: "ENABLE_ICON" });
} else {
power.removeAttribute("checked");
chrome.runtime.sendMessage({ type: "DISABLE_ICON" });
} }
);
chrome.tabs.reload(tab.id, { bypassCache: true }); });
}
);
}; };
/** /**
@ -100,9 +61,9 @@ const handlePowerChange = async () => {
*/ */
const handleReload = async () => { const handleReload = async () => {
const tab = await currentTab(); chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ id }) => {
chrome.tabs.reload(id, { bypassCache: true });
chrome.tabs.reload(tab.id, { bypassCache: true }); });
}; };
/** /**
@ -137,23 +98,28 @@ const handleRate = (event) => {
*/ */
const handleContentLoaded = async () => { const handleContentLoaded = async () => {
const host = document.getElementById("host"); chrome.runtime.sendMessage({ type: "GET_TAB" }, null, ({ hostname, id }) => {
const like = document.getElementById("like"); chrome.runtime.sendMessage(
const power = document.getElementById("power"); { hostname, type: "GET_CACHE" },
const reload = document.getElementById("reload"); null,
const state = await currentState(); ({ enabled }) => {
const store = document.getElementById("store"); const host = document.getElementById("host");
const tab = await currentTab(); const like = document.getElementById("like");
const unlike = document.getElementById("unlike"); const power = document.getElementById("power");
const reload = document.getElementById("reload");
const store = document.getElementById("store");
const unlike = document.getElementById("unlike");
like.addEventListener("click", handleRate); like.addEventListener("click", handleRate);
power.addEventListener("change", handlePowerChange); power.addEventListener("change", handlePowerChange);
reload.addEventListener("click", handleReload); reload.addEventListener("click", handleReload);
store.setAttribute("href", isChromium ? chromeUrl : firefoxUrl); store.setAttribute("href", isChromium ? chromeUrl : firefoxUrl);
unlike.addEventListener("click", handleRate); unlike.addEventListener("click", handleRate);
if (location) host.innerText = hostname.replace("www.", "");
if (tab.location) host.innerText = tab.location.hostname.replace("www.", ""); if (!enabled) power.removeAttribute("checked");
if (!state.enabled) power.removeAttribute("checked"); }
);
});
}; };
/** /**