feat(content): improve script

This commit is contained in:
wanhose 2021-03-24 09:52:35 +01:00
parent 2df5530dea
commit b277250dfc

View File

@ -1,20 +1,15 @@
if (!!window.chrome) { let attempts = 0;
let attempts = 0; let filters = [];
let filters = [];
const options = {
attributes: true,
childList: true,
};
const fix = () => { const fix = () => {
const body = document.body.style; const body = document.body.style;
const html = document.documentElement.style; const html = document.documentElement.style;
body.setProperty("overflow-y", "unset", "important"); body.setProperty("overflow-y", "unset", "important");
html.setProperty("overflow-y", "unset", "important"); html.setProperty("overflow-y", "unset", "important");
}; };
const retrieveElement = (match) => { const retrieveElement = (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];
@ -28,46 +23,48 @@ if (!!window.chrome) {
} }
return null; return null;
}; };
const removeFromCache = () => { const observe = () => {
observer.observe(document.body, {
attributes: true,
childList: true,
});
};
const removeFromCache = () => {
chrome.storage.local.get([document.location.hostname], (value) => { chrome.storage.local.get([document.location.hostname], (value) => {
const matches = value[document.location.hostname]; const matches = value[document.location.hostname];
if (matches && !!matches.length) { if (matches && !!matches.length) {
matches.forEach((match) => { for (let i = 0; i < matches.length; i++) {
const element = retrieveElement(match); const element = retrieveElement(matches[i]);
const tagName = element ? element.tagName.toUpperCase() : ""; const tagName = element ? element.tagName.toUpperCase() : "";
if (element && !["BODY", "HTML"].includes(tagName)) { if (element && !["BODY", "HTML"].includes(tagName)) {
element.remove(); element.remove();
} }
}); }
} }
}); });
}; };
const updateCache = (value) => { const updateCache = (value) => {
chrome.storage.local.get([document.location.hostname], (store) => { chrome.storage.local.get([document.location.hostname], (store) => {
const matches = store[document.location.hostname]; const matches = store[document.location.hostname];
if (!!matches) {
if (!matches.includes(value)) {
chrome.storage.local.set({ chrome.storage.local.set({
[document.location.hostname]: [...new Set([...matches, value])], [document.location.hostname]: matches
? [...new Set([...matches, value])]
: [value],
}); });
}
} else {
chrome.storage.local.set({
[document.location.hostname]: [value],
}); });
} };
});
};
const removeFromFilters = () => { const removeFromFilters = () => {
if (attempts < 5) { if (attempts < 5) {
filters.forEach((match) => { for (let i = 0; i < filters.length; i++) {
const match = filters[i];
const element = retrieveElement(match); const element = retrieveElement(match);
const tagName = element ? element.tagName.toUpperCase() : ""; const tagName = element ? element.tagName.toUpperCase() : "";
@ -75,26 +72,22 @@ if (!!window.chrome) {
updateCache(match); updateCache(match);
element.remove(); element.remove();
} }
});
} }
}; }
};
const observer = new MutationObserver((mutations, observer) => { const observer = new MutationObserver((mutations, observer) => {
mutations.forEach(() => { mutations.forEach(() => {
observer.disconnect(); observer.disconnect();
fix(); fix();
removeFromCache(); removeFromCache();
removeFromFilters(); removeFromFilters();
attempts += 1; attempts += 1;
observer.observe(document.body, options); observe();
});
}); });
});
const observe = () => { (async () => {
observer.observe(document.body, options);
};
(async () => {
const url = chrome.runtime.getURL("filters/index.txt"); const url = chrome.runtime.getURL("filters/index.txt");
const db = await fetch(url).then((res) => res.text()); const db = await fetch(url).then((res) => res.text());
filters = db.split("\n"); filters = db.split("\n");
@ -103,5 +96,4 @@ if (!!window.chrome) {
removeFromCache(); removeFromCache();
removeFromFilters(); removeFromFilters();
observe(); observe();
})(); })();
}