feat(browser-extension): improve content logic
This commit is contained in:
parent
9a735beaeb
commit
8880ae855c
@ -31,13 +31,6 @@ const options = { childList: true, subtree: true };
|
|||||||
|
|
||||||
const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.');
|
const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.');
|
||||||
|
|
||||||
/**
|
|
||||||
* @description Element that were being removed count
|
|
||||||
* @type {number}
|
|
||||||
*/
|
|
||||||
|
|
||||||
let elementCount = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Extension state
|
* @description Extension state
|
||||||
* @type {{ enabled: boolean }}
|
* @type {{ enabled: boolean }}
|
||||||
@ -62,7 +55,6 @@ const clean = (nodes, skipMatch) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!node.hasAttribute('data-cookie-dialog-monster')) {
|
if (!node.hasAttribute('data-cookie-dialog-monster')) {
|
||||||
elementCount += 1;
|
|
||||||
node.setAttribute('data-cookie-dialog-monster', 'true');
|
node.setAttribute('data-cookie-dialog-monster', 'true');
|
||||||
node.style.setProperty('display', 'none', 'important');
|
node.style.setProperty('display', 'none', 'important');
|
||||||
observer.observe(node, { attributes: true, attributeFilter: ['style'] });
|
observer.observe(node, { attributes: true, attributeFilter: ['style'] });
|
||||||
@ -72,7 +64,7 @@ const clean = (nodes, skipMatch) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Cleans DOM
|
* @description Forces a DOM clean
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -83,7 +75,6 @@ const forceClean = () => {
|
|||||||
if (nodes.length) {
|
if (nodes.length) {
|
||||||
fix();
|
fix();
|
||||||
clean(nodes, true);
|
clean(nodes, true);
|
||||||
elementCount += nodes.length;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -95,47 +86,26 @@ const forceClean = () => {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const isInViewport = (node) => {
|
const isInViewport = (node) => {
|
||||||
const bounding = node.getBoundingClientRect();
|
const height = window.innerHeight || document.documentElement.clientHeight;
|
||||||
|
const position = node.getBoundingClientRect();
|
||||||
|
const scroll = window.scrollY || window.pageYOffset;
|
||||||
|
|
||||||
return (
|
return scroll + position.top <= scroll + height && scroll + position.bottom >= scroll;
|
||||||
bounding.top >= -node.offsetHeight &&
|
|
||||||
bounding.left >= -node.offsetWidth &&
|
|
||||||
bounding.right <=
|
|
||||||
(window.innerWidth || document.documentElement.clientWidth) + node.offsetWidth &&
|
|
||||||
bounding.bottom <=
|
|
||||||
(window.innerHeight || document.documentElement.clientHeight) + node.offsetHeight
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Matches if node element is removable
|
* @description Checks if node element is removable
|
||||||
* @param {Element} node
|
* @param {Element} node
|
||||||
* @param {boolean?} skipMatch
|
* @param {boolean?} skipMatch
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const match = (node, skipMatch) => {
|
const match = (node, skipMatch) =>
|
||||||
if (node instanceof HTMLElement) {
|
node instanceof HTMLElement &&
|
||||||
const style = window.getComputedStyle(node);
|
!node.getAttribute('data-cookie-dialog-monster') &&
|
||||||
const skipIsInViewport =
|
|
||||||
(style.animationName !== 'none' && style.animationPlayState === 'running') ||
|
|
||||||
style.display === 'none' ||
|
|
||||||
style.height === '0px' ||
|
|
||||||
style.opacity === '0' ||
|
|
||||||
style.transitionProperty !== 'none' ||
|
|
||||||
style.visibility === 'hidden';
|
|
||||||
|
|
||||||
return (
|
|
||||||
!data?.tags.includes(node.tagName?.toUpperCase?.()) &&
|
!data?.tags.includes(node.tagName?.toUpperCase?.()) &&
|
||||||
(skipIsInViewport || isInViewport(node)) &&
|
isInViewport(node) &&
|
||||||
(!!node.offsetParent || style.position === 'fixed') &&
|
(skipMatch || node.matches(data?.elements ?? []));
|
||||||
!!node.parentElement &&
|
|
||||||
(skipMatch || node.matches(data?.elements ?? []))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Fixes scroll issues
|
* @description Fixes scroll issues
|
||||||
@ -184,7 +154,7 @@ const fix = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!skips.some((skip) => skip.includes(hostname))) {
|
if (skips.indexOf(hostname) === -1) {
|
||||||
for (const item of [document.body, document.documentElement]) {
|
for (const item of [document.body, document.documentElement]) {
|
||||||
item?.classList.remove(...(data?.classes ?? []));
|
item?.classList.remove(...(data?.classes ?? []));
|
||||||
item?.style.setProperty('position', 'initial', 'important');
|
item?.style.setProperty('position', 'initial', 'important');
|
||||||
@ -207,11 +177,11 @@ const observer = new MutationObserver((mutations) => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description Fixes already existing element when page load issues
|
* @description Fixes already existing element when page load issues
|
||||||
* @listens window#load
|
* @listens window#DOMContentLoaded
|
||||||
*/
|
*/
|
||||||
|
|
||||||
window.addEventListener('load', () => {
|
window.addEventListener('DOMContentLoaded', () => {
|
||||||
if (elementCount < 2) forceClean();
|
forceClean();
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -220,7 +190,9 @@ window.addEventListener('load', () => {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
window.addEventListener('pageshow', (event) => {
|
window.addEventListener('pageshow', (event) => {
|
||||||
if (event.persisted) forceClean();
|
if (event.persisted) {
|
||||||
|
forceClean();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user