Merge pull request #19 from wanhose/5.2.0

5.2.0
This commit is contained in:
wanhose 2021-10-30 14:28:34 +02:00 committed by GitHub
commit 98236962ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 120 deletions

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Cookie Dialog Monster", "name": "Cookie Dialog Monster",
"version": "5.1.0", "version": "5.2.0",
"default_locale": "en", "default_locale": "en",
"description": "__MSG_appDesc__", "description": "__MSG_appDesc__",
"icons": { "icons": {

View File

@ -1,11 +1,10 @@
/** /**
* @description Cache initial state * @description Cache initial state
* @type {{ enabled: boolean, matches: string[] }} * @type {{ enabled: boolean }}
*/ */
const initial = { const initial = {
enabled: true, enabled: true,
matches: [],
}; };
/** /**
@ -13,10 +12,7 @@ const initial = {
* @param {object} [cache] * @param {object} [cache]
*/ */
const check = (cache) => const check = (cache) => typeof cache.enabled === "boolean";
typeof cache.enabled === "boolean" &&
Array.isArray(cache.matches) &&
cache.matches.every((match) => typeof match === "string");
/** /**
* @description Disables icon * @description Disables icon
@ -70,7 +66,7 @@ const enablePopup = (tabId) => {
* @description Retrieves cache state * @description Retrieves cache state
* @param {string} [hostname] * @param {string} [hostname]
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state * @returns {Promise<{ enabled: boolean }>}
*/ */
const getCache = (hostname, callback) => { const getCache = (hostname, callback) => {
@ -92,7 +88,7 @@ const getCache = (hostname, callback) => {
* @async * @async
* @description Retrieves a selectors list * @description Retrieves a selectors list
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ classes: string[] }>}
*/ */
const getClasses = async (callback) => { const getClasses = async (callback) => {
@ -114,7 +110,7 @@ const getClasses = async (callback) => {
* @async * @async
* @description Retrieves a selectors list * @description Retrieves a selectors list
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ selectors: string }>}
*/ */
const getSelectors = async (callback) => { const getSelectors = async (callback) => {
@ -126,7 +122,7 @@ const getSelectors = async (callback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
callback({ selectors: data.split("\n") }); callback({ selectors: data.split("\n").join(",") });
} catch { } catch {
callback({ selectors: [] }); callback({ selectors: [] });
} }
@ -135,7 +131,7 @@ const getSelectors = async (callback) => {
/** /**
* @description Retrieves current tab information * @description Retrieves current tab information
* @param {void} [callback] * @param {void} [callback]
* @returns {Promise<{ id: string, location: string }>} Current tab information * @returns {Promise<{ id: string, location: string }>}
*/ */
const getTab = (callback) => { const getTab = (callback) => {
@ -163,10 +159,6 @@ const updateCache = (hostname, state) => {
typeof state.enabled === "undefined" typeof state.enabled === "undefined"
? current.enabled ? current.enabled
: state.enabled, : state.enabled,
matches:
typeof state.matches === "undefined"
? current.matches
: [...new Set([...current.matches, ...state.matches])],
}, },
}); });
}); });

View File

@ -1,16 +1,9 @@
/**
* @description Number of attempts
* @type {number}
*/
let attempts = 1;
/** /**
* @description Array of selectors * @description Array of selectors
* @type {string[]} * @type {string[]}
*/ */
let classesFromNetwork = []; let classes = [];
/** /**
* @description Shortcut to send messages to background script * @description Shortcut to send messages to background script
@ -20,32 +13,23 @@ let classesFromNetwork = [];
const dispatch = chrome.runtime.sendMessage; const dispatch = chrome.runtime.sendMessage;
/** /**
* @description Array of selectors * @description Options provided to observer
* @type {string[]}
*/ */
let selectorsFromCache = []; const options = { childList: true, subtree: true };
/** /**
* @description Array of selectors * @description Selectors list
* @type {Promise<string[]>[]} * @type {string}
*/ */
let selectorsFromNetwork = []; let selectors = "";
/** /**
* @description Split large arrays into promises * @description Target provided to observer
* @param {string[]} array
*/ */
const chunkerize = (array) => const target = document.body || document.documentElement;
[...Array(Math.ceil(array.length / 300))].map(
(_, index) => () =>
new Promise((resolve) => {
removeElements(array.slice(index * 300, (index + 1) * 300), true);
resolve(true);
})
);
/** /**
* @description Fixes scroll issues * @description Fixes scroll issues
@ -53,7 +37,6 @@ const chunkerize = (array) =>
const fix = () => { const fix = () => {
const body = document.body; const body = document.body;
const classes = classesFromNetwork;
const facebook = document.getElementsByClassName("_31e")[0]; const facebook = document.getElementsByClassName("_31e")[0];
const html = document.documentElement; const html = document.documentElement;
@ -63,116 +46,61 @@ const fix = () => {
if (html) html.style.setProperty("overflow-y", "unset", "important"); if (html) html.style.setProperty("overflow-y", "unset", "important");
}; };
/** const observer = new MutationObserver((mutations, instance) => {
* @description Removes matched elements from a selectors array instance.disconnect();
* @param {string[]} selectors
* @param {boolean} updateCache
*/
const removeElements = (selectors, updateCache) => {
for (let i = selectors.length; i--; ) {
const selector = selectors[i];
const element = search(selector);
if (element) {
const tagName = element.tagName.toUpperCase();
if (!["BODY", "HTML"].includes(tagName)) {
element.outerHTML = "";
if (updateCache) {
selectorsFromCache = [...selectorsFromCache, selector];
dispatch({
hostname: document.location.hostname,
state: { matches: [selector] },
type: "UPDATE_CACHE",
});
}
}
}
}
};
/**
* @async
* @description Runs tasks
*/
const run = async () => {
if (attempts <= 20) {
fix(); fix();
removeElements(selectorsFromCache);
if (selectorsFromNetwork.length > 0) { for (let i = mutations.length; i--; ) {
const selectors = selectorsFromNetwork; const mutation = mutations[i];
if (attempts <= 5) await Promise.all(selectors.map((fn) => fn())); for (let j = mutation.addedNodes.length; j--; ) {
if (document.readyState === "complete") attempts += 1; const node = mutation.addedNodes[j];
if (!(node instanceof HTMLElement)) continue;
if (node.matches(selectors)) node.outerHTML = "";
}
} }
requestAnimationFrame(run); instance.observe(target, options);
} });
};
/**
* @description Retrieves HTML element if selector exists
* @param {string} selector
* @returns {HTMLElement | null} An HTML element or null
*/
const search = (selector) => {
if (!selector.includes("[") && !selector.includes(">")) {
if (selector.startsWith(".")) {
return document.getElementsByClassName(selector.slice(1))[0];
}
if (selector.startsWith("#")) {
return document.getElementById(selector.slice(1));
}
} else {
return document.querySelector(selector);
}
return null;
};
/** /**
* @description Setups classes selectors * @description Setups classes selectors
* @returns {Promise<boolean>} * @returns {Promise<{ classes: string[] }>}
*/ */
const setupClasses = () => const setupClasses = () =>
new Promise((resolve) => { new Promise((resolve) => {
dispatch({ type: "GET_CLASSES" }, null, ({ classes }) => { dispatch({ type: "GET_CLASSES" }, null, resolve);
classesFromNetwork = classes;
resolve(true);
});
}); });
/** /**
* @description Setups elements selectors * @description Setups elements selectors
* @returns {Promise<boolean>} * @returns {Promise<{ selectors: string }>}
*/ */
const setupSelectors = () => const setupSelectors = () =>
new Promise((resolve) => { new Promise((resolve) => {
dispatch({ type: "GET_SELECTORS" }, null, ({ selectors }) => { dispatch({ type: "GET_SELECTORS" }, null, resolve);
selectorsFromNetwork = chunkerize(selectors);
resolve(true);
});
}); });
/**
* @description Setups everything and starts to observe if enabled
*/
dispatch( dispatch(
{ hostname: document.location.hostname, type: "GET_CACHE" }, { hostname: document.location.hostname, type: "GET_CACHE" },
null, null,
async ({ enabled, matches }) => { async ({ enabled }) => {
dispatch({ type: "ENABLE_POPUP" }); dispatch({ type: "ENABLE_POPUP" });
if (enabled) { if (enabled) {
selectorsFromCache = matches;
dispatch({ type: "ENABLE_ICON" }); dispatch({ type: "ENABLE_ICON" });
await Promise.all([setupClasses(), setupSelectors()]); const results = await Promise.all([setupClasses(), setupSelectors()]);
await run(); classes = results[0].classes;
selectors = results[1].selectors;
observer.observe(target, options);
} }
} }
); );