feat(content): implement cache

This commit is contained in:
Juan José Vílchez 2021-01-27 20:28:31 +01:00
parent d93e6a9351
commit 33c579d0c1

View File

@ -19,22 +19,50 @@ if (!!window.chrome) {
});
});
const remove = async () => {
const filtersUrl = chrome.runtime.getURL("filters/index.txt");
const text = await fetch(filtersUrl).then((res) => res.text());
const filters = text.split("\n");
const removeFromCache = () => {
chrome.storage.sync.get([document.location.hostname], (value) => {
const matches = value[document.location.hostname];
filters.forEach((match) => {
if (matches && !!matches.length) {
matches.forEach((match) => {
const element = document.querySelector(match);
const tagName = element ? element.tagName.toUpperCase() : "";
if (element && element.tagName !== "BODY" && element.tagName !== "HTML") {
if (element && !["BODY", "HTML"].includes(tagName)) {
matches.push(match);
element.remove();
}
});
}
});
};
const saveToCache = (value) => {
chrome.storage.sync.set({ [document.location.hostname]: value });
};
const remove = async () => {
const filtersUrl = chrome.runtime.getURL("filters/index.txt");
const text = await fetch(filtersUrl).then((res) => res.text());
const filters = text.split("\n");
const matches = [];
filters.forEach((match) => {
const element = document.querySelector(match);
const tagName = element ? element.tagName.toUpperCase() : "";
if (element && !["BODY", "HTML"].includes(tagName)) {
matches.push(match);
element.remove();
}
});
saveToCache(matches);
};
(async () => {
fix();
removeFromCache();
await remove();
observe();
})();