From e5285cb88546c35c4624eba33375324765497069 Mon Sep 17 00:00:00 2001 From: wanhose Date: Tue, 17 May 2022 17:28:40 +0200 Subject: [PATCH] feat(browser-extension): add attributes to mutation observer --- .../browser-extension/src/scripts/content.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/browser-extension/src/scripts/content.js b/packages/browser-extension/src/scripts/content.js index 5d13144..f54d039 100644 --- a/packages/browser-extension/src/scripts/content.js +++ b/packages/browser-extension/src/scripts/content.js @@ -40,9 +40,10 @@ const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivac /** * @description Options provided to observer + * @type {MutationObserverInit} */ -const options = { childList: true, subtree: true }; +const options = { attributes: true, childList: true, subtree: true }; /** * @description Selectors list @@ -76,13 +77,7 @@ const check = (node) => * @returns {void} */ -const clean = (nodes) => { - if (selectors.length) { - nodes.filter(check).forEach((node) => { - if (node.parentElement) node.outerHTML = ''; - }); - } -}; +const clean = (nodes) => nodes.filter(check).forEach((node) => (node.outerHTML = '')); /** * @description Fixes scroll issues @@ -139,7 +134,7 @@ const observer = new MutationObserver((mutations, instance) => { instance.disconnect(); fix(); - if (!preview) clean(nodes); + if (!preview && selectors.length) clean(nodes); instance.observe(target, options); }); @@ -163,8 +158,8 @@ const promiseAll = () => document.addEventListener('readystatechange', () => { dispatch({ hostname, type: 'GET_CACHE' }, null, async ({ enabled }) => { - if (document.readyState === 'complete' && enabled && !preview && selectors.length) { - const nodes = Array.from(document.querySelectorAll(selectors)); + if (document.readyState === 'complete' && enabled && !preview) { + const nodes = selectors.length ? Array.from(document.querySelectorAll(selectors)) : []; fix(); clean(nodes);