Merge pull request #30 from wanhose/5.4.2

5.4.2
This commit is contained in:
wanhose 2021-11-10 15:47:16 +01:00 committed by GitHub
commit ccb720f2bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 91 deletions

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Cookie Dialog Monster", "name": "Cookie Dialog Monster",
"version": "5.4.1", "version": "5.4.2",
"default_locale": "en", "default_locale": "en",
"description": "__MSG_appDesc__", "description": "__MSG_appDesc__",
"icons": { "icons": {
@ -20,7 +20,11 @@
"content_scripts": [ "content_scripts": [
{ {
"all_frames": true, "all_frames": true,
"exclude_matches": ["*://*.gfycat.com/*"], "exclude_matches": [
"*://*.gfycat.com/*",
"*://*.gmx.com/*",
"*://*.rundschau-online.de/*"
],
"js": ["scripts/content.js"], "js": ["scripts/content.js"],
"matches": ["http://*/*", "https://*/*"], "matches": ["http://*/*", "https://*/*"],
"run_at": "document_start" "run_at": "document_start"

View File

@ -112,6 +112,28 @@ const getClasses = async (callback) => {
} }
}; };
/**
* @async
* @description Retrieves a selectors list
* @param {void} [callback]
* @returns {Promise<{ classes: string[] }>}
*/
const getFixes = async (callback) => {
try {
const url =
"https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/fixes.txt";
const response = await fetch(url);
const data = await response.text();
if (response.status !== 200) throw new Error();
callback({ fixes: data.split("\n") });
} catch {
callback({ fixes: [] });
}
};
/** /**
* @async * @async
* @description Retrieves a selectors list * @description Retrieves a selectors list
@ -128,7 +150,7 @@ const getSelectors = async (callback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
callback({ selectors: data.split("\n").join(",") }); callback({ selectors: data.split("\n") });
} catch { } catch {
callback({ selectors: [] }); callback({ selectors: [] });
} }
@ -156,11 +178,13 @@ const getTab = (callback) => {
const report = () => { const report = () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0]; const tab = tabs[0];
const userAgent = window.navigator.userAgent;
const version = chrome.runtime.getManifest().version;
if (tab) { if (tab) {
fetch("https://cdm-report-service.herokuapp.com/rest/v1/report/", { fetch("https://cdm-report-service.herokuapp.com/rest/v1/report/", {
body: JSON.stringify({ body: JSON.stringify({
text: tab.url, text: `There's a problem with ${tab.url} using ${userAgent} in CDM ${version}`,
to: "wanhose.development@gmail.com", to: "wanhose.development@gmail.com",
subject: "Cookie Dialog Monster Report", subject: "Cookie Dialog Monster Report",
}), }),
@ -183,8 +207,6 @@ const updateCache = (hostname, state) => {
chrome.storage.local.get(null, (cache) => { chrome.storage.local.get(null, (cache) => {
const current = cache[hostname]; const current = cache[hostname];
if (!state.enabled) report();
chrome.storage.local.set({ chrome.storage.local.set({
[hostname]: { [hostname]: {
enabled: enabled:
@ -204,25 +226,18 @@ chrome.runtime.onMessage.addListener((request, sender, callback) => {
const hasPermission = !sender.frameId || sender.frameId === 0; const hasPermission = !sender.frameId || sender.frameId === 0;
let tabId = sender.tab ? sender.tab.id : undefined; let tabId = sender.tab ? sender.tab.id : undefined;
if (!tabId) {
chrome.tabs.query(
{ active: true, currentWindow: true },
(tabs) => (tabId = tabs[0] ? tabs[0].id : 0)
);
}
switch (request.type) { switch (request.type) {
case "DISABLE_ICON": case "DISABLE_ICON":
if (hasPermission) disableIcon(tabId); if (hasPermission && tabId) disableIcon(tabId);
break; break;
case "DISABLE_POPUP": case "DISABLE_POPUP":
if (hasPermission) disablePopup(tabId); if (hasPermission && tabId) disablePopup(tabId);
break; break;
case "ENABLE_ICON": case "ENABLE_ICON":
if (hasPermission) enableIcon(tabId); if (hasPermission && tabId) enableIcon(tabId);
break; break;
case "ENABLE_POPUP": case "ENABLE_POPUP":
if (hasPermission) enablePopup(tabId); if (hasPermission && tabId) enablePopup(tabId);
break; break;
case "GET_CACHE": case "GET_CACHE":
getCache(request.hostname, callback); getCache(request.hostname, callback);
@ -230,6 +245,9 @@ chrome.runtime.onMessage.addListener((request, sender, callback) => {
case "GET_CLASSES": case "GET_CLASSES":
getClasses(callback); getClasses(callback);
break; break;
case "GET_FIXES":
getFixes(callback);
break;
case "GET_SELECTORS": case "GET_SELECTORS":
getSelectors(callback); getSelectors(callback);
break; break;

View File

@ -12,6 +12,13 @@ let classes = [];
const dispatch = chrome.runtime.sendMessage; const dispatch = chrome.runtime.sendMessage;
/**
* @description Array of instructions
* @type {string[]}
*/
let fixes = [];
/** /**
* @description Hostname * @description Hostname
*/ */
@ -22,20 +29,20 @@ const hostname = document.location.hostname;
* @description Is consent preview page? * @description Is consent preview page?
*/ */
const isConsentPreview = hostname.startsWith("consent."); const isPreview = hostname.startsWith("consent.");
/** /**
* @description Options provided to observer * @description Options provided to observer
*/ */
const options = { childList: true, subtree: true }; const options = { attributes: true, childList: true, subtree: true };
/** /**
* @description Selectors list * @description Selectors list
* @type {string} * @type {string[]}
*/ */
let selectors = ""; let selectors = [];
/** /**
* @description Target provided to observer * @description Target provided to observer
@ -51,8 +58,8 @@ const target = document.body || document.documentElement;
const check = (node) => const check = (node) =>
node instanceof HTMLElement && node instanceof HTMLElement &&
node.parentElement && node.parentElement &&
!["APP", "ROOT"].includes(node.id.toUpperCase()) && !["BODY", "HTML"].includes(node.tagName) &&
!["BODY", "HTML"].includes(node.tagName); !(node.id && ["APP", "ROOT"].includes(node.id.toUpperCase()));
/** /**
* @description Cleans DOM * @description Cleans DOM
@ -62,12 +69,11 @@ const clean = () => {
if (selectors.length) { if (selectors.length) {
const nodes = document.querySelectorAll(selectors); const nodes = document.querySelectorAll(selectors);
for (let i = nodes.length; i--; ) { nodes.forEach((node) => {
const node = nodes[i];
const valid = check(node); const valid = check(node);
if (valid) node.outerHTML = ""; if (valid) node.outerHTML = "";
} });
} }
}; };
@ -76,108 +82,100 @@ const clean = () => {
*/ */
const fix = () => { const fix = () => {
const automobiel = /automobielmanagement.nl/g.test(hostname);
const body = document.body; const body = document.body;
const facebook = document.getElementsByClassName("_31e")[0]; const frame = !(window === window.parent || window.opener);
const frame = document.location.ancestorOrigins.length;
const google = document.querySelector('form[action*="consent.google"]');
const html = document.documentElement; const html = document.documentElement;
const play = hostname.startsWith("play.google.");
const yahoo = document.querySelector("#consent-page");
if (automobiel && body) { if (body && html) {
for (let i = body.childNodes.length; i--; ) {
const node = body.childNodes[i];
if (node instanceof HTMLElement) {
node.style.setProperty("filter", "initial", "important");
}
}
}
if (body) {
body.classList.remove(...classes); body.classList.remove(...classes);
if (!frame) {
body.style.setProperty("overflow-y", "initial", "important");
body.style.setProperty("position", "initial", "important");
}
}
if (facebook) {
facebook.style.setProperty("position", "initial", "important");
}
if (google) {
const submit = google.querySelector("button");
if (submit && submit instanceof HTMLElement) {
submit.click();
}
}
if (html) {
html.classList.remove(...classes); html.classList.remove(...classes);
if (!frame) { if (!frame) {
body.style.setProperty("position", "initial", "important");
body.style.setProperty("overflow-y", "initial", "important");
html.style.setProperty("position", "initial", "important"); html.style.setProperty("position", "initial", "important");
html.style.setProperty("overflow-y", "initial", "important"); html.style.setProperty("overflow-y", "initial", "important");
} }
} }
if (play) { fixes.forEach((fix) => {
const node = document.querySelector("body > div"); const [match, selector, action, property] = fix.split("##");
if (node && node instanceof HTMLElement) { if (hostname.includes(match)) {
node.style.setProperty("z-index", "initial", "important"); switch (action) {
case "click":
const submit = document.querySelector(selector);
if (submit && submit instanceof HTMLElement) {
submit.click();
}
break;
case "reset":
const node = document.querySelector(selector);
if (node && node instanceof HTMLElement) {
node.style.setProperty(property, "initial", "important");
}
break;
case "resetAll":
const nodes = document.querySelectorAll(selector);
nodes.forEach((node) => {
if (node instanceof HTMLElement) {
node.style.setProperty(property, "initial", "important");
}
});
break;
default:
break;
}
} }
} });
if (yahoo) {
const submit = yahoo.querySelector('button[type="submit"]');
if (submit && submit instanceof HTMLElement) {
submit.click();
}
}
}; };
const observer = new MutationObserver((mutations, instance) => { const observer = new MutationObserver((mutations, instance) => {
instance.disconnect(); instance.disconnect();
fix(); fix();
if (!isConsentPreview) { if (!isPreview) {
for (let i = mutations.length; i--; ) { mutations.forEach((mutation) => {
const mutation = mutations[i]; mutation.addedNodes.forEach((node) => {
for (let j = mutation.addedNodes.length; j--; ) {
const node = mutation.addedNodes[j];
const valid = check(node); const valid = check(node);
if (valid && node.matches(selectors)) node.outerHTML = ""; if (valid && node.matches(selectors)) node.outerHTML = "";
} });
} });
} }
instance.observe(target, options); instance.observe(target, options);
}); });
/** /**
* @description Setups classes selectors * @description Queries classes selectors
* @returns {Promise<{ classes: string[] }>} * @returns {Promise<{ classes: string[] }>}
*/ */
const setupClasses = () => const queryClasses = () =>
new Promise((resolve) => { new Promise((resolve) => {
dispatch({ type: "GET_CLASSES" }, null, resolve); dispatch({ type: "GET_CLASSES" }, null, resolve);
}); });
/** /**
* @description Setups elements selectors * @description Queries fixes instructions
* @returns {Promise<{ fixes: string[] }>}
*/
const queryFixes = () =>
new Promise((resolve) => {
dispatch({ type: "GET_FIXES" }, null, resolve);
});
/**
* @description Queries elements selectors
* @returns {Promise<{ selectors: string }>} * @returns {Promise<{ selectors: string }>}
*/ */
const setupSelectors = () => const querySelectors = () =>
new Promise((resolve) => { new Promise((resolve) => {
dispatch({ type: "GET_SELECTORS" }, null, resolve); dispatch({ type: "GET_SELECTORS" }, null, resolve);
}); });
@ -189,7 +187,7 @@ const setupSelectors = () =>
document.addEventListener("readystatechange", () => { document.addEventListener("readystatechange", () => {
dispatch({ hostname, type: "GET_CACHE" }, null, async ({ enabled }) => { dispatch({ hostname, type: "GET_CACHE" }, null, async ({ enabled }) => {
if (document.readyState === "complete" && enabled && !isConsentPreview) { if (document.readyState === "complete" && enabled && !isPreview) {
fix(); fix();
clean(); clean();
setTimeout(clean, 2000); setTimeout(clean, 2000);
@ -206,9 +204,14 @@ dispatch({ hostname, type: "GET_CACHE" }, null, async ({ enabled }) => {
if (enabled) { if (enabled) {
dispatch({ type: "ENABLE_ICON" }); dispatch({ type: "ENABLE_ICON" });
const results = await Promise.all([setupClasses(), setupSelectors()]); const results = await Promise.all([
queryClasses(),
queryFixes(),
querySelectors(),
]);
classes = results[0].classes; classes = results[0].classes;
selectors = results[1].selectors; fixes = results[1].fixes;
selectors = results[2].selectors;
observer.observe(target, options); observer.observe(target, options);
} }
}); });