From f024068b507f99cf5c812beba35a436f7fce487a Mon Sep 17 00:00:00 2001 From: wanhose Date: Sat, 6 Nov 2021 14:43:17 +0100 Subject: [PATCH] feat(scripts): improve content script performance --- scripts/content.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/scripts/content.js b/scripts/content.js index 2a5c0ef..277617d 100644 --- a/scripts/content.js +++ b/scripts/content.js @@ -31,6 +31,18 @@ let selectors = ""; const target = document.body || document.documentElement; +/** + * @description Cleans DOM + */ + +const clean = () => { + if (selectors.length) { + const results = document.querySelectorAll(selectors); + + for (let i = results.length; i--; ) results[i].outerHTML = ""; + } +}; + /** * @description Fixes scroll issues */ @@ -40,10 +52,21 @@ const fix = () => { const facebook = document.getElementsByClassName("_31e")[0]; const html = document.documentElement; - if (body && classes.length > 0) body.classList.remove(...classes); - if (body) body.style.setProperty("overflow-y", "unset", "important"); - if (facebook) facebook.style.setProperty("position", "unset", "important"); - if (html) html.style.setProperty("overflow-y", "unset", "important"); + if (body) { + if (classes.length) body.classList.remove(...classes); + body.style.setProperty("overflow-y", "initial", "important"); + body.style.setProperty("position", "initial", "important"); + } + + if (facebook) { + facebook.style.setProperty("position", "initial", "important"); + } + + if (html) { + if (classes.length) html.classList.remove(...classes); + html.style.setProperty("position", "initial", "important"); + html.style.setProperty("overflow-y", "initial", "important"); + } }; const observer = new MutationObserver((mutations, instance) => { @@ -89,6 +112,17 @@ const setupSelectors = () => dispatch({ type: "GET_SELECTORS" }, null, resolve); }); +/** + * @description Listens DOM complete state + */ + +document.addEventListener("readystatechange", () => { + if (document.readyState === "complete") { + clean(); + setTimeout(clean, 2000); + } +}); + /** * @description Setups everything and starts to observe if enabled */