Skip to content

Commit

Permalink
feat(browser-extension): improve content script
Browse files Browse the repository at this point in the history
  • Loading branch information
wanhose committed Aug 2, 2022
1 parent 9f993f5 commit fb85ac7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/browser-extension/src/scripts/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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)];
Expand Down

0 comments on commit fb85ac7

Please sign in to comment.