refactor(scripts): bg and popup script to fit new structure
This commit is contained in:
parent
83a1a7fb22
commit
dded13c8f6
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
const disableIcon = (tabId) => {
|
const disableIcon = (tabId) => {
|
||||||
chrome.browserAction.setIcon({
|
chrome.browserAction.setIcon({
|
||||||
path: "assets/icon-disabled.png",
|
path: "assets/icons/disabled.png",
|
||||||
tabId: tabId,
|
tabId: tabId,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -35,7 +35,7 @@ const disablePopup = (tabId) => {
|
|||||||
|
|
||||||
const enableIcon = (tabId) => {
|
const enableIcon = (tabId) => {
|
||||||
chrome.browserAction.setIcon({
|
chrome.browserAction.setIcon({
|
||||||
path: "assets/icon-enabled.png",
|
path: "assets/icons/enabled.png",
|
||||||
tabId: tabId,
|
tabId: tabId,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -58,23 +58,26 @@ const enablePopup = (tabId) => {
|
|||||||
* @description Listens to content messages
|
* @description Listens to content messages
|
||||||
*/
|
*/
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener((request, sender) => {
|
chrome.runtime.onMessage.addListener((request) => {
|
||||||
const tabId = sender.tab ? sender.tab.id : undefined;
|
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||||
|
const tab = tabs[0];
|
||||||
|
const tabId = tab.id;
|
||||||
|
|
||||||
switch (request.type) {
|
switch (request.type) {
|
||||||
case "DISABLE_ICON":
|
case "DISABLE_ICON":
|
||||||
disableIcon(tabId);
|
disableIcon(tabId);
|
||||||
break;
|
break;
|
||||||
case "DISABLE_POPUP":
|
case "DISABLE_POPUP":
|
||||||
disablePopup(tabId);
|
disablePopup(tabId);
|
||||||
break;
|
break;
|
||||||
case "ENABLE_ICON":
|
case "ENABLE_ICON":
|
||||||
enableIcon(tabId);
|
enableIcon(tabId);
|
||||||
break;
|
break;
|
||||||
case "ENABLE_POPUP":
|
case "ENABLE_POPUP":
|
||||||
enablePopup(tabId);
|
enablePopup(tabId);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -47,7 +47,7 @@ const currentTab = () =>
|
|||||||
* @function currentState
|
* @function currentState
|
||||||
* @description Returns current extension state
|
* @description Returns current extension state
|
||||||
*
|
*
|
||||||
* @returns {Promise<Object<string, { enabled: boolean, matches: string[] }>>}>}
|
* @returns {Promise<{ enabled: boolean, matches: string[] }>}>}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const currentState = async () => {
|
const currentState = async () => {
|
||||||
@ -61,13 +61,12 @@ const currentState = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function handleButtonClick
|
* @async
|
||||||
* @description Disables or enables extension
|
* @function handlePowerChange
|
||||||
*
|
* @description Disables or enables extension on current page
|
||||||
* @param {MouseEvent} event
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const handleStateButtonClick = async () => {
|
const handlePowerChange = async () => {
|
||||||
const state = await currentState();
|
const state = await currentState();
|
||||||
const tab = await currentTab();
|
const tab = await currentTab();
|
||||||
|
|
||||||
@ -79,42 +78,51 @@ const handleStateButtonClick = async () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
() => {
|
() => {
|
||||||
const stateButton = document.getElementById("state-button");
|
const power = document.getElementById("power");
|
||||||
|
|
||||||
|
if (!state.enabled === true) {
|
||||||
|
power.setAttribute("checked", "checked");
|
||||||
|
chrome.runtime.sendMessage({ type: "ENABLE_ICON" });
|
||||||
|
} else {
|
||||||
|
power.removeAttribute("checked");
|
||||||
|
chrome.runtime.sendMessage({ type: "DISABLE_ICON" });
|
||||||
|
}
|
||||||
|
|
||||||
stateButton.textContent = state.enabled
|
|
||||||
? "Enable extension"
|
|
||||||
: "Disable extension";
|
|
||||||
chrome.runtime.sendMessage({
|
|
||||||
type: state.enabled ? "DISABLE_ICON" : "ENABLE:ICON",
|
|
||||||
});
|
|
||||||
chrome.tabs.reload(tab.id, { bypassCache: true });
|
chrome.tabs.reload(tab.id, { bypassCache: true });
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function handleStarClick
|
* @async
|
||||||
* @description Hides stars and shows negative or positive messages
|
* @function handleReload
|
||||||
|
* @description Reload current page
|
||||||
|
*/
|
||||||
|
|
||||||
|
const handleReload = async () => {
|
||||||
|
const tab = await currentTab();
|
||||||
|
|
||||||
|
chrome.tabs.reload(tab.id, { bypassCache: true });
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @function handleRate
|
||||||
|
* @description Shows negative or positive messages
|
||||||
*
|
*
|
||||||
* @param {MouseEvent} event
|
* @param {MouseEvent} event
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const handleStarClick = (event) => {
|
const handleRate = (event) => {
|
||||||
const negative = document.getElementById("negative");
|
const negative = document.getElementById("negative");
|
||||||
const positive = document.getElementById("positive");
|
const positive = document.getElementById("positive");
|
||||||
const { score } = event.currentTarget.dataset;
|
|
||||||
const stars = document.getElementById("stars");
|
|
||||||
|
|
||||||
switch (score) {
|
switch (event.currentTarget.id) {
|
||||||
case "1":
|
case "unlike":
|
||||||
case "2":
|
positive.setAttribute("hidden", "true");
|
||||||
case "3":
|
|
||||||
stars.setAttribute("hidden", "true");
|
|
||||||
negative.removeAttribute("hidden");
|
negative.removeAttribute("hidden");
|
||||||
break;
|
break;
|
||||||
case "4":
|
case "like":
|
||||||
case "5":
|
negative.setAttribute("hidden", "true");
|
||||||
stars.setAttribute("hidden", "true");
|
|
||||||
positive.removeAttribute("hidden");
|
positive.removeAttribute("hidden");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -123,22 +131,29 @@ const handleStarClick = (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 = async () => {
|
||||||
const stars = Array.from(document.getElementsByClassName("star"));
|
const host = document.getElementById("host");
|
||||||
|
const like = document.getElementById("like");
|
||||||
|
const power = document.getElementById("power");
|
||||||
|
const reload = document.getElementById("reload");
|
||||||
const state = await currentState();
|
const state = await currentState();
|
||||||
const stateButton = document.getElementById("state-button");
|
const store = document.getElementById("store");
|
||||||
const storeLink = document.getElementById("store-link");
|
const tab = await currentTab();
|
||||||
|
const unlike = document.getElementById("unlike");
|
||||||
|
|
||||||
stars.forEach((star) => star.addEventListener("click", handleStarClick));
|
like.addEventListener("click", handleRate);
|
||||||
stateButton.textContent = state.enabled
|
power.addEventListener("change", handlePowerChange);
|
||||||
? "Disable extension"
|
reload.addEventListener("click", handleReload);
|
||||||
: "Enable extension";
|
store.setAttribute("href", isChromium ? chromeUrl : firefoxUrl);
|
||||||
stateButton.addEventListener("click", handleStateButtonClick);
|
unlike.addEventListener("click", handleRate);
|
||||||
storeLink.setAttribute("href", isChromium ? chromeUrl : firefoxUrl);
|
|
||||||
|
if (tab.location) host.innerText = tab.location.hostname.replace("www.", "");
|
||||||
|
if (state.enabled) power.setAttribute("checked", "checked");
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user