feat(browser-extension): add attributes to mutation observer

This commit is contained in:
wanhose 2022-05-17 17:28:40 +02:00
parent fbfc76eb3d
commit e5285cb885

View File

@ -40,9 +40,10 @@ const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivac
/** /**
* @description Options provided to observer * @description Options provided to observer
* @type {MutationObserverInit}
*/ */
const options = { childList: true, subtree: true }; const options = { attributes: true, childList: true, subtree: true };
/** /**
* @description Selectors list * @description Selectors list
@ -76,13 +77,7 @@ const check = (node) =>
* @returns {void} * @returns {void}
*/ */
const clean = (nodes) => { const clean = (nodes) => nodes.filter(check).forEach((node) => (node.outerHTML = ''));
if (selectors.length) {
nodes.filter(check).forEach((node) => {
if (node.parentElement) node.outerHTML = '';
});
}
};
/** /**
* @description Fixes scroll issues * @description Fixes scroll issues
@ -139,7 +134,7 @@ const observer = new MutationObserver((mutations, instance) => {
instance.disconnect(); instance.disconnect();
fix(); fix();
if (!preview) clean(nodes); if (!preview && selectors.length) clean(nodes);
instance.observe(target, options); instance.observe(target, options);
}); });
@ -163,8 +158,8 @@ const promiseAll = () =>
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 && !preview && selectors.length) { if (document.readyState === 'complete' && enabled && !preview) {
const nodes = Array.from(document.querySelectorAll(selectors)); const nodes = selectors.length ? Array.from(document.querySelectorAll(selectors)) : [];
fix(); fix();
clean(nodes); clean(nodes);