feat(scripts): improve content script performance

This commit is contained in:
wanhose 2021-11-06 14:43:17 +01:00
parent 857dcdd030
commit f024068b50

View File

@ -31,6 +31,18 @@ let selectors = "";
const target = document.body || document.documentElement; 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 * @description Fixes scroll issues
*/ */
@ -40,10 +52,21 @@ const fix = () => {
const facebook = document.getElementsByClassName("_31e")[0]; const facebook = document.getElementsByClassName("_31e")[0];
const html = document.documentElement; const html = document.documentElement;
if (body && classes.length > 0) body.classList.remove(...classes); if (body) {
if (body) body.style.setProperty("overflow-y", "unset", "important"); if (classes.length) body.classList.remove(...classes);
if (facebook) facebook.style.setProperty("position", "unset", "important"); body.style.setProperty("overflow-y", "initial", "important");
if (html) html.style.setProperty("overflow-y", "unset", "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) => { const observer = new MutationObserver((mutations, instance) => {
@ -89,6 +112,17 @@ const setupSelectors = () =>
dispatch({ type: "GET_SELECTORS" }, null, resolve); 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 * @description Setups everything and starts to observe if enabled
*/ */