Skip to content

Commit

Permalink
feat(browser-extension): some performance improvements avoiding dupli…
Browse files Browse the repository at this point in the history
…cate checks and fake dialogs
  • Loading branch information
wanhose committed Feb 27, 2024
1 parent f84b2ab commit 415c363
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions packages/browser-extension/src/scripts/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const options = { childList: true, subtree: true };
*/
const preview = hostname.startsWith('consent.') || hostname.startsWith('myprivacy.');

/**
* @description Elements that were already seen
* @type {HTMLElement[]}
*/
const seen = [];

/**
* @description Extension state
* @type {{ enabled: boolean }}
Expand Down Expand Up @@ -117,16 +123,29 @@ function match(element, skipMatch) {
return false;
}

if (!data?.tags?.length || data.tags.includes(element.tagName?.toUpperCase?.())) {
const tagName = element.tagName?.toUpperCase?.();

if (!data?.tags?.length || data.tags.includes(tagName)) {
return false;
}

if (seen.includes(element)) {
return false;
}

seen.push(element);

if (element.hasAttributes()) {
// 2023-06-10: twitch.tv temporary fix
if (element.classList.contains('chat-line__message')) {
return false;
}

const isDialog = tagName === 'DIALOG' && element.getAttribute('open') === 'true';
const isFakeDialog = tagName === 'DIV' && element.className.includes('cmp');

return (
// 2023-06-10: twitch.tv temporary fix
!element.classList.contains('chat-line__message') &&
// ...
isInViewport(element) &&
(isDialog || isFakeDialog || isInViewport(element)) &&
(skipMatch || element.matches(data?.elements ?? []))
);
} else {
Expand Down Expand Up @@ -226,7 +245,7 @@ async function runSetup(skipReadyStateHack) {
* @type {MutationObserver}
*/
const observer = new MutationObserver((mutations) => {
const elements = mutations.map((mutation) => Array.from(mutation.addedNodes)).flat();
const elements = mutations.map((mutation) => Array.from(mutation.addedNodes)).flat(1);

fix();
if (data?.elements.length && !preview) clean(elements);
Expand Down

0 comments on commit 415c363

Please sign in to comment.