feat(browser-extension): improve performance and add support for reload fix case

This commit is contained in:
wanhose 2024-09-07 10:14:41 +02:00
parent cb93096031
commit f05f0b3a14

View File

@ -88,14 +88,6 @@ let initiallyVisible = document.visibilityState === 'visible';
*/ */
const options = { childList: true, subtree: true }; const options = { childList: true, subtree: true };
/**
* @description Is consent preview page?
*/
const preview =
(hostname.startsWith('consent.') &&
!(hostname.startsWith('consent.google') || hostname.startsWith('consent.youtube'))) ||
hostname.startsWith('myprivacy.');
/** /**
* @description Elements that were already matched and are removable * @description Elements that were already matched and are removable
* @type {HTMLElement[]} * @type {HTMLElement[]}
@ -372,6 +364,10 @@ function fix() {
element?.style?.removeProperty(property); element?.style?.removeProperty(property);
break; break;
} }
case 'reload': {
window.location.reload();
break;
}
case 'reset': { case 'reset': {
const element = document.querySelector(selector); const element = document.querySelector(selector);
element?.style?.setProperty(property, 'initial', 'important'); element?.style?.setProperty(property, 'initial', 'important');
@ -431,7 +427,7 @@ function restoreDOM() {
function run(params = {}) { function run(params = {}) {
const { containers, elements, skipMatch } = params; const { containers, elements, skipMatch } = params;
if (document.body?.children.length && !preview && state.enabled && tokens.selectors.length) { if (document.body?.children.length && state.enabled && tokens.selectors.length) {
fix(); fix();
if (elements?.length) { if (elements?.length) {
@ -482,7 +478,7 @@ async function setUp(params = {}) {
* @type {MutationObserver} * @type {MutationObserver}
*/ */
const observer = new MutationObserver((mutations) => { const observer = new MutationObserver((mutations) => {
if (preview || !state.enabled || !tokens.selectors.length) { if (!state.enabled || !tokens.selectors.length) {
return; return;
} }
@ -505,11 +501,7 @@ browser.runtime.onMessage.addListener(async (message) => {
} }
case 'RUN': { case 'RUN': {
await setUp({ skipRunFn: true }); await setUp({ skipRunFn: true });
run( run({ elements: removables, skipMatch: true });
removables.length
? { elements: removables, skipMatch: true }
: { containers: tokens.containers }
);
break; break;
} }
} }
@ -521,7 +513,7 @@ browser.runtime.onMessage.addListener(async (message) => {
* @listens window#DOMContentLoaded * @listens window#DOMContentLoaded
* @returns {void} * @returns {void}
*/ */
window.addEventListener('DOMContentLoaded', async () => { document.addEventListener('DOMContentLoaded', async () => {
if (document.visibilityState === 'visible') { if (document.visibilityState === 'visible') {
await setUp(); await setUp();
} }
@ -535,7 +527,6 @@ 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();
run({ containers: tokens.containers });
} }
}); });