diff --git a/packages/browser-extension/src/manifest.json b/packages/browser-extension/src/manifest.json index f464a51..cc6a785 100644 --- a/packages/browser-extension/src/manifest.json +++ b/packages/browser-extension/src/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Cookie Dialog Monster", - "version": "6.0.2", + "version": "6.0.3", "default_locale": "en", "description": "__MSG_appDesc__", "icons": { diff --git a/packages/browser-extension/src/scripts/content.js b/packages/browser-extension/src/scripts/content.js index ba7dd94..2ec0a71 100644 --- a/packages/browser-extension/src/scripts/content.js +++ b/packages/browser-extension/src/scripts/content.js @@ -31,6 +31,13 @@ const options = { childList: true, subtree: true }; const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.'); +/** + * @description Element that were being removed count + * @type {number} + */ + +let elementCount = 0; + /** * @description Extension state * @type {{ enabled: boolean }} @@ -38,6 +45,39 @@ const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivac let state = { enabled: true }; +/** + * @description Cleans DOM + * @param {Element[]} nodes + * @param {boolean?} skipMatch + * @returns {void} + */ + +const clean = (nodes, skipMatch) => { + const targets = nodes.filter((node) => skipMatch || match(node)); + + targets.forEach((node) => { + node.remove(); + elementCount += 1; + }); +}; + +/** + * @description Cleans DOM + * @returns {void} + */ + +const forceClean = () => { + if (data?.elements.length && state.enabled && !preview) { + const nodes = [...document.querySelectorAll(data.elements)]; + + if (nodes.length) { + fix(); + clean(nodes, true); + elementCount += nodes.length; + } + } +}; + /** * @description Matches if node element is removable * @param {Element} node @@ -60,16 +100,6 @@ const match = (node) => { ); }; -/** - * @description Cleans DOM - * @param {Element[]} nodes - * @param {boolean?} skipMatch - * @returns {void} - */ - -const clean = (nodes, skipMatch) => - nodes.filter((node) => skipMatch || match(node)).forEach((node) => node.remove()); - /** * @description Fixes scroll issues */ @@ -130,18 +160,21 @@ const observer = new MutationObserver((mutations) => { }); /** - * @async + * @description Fixes already existing element when page load issues + * @listens window#load + */ + +window.addEventListener('load', () => { + if (elementCount < 2) forceClean(); +}); + +/** * @description Fixes bfcache issues * @listens window#pageshow */ window.addEventListener('pageshow', (event) => { - if (data?.elements.length && event.persisted && state.enabled && !preview) { - const nodes = [...document.querySelectorAll(data.elements)]; - - fix(); - if (nodes.length) clean(nodes, true); - } + if (event.persisted) forceClean(); }); /**