2021-04-08 21:50:24 +00:00
|
|
|
/**
|
|
|
|
* @constant chromeUrl
|
|
|
|
* @description Chrome Web Store link
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const chromeUrl =
|
|
|
|
"https://chrome.google.com/webstore/detail/do-not-consent/djcbfpkdhdkaflcigibkbpboflaplabg";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant firefoxUrl
|
|
|
|
* @description Firefox Add-ons link
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const firefoxUrl =
|
|
|
|
"https://addons.mozilla.org/es/firefox/addon/do-not-consent/";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant isChromium
|
|
|
|
* @description Is current browser an instance of Chromium?
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
2021-04-11 09:44:12 +00:00
|
|
|
* @returns {Promise<{ enabled: boolean, matches: string[] }>}>}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const currentState = async () => {
|
|
|
|
const tab = await currentTab();
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.storage.local.get(null, (store) => {
|
|
|
|
resolve(store[tab.location.hostname]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-04-11 09:44:12 +00:00
|
|
|
* @async
|
|
|
|
* @function handlePowerChange
|
|
|
|
* @description Disables or enables extension on current page
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-04-11 09:44:12 +00:00
|
|
|
const handlePowerChange = async () => {
|
2021-04-08 21:50:24 +00:00
|
|
|
const state = await currentState();
|
|
|
|
const tab = await currentTab();
|
|
|
|
|
|
|
|
chrome.storage.local.set(
|
|
|
|
{
|
|
|
|
[tab.location.hostname]: {
|
|
|
|
...state,
|
|
|
|
enabled: !state.enabled,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
() => {
|
2021-04-11 09:44:12 +00:00
|
|
|
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" });
|
|
|
|
}
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
chrome.tabs.reload(tab.id, { bypassCache: true });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-04-11 09:44:12 +00:00
|
|
|
* @async
|
|
|
|
* @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
|
2021-04-08 21:50:24 +00:00
|
|
|
*
|
|
|
|
* @param {MouseEvent} event
|
|
|
|
*/
|
|
|
|
|
2021-04-11 09:44:12 +00:00
|
|
|
const handleRate = (event) => {
|
2021-04-08 21:50:24 +00:00
|
|
|
const negative = document.getElementById("negative");
|
|
|
|
const positive = document.getElementById("positive");
|
2021-04-11 09:44:12 +00:00
|
|
|
|
|
|
|
switch (event.currentTarget.id) {
|
|
|
|
case "unlike":
|
|
|
|
positive.setAttribute("hidden", "true");
|
2021-04-08 21:50:24 +00:00
|
|
|
negative.removeAttribute("hidden");
|
|
|
|
break;
|
2021-04-11 09:44:12 +00:00
|
|
|
case "like":
|
|
|
|
negative.setAttribute("hidden", "true");
|
2021-04-08 21:50:24 +00:00
|
|
|
positive.removeAttribute("hidden");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-04-11 09:44:12 +00:00
|
|
|
* @async
|
2021-04-08 21:50:24 +00:00
|
|
|
* @function handleContentLoaded
|
|
|
|
* @description Setup stars handlers and result message links
|
|
|
|
*/
|
|
|
|
|
|
|
|
const handleContentLoaded = async () => {
|
2021-04-11 09:44:12 +00:00
|
|
|
const host = document.getElementById("host");
|
|
|
|
const like = document.getElementById("like");
|
|
|
|
const power = document.getElementById("power");
|
|
|
|
const reload = document.getElementById("reload");
|
2021-04-08 21:50:24 +00:00
|
|
|
const state = await currentState();
|
2021-04-11 09:44:12 +00:00
|
|
|
const store = document.getElementById("store");
|
|
|
|
const tab = await currentTab();
|
|
|
|
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 (tab.location) host.innerText = tab.location.hostname.replace("www.", "");
|
2021-04-11 10:47:23 +00:00
|
|
|
if (!state.enabled) power.removeAttribute("checked");
|
2021-04-08 21:50:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Listen to document ready
|
|
|
|
*
|
|
|
|
* @type {Document}
|
|
|
|
* @listens document#ready
|
|
|
|
*/
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", handleContentLoaded);
|