feat(browser-extension): improve content script

This commit is contained in:
wanhose 2022-08-02 21:03:43 +02:00
parent 9f993f5399
commit fb85ac7865

View File

@ -43,15 +43,25 @@ const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivac
* @returns {boolean}
*/
const match = (node) =>
node instanceof HTMLElement &&
node.parentElement &&
!forbiddenTags.includes(node.tagName?.toUpperCase?.()) &&
node.matches(data?.elements ?? []);
const match = (node) => {
if (!(node instanceof HTMLElement)) return false;
const rect = node.getBoundingClientRect();
const isFullscreen = rect.bottom + rect.top > 0 && rect.bottom - rect.top === 0;
const isVisible = rect.top <= (window.innerHeight || document.documentElement.clientHeight);
return (
!forbiddenTags.includes(node.tagName?.toUpperCase?.()) &&
(isFullscreen || isVisible) &&
(node.offsetParent || window.getComputedStyle(node).position === 'fixed') &&
node.parentElement &&
node.matches(data?.elements ?? [])
);
};
/**
* @description Cleans DOM
* @param {HTMLElement[]} nodes
* @param {Element[]} nodes
* @param {boolean?} skipMatch
* @returns {void}
*/
@ -126,7 +136,7 @@ const observer = new MutationObserver((mutations) => {
window.addEventListener('pageshow', async (event) => {
if (event.persisted) {
const state = dispatch({ hostname, type: 'GET_STATE' });
const state = await dispatch({ hostname, type: 'GET_STATE' });
if (data?.elements?.length && state?.enabled && !preview) {
const nodes = [...document.querySelectorAll(data.elements)];