feat(scripts): improve content scripts
This commit is contained in:
parent
8702d7746c
commit
ca1b32e0c3
@ -1,3 +1,4 @@
|
|||||||
|
let attempts = 1;
|
||||||
let elements = [];
|
let elements = [];
|
||||||
|
|
||||||
const fix = () => {
|
const fix = () => {
|
||||||
@ -8,7 +9,14 @@ const fix = () => {
|
|||||||
body.style.setProperty("overflow-y", "unset", "important");
|
body.style.setProperty("overflow-y", "unset", "important");
|
||||||
};
|
};
|
||||||
|
|
||||||
const retrieveElement = (match) => {
|
const observe = () => {
|
||||||
|
observer.observe(document.body, {
|
||||||
|
attributes: true,
|
||||||
|
childList: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const search = (match) => {
|
||||||
if (!match.includes("[") && !match.includes(">")) {
|
if (!match.includes("[") && !match.includes(">")) {
|
||||||
if (match.startsWith(".")) {
|
if (match.startsWith(".")) {
|
||||||
return document.getElementsByClassName(match.slice(1))[0];
|
return document.getElementsByClassName(match.slice(1))[0];
|
||||||
@ -24,40 +32,37 @@ const retrieveElement = (match) => {
|
|||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const observe = () => {
|
|
||||||
observer.observe(document.body, {
|
|
||||||
attributes: true,
|
|
||||||
childList: true,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const remove = () => {
|
const remove = () => {
|
||||||
for (let i = elements.length; i--; ) {
|
for (let i = elements.length; i--; ) {
|
||||||
const match = elements[i];
|
const match = elements[i];
|
||||||
const element = retrieveElement(match);
|
const element = search(match);
|
||||||
const tagName = element ? element.tagName.toUpperCase() : "";
|
|
||||||
|
|
||||||
if (element && !["BODY", "HTML"].includes(tagName)) {
|
if (element) {
|
||||||
element.remove();
|
const tagName = element.tagName.toUpperCase();
|
||||||
|
|
||||||
|
if (!["BODY", "HTML"].includes(tagName)) element.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const observer = new MutationObserver((_, instance) => {
|
const observer = new MutationObserver((_, instance) => {
|
||||||
|
if (attempts <= 5) {
|
||||||
|
attempts += 1;
|
||||||
instance.disconnect();
|
instance.disconnect();
|
||||||
fix();
|
fix();
|
||||||
remove();
|
remove();
|
||||||
observe();
|
observe();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const url = chrome.runtime.getURL("elements/index.txt");
|
const url = chrome.runtime.getURL("data/elements.txt");
|
||||||
const db = await fetch(url).then((res) => res.text());
|
const db = await fetch(url).then((res) => res.text());
|
||||||
elements = db.split("\n");
|
elements = db.split("\n");
|
||||||
|
})();
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
fix();
|
fix();
|
||||||
remove();
|
remove();
|
||||||
observe();
|
observe();
|
||||||
});
|
});
|
||||||
})();
|
|
||||||
|
Loading…
Reference in New Issue
Block a user