Merge pull request #4 from wanhose/4.0.1

4.0.1
This commit is contained in:
wanhose 2021-06-23 13:32:43 +02:00 committed by GitHub
commit 03a9104941
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 125 additions and 111 deletions

View File

@ -12523,7 +12523,6 @@ script[src="//clickio.mgr.consensu.org/t/consent_216889.js"]
#didomi-popup #didomi-popup
div[style="position: fixed; z-index: 9999; width: 100%; height: 100%; inset: 0px; background-color: rgba(0, 0, 0, 0.5);"] div[style="position: fixed; z-index: 9999; width: 100%; height: 100%; inset: 0px; background-color: rgba(0, 0, 0, 0.5);"]
#didomiConsentOverlay #didomiConsentOverlay
.softMessages-list
#acceptationCMPWall #acceptationCMPWall
.qc-cmp2-container .qc-cmp2-container
#qc-cmp2-container #qc-cmp2-container

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Do Not Consent", "name": "Do Not Consent",
"version": "4.0.0", "version": "4.0.1",
"default_locale": "en", "default_locale": "en",
"description": "__MSG_appDesc__", "description": "__MSG_appDesc__",
"icons": { "icons": {

View File

@ -9,11 +9,24 @@ const cacheInitialState = {
matches: [], matches: [],
}; };
/**
* @function isValid
* @description Check cache validity
*
* @param {object} [cache]
*/
const isValid = (cache) =>
typeof cache.enabled === "boolean" &&
Array.isArray(cache.matches) &&
cache.matches.every((match) => typeof match === "string");
/** /**
* @function disableIcon * @function disableIcon
* @description Disables icon * @description Disables icon
* *
* @param {string} [tabId] * @param {string} [tabId]
* @returns {boolean}
*/ */
const disableIcon = (tabId) => { const disableIcon = (tabId) => {
@ -70,16 +83,20 @@ const enablePopup = (tabId) => {
* @function getCache * @function getCache
* @description Retrieves cache state * @description Retrieves cache state
* *
* @param {string} [hostname]
* @param {void} [responseCallback]
* @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state * @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state
*/ */
const getCache = async (hostname, responseCallback) => { const getCache = async (hostname, responseCallback) => {
chrome.storage.local.get(null, (store) => { chrome.storage.local.get(null, (store) => {
const cache = store[hostname]; try {
const cache = store[hostname];
if (!isValid(cache)) throw new Error();
if (cache) {
responseCallback(cache); responseCallback(cache);
} else { } catch {
chrome.storage.local.set({ [hostname]: cacheInitialState }); chrome.storage.local.set({ [hostname]: cacheInitialState });
responseCallback(cacheInitialState); responseCallback(cacheInitialState);
} }
@ -88,19 +105,18 @@ const getCache = async (hostname, responseCallback) => {
/** /**
* @async * @async
* @function updateCache * @function getTab
* @description Update cache state * @description Retrieves current tab information
*
* @param {void} [responseCallback]
* @returns {Promise<{ id: string, location: string }>} Current tab information
*/ */
const updateCache = async (hostname, selector) => { const getTab = (responseCallback) => {
chrome.storage.local.get(null, (cache) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const current = cache[hostname]; responseCallback({
id: tabs[0].id,
chrome.storage.local.set({ hostname: new URL(tabs[0].url).hostname,
[hostname]: {
...current,
matches: [...new Set([...current.matches, selector])],
},
}); });
}); });
}; };
@ -110,6 +126,7 @@ const updateCache = async (hostname, selector) => {
* @function getList * @function getList
* @description Retrieves selectors list * @description Retrieves selectors list
* *
* @param {void} [responseCallback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ matches: string[] }>} A selectors list
*/ */
@ -122,12 +139,40 @@ const getList = async (responseCallback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
responseCallback({ matches: data.split("\n") }); responseCallback({ selectors: data.split("\n") });
} catch { } catch {
responseCallback({ matches: [] }); responseCallback({ selectors: [] });
} }
}; };
/**
* @async
* @function updateCache
* @description Update cache state
*
* @param {string} [hostname]
* @param {object} [state]
*/
const updateCache = async (hostname, state) => {
chrome.storage.local.get(null, (cache) => {
const current = cache[hostname];
chrome.storage.local.set({
[hostname]: {
enabled:
typeof state.enabled === "undefined"
? current.enabled
: state.enabled,
matches:
typeof state.matches === "undefined"
? current.matches
: [...new Set([...current.matches, ...state.matches])],
},
});
});
};
/** /**
* @description Listens to content messages * @description Listens to content messages
*/ */
@ -156,8 +201,11 @@ chrome.runtime.onMessage.addListener((request, sender, responseCallback) => {
case "GET_LIST": case "GET_LIST":
getList(responseCallback); getList(responseCallback);
break; break;
case "GET_TAB":
getTab(responseCallback);
break;
case "UPDATE_CACHE": case "UPDATE_CACHE":
updateCache(request.hostname, request.selector); updateCache(request.hostname, request.state);
break; break;
default: default:
break; break;

View File

@ -15,12 +15,12 @@ let attempts = 1;
let intervalId = 0; let intervalId = 0;
/** /**
* @var ready * @var loading
* @description Is extension ready? * @description Is extension loading?
* @type {boolean} * @type {boolean}
*/ */
let ready = false; let loading = true;
/** /**
* @var selectorsFromCache * @var selectorsFromCache
@ -51,7 +51,7 @@ const fix = () => {
if (body) body.style.setProperty("overflow-y", "unset", "important"); if (body) body.style.setProperty("overflow-y", "unset", "important");
if (facebook) facebook.style.setProperty("position", "unset", "important"); if (facebook) facebook.style.setProperty("position", "unset", "important");
if (html) html.style.setProperty("overflow-y", "unset", "important"); if (html) html.style.setProperty("overflow-y", "unset", "important");
if (ready) html.style.setProperty("opacity", "1", "important"); if (!loading) html.style.setProperty("opacity", "1", "important");
}; };
/** /**
@ -93,7 +93,7 @@ const removeFromCache = () => {
if (!["BODY", "HTML"].includes(tagName)) { if (!["BODY", "HTML"].includes(tagName)) {
element.remove(); element.remove();
ready = true; loading = false;
} }
} }
} }
@ -114,10 +114,10 @@ const removeFromNetwork = () => {
if (!["BODY", "HTML"].includes(tagName)) { if (!["BODY", "HTML"].includes(tagName)) {
element.remove(); element.remove();
ready = true; loading = false;
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
hostname: document.location.hostname, hostname: document.location.hostname,
selector, state: { matches: [selector] },
type: "UPDATE_CACHE", type: "UPDATE_CACHE",
}); });
} }
@ -131,13 +131,15 @@ const removeFromNetwork = () => {
*/ */
const runTasks = () => { const runTasks = () => {
if (attempts >= 5 || selectorsFromCache.length === 0) ready = true; if (attempts >= 5 || selectorsFromCache.length === 0) {
loading = false;
}
if (attempts <= 20) { if (attempts <= 20) {
fix(); fix();
removeFromCache(); removeFromCache();
if (attempts <= 5) removeFromNetwork(); if (attempts <= 5) removeFromNetwork();
attempts += 1; if (document.readyState !== "loading") attempts += 1;
} }
if (attempts > 20) { if (attempts > 20) {
@ -152,18 +154,17 @@ const runTasks = () => {
chrome.runtime.sendMessage( chrome.runtime.sendMessage(
{ hostname: document.location.hostname, type: "GET_CACHE" }, { hostname: document.location.hostname, type: "GET_CACHE" },
null, null,
async (cacheResponse) => { async ({ enabled, matches }) => {
console.log(cacheResponse);
chrome.runtime.sendMessage({ type: "ENABLE_POPUP" }); chrome.runtime.sendMessage({ type: "ENABLE_POPUP" });
if (cacheResponse.enabled) { if (enabled) {
selectorsFromCache = cacheResponse.matches; selectorsFromCache = matches;
chrome.runtime.sendMessage({ type: "ENABLE_ICON" }); chrome.runtime.sendMessage({ type: "ENABLE_ICON" });
chrome.runtime.sendMessage( chrome.runtime.sendMessage(
{ type: "GET_LIST" }, { type: "GET_LIST" },
null, null,
async (networkResponse) => { async ({ selectors }) => {
selectorsFromNetwork = networkResponse.matches; selectorsFromNetwork = selectors;
intervalId = setInterval(runTasks, 500); intervalId = setInterval(runTasks, 500);
} }
); );

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"); }
);
});
}; };
/** /**