refactor(scripts): replace popup old methods by new ones
This commit is contained in:
parent
b0966d62dd
commit
c8ba15d392
@ -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" },
|
||||||
chrome.storage.local.set(
|
null,
|
||||||
{
|
({ enabled }) => {
|
||||||
[tab.location.hostname]: {
|
|
||||||
...state,
|
|
||||||
enabled: !state.enabled,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
const power = document.getElementById("power");
|
const power = document.getElementById("power");
|
||||||
|
|
||||||
if (!state.enabled === true) {
|
chrome.runtime.sendMessage({
|
||||||
power.setAttribute("checked", "checked");
|
hostname,
|
||||||
chrome.runtime.sendMessage({ type: "ENABLE_ICON" });
|
state: { enabled: !enabled },
|
||||||
} else {
|
type: "UPDATE_CACHE",
|
||||||
power.removeAttribute("checked");
|
});
|
||||||
chrome.runtime.sendMessage({ type: "DISABLE_ICON" });
|
chrome.runtime.sendMessage({
|
||||||
}
|
type: !enabled === true ? "ENABLE_ICON" : "DISABLE_ICON",
|
||||||
|
});
|
||||||
chrome.tabs.reload(tab.id, { bypassCache: true });
|
if (!enabled === false) power.removeAttribute("checked");
|
||||||
|
if (!enabled === true) power.setAttribute("checked", "checked");
|
||||||
|
chrome.tabs.reload(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,13 +98,16 @@ const handleRate = (event) => {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const handleContentLoaded = async () => {
|
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 host = document.getElementById("host");
|
||||||
const like = document.getElementById("like");
|
const like = document.getElementById("like");
|
||||||
const power = document.getElementById("power");
|
const power = document.getElementById("power");
|
||||||
const reload = document.getElementById("reload");
|
const reload = document.getElementById("reload");
|
||||||
const state = await currentState();
|
|
||||||
const store = document.getElementById("store");
|
const store = document.getElementById("store");
|
||||||
const tab = await currentTab();
|
|
||||||
const unlike = document.getElementById("unlike");
|
const unlike = document.getElementById("unlike");
|
||||||
|
|
||||||
like.addEventListener("click", handleRate);
|
like.addEventListener("click", handleRate);
|
||||||
@ -151,9 +115,11 @@ const handleContentLoaded = async () => {
|
|||||||
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");
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user