feat(browser-extension): try chunk approach for some issues

This commit is contained in:
wanhose 2024-08-03 08:33:29 +02:00
parent 83198c3b4f
commit a89ed2cace

View File

@ -85,25 +85,43 @@ const triggerEventName = 'cookie-dialog-monster';
* @returns {void} * @returns {void}
*/ */
function clean(elements, skipMatch) { function clean(elements, skipMatch) {
for (const element of elements) { let index = 0;
if (match(element, skipMatch)) { const size = 50;
const observer = new MutationObserver(forceElementStyles);
const options = { attributes: true, attributeFilter: [dataAttributeName, 'class', 'style'] };
element.setAttribute(dataAttributeName, 'true'); function chunk() {
element.style.setProperty('display', 'none', 'important'); const end = Math.min(index + size, elements.length);
observer.observe(element, options);
count += 1; for (; index < end; index++) {
dispatch({ type: 'SET_BADGE', value: `${count}` }); const element = elements[index];
if (!removables.includes(element)) { if (match(element, skipMatch)) {
removables.push(element); const observer = new MutationObserver(forceElementStyles);
element.setAttribute(dataAttributeName, 'true');
element.style.setProperty('display', 'none', 'important');
observer.observe(element, {
attributes: true,
attributeFilter: [dataAttributeName, 'class', 'style'],
});
count += 1;
dispatch({ type: 'SET_BADGE', value: `${count}` });
if (!removables.includes(element)) {
removables.push(element);
}
} }
seen.push(element);
} }
seen.push(element); if (index < elements.length) {
requestAnimationFrame(chunk);
}
} }
requestAnimationFrame(chunk);
} }
/** /**
@ -362,7 +380,7 @@ const observer = new MutationObserver((mutations) => {
const elements = mutations.flatMap((mutation) => Array.from(mutation.addedNodes)); const elements = mutations.flatMap((mutation) => Array.from(mutation.addedNodes));
window.dispatchEvent(new Event(triggerEventName, { detail: { elements } })); window.dispatchEvent(new CustomEvent(triggerEventName, { detail: { elements } }));
}); });
/** /**
@ -394,7 +412,7 @@ browser.runtime.onMessage.addListener(async (message) => {
window.addEventListener('DOMContentLoaded', async () => { window.addEventListener('DOMContentLoaded', async () => {
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible') {
await setup(); await setup();
window.dispatchEvent(new Event(triggerEventName)); window.dispatchEvent(new CustomEvent(triggerEventName));
} }
}); });
@ -406,7 +424,7 @@ window.addEventListener('DOMContentLoaded', async () => {
window.addEventListener('pageshow', async (event) => { window.addEventListener('pageshow', async (event) => {
if (document.visibilityState === 'visible' && event.persisted) { if (document.visibilityState === 'visible' && event.persisted) {
await setup(); await setup();
window.dispatchEvent(new Event(triggerEventName)); window.dispatchEvent(new CustomEvent(triggerEventName));
} }
}); });
@ -442,6 +460,6 @@ window.addEventListener('visibilitychange', async () => {
if (document.visibilityState === 'visible' && !initiallyVisible) { if (document.visibilityState === 'visible' && !initiallyVisible) {
initiallyVisible = true; initiallyVisible = true;
await setup(); await setup();
window.dispatchEvent(new Event(triggerEventName)); window.dispatchEvent(new CustomEvent(triggerEventName));
} }
}); });