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,10 +1,5 @@
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;
@ -30,19 +25,26 @@ if (!!window.chrome) {
return null; return null;
}; };
const observe = () => {
observer.observe(document.body, {
attributes: true,
childList: true,
});
};
const removeFromCache = () => { 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();
} }
}); }
} }
}); });
}; };
@ -51,23 +53,18 @@ if (!!window.chrome) {
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,7 +72,7 @@ if (!!window.chrome) {
updateCache(match); updateCache(match);
element.remove(); element.remove();
} }
}); }
} }
}; };
@ -86,14 +83,10 @@ if (!!window.chrome) {
removeFromCache(); removeFromCache();
removeFromFilters(); removeFromFilters();
attempts += 1; attempts += 1;
observer.observe(document.body, options); observe();
}); });
}); });
const observe = () => {
observer.observe(document.body, options);
};
(async () => { (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());
@ -104,4 +97,3 @@ if (!!window.chrome) {
removeFromFilters(); removeFromFilters();
observe(); observe();
})(); })();
}