fix(content): use local storage instead of sync storage and performance improvements

This commit is contained in:
Juan José Vílchez 2021-01-27 21:53:57 +01:00
parent 607a73a589
commit c84762c259

View File

@ -1,31 +1,38 @@
if (!!window.chrome) {
let attempts = 0;
const filtersUrl = chrome.runtime.getURL("filters/index.txt");
const options = {
attributes: true,
childList: true,
};
const fix = () => {
document.body.style = "overflow-y: unset !important;";
};
const observe = () => {
observer.observe(document.body, {
attributes: true,
childList: true,
});
const retrieveElement = (match) => {
if (!match.includes("[") && !match.includes(">")) {
if (match[0] === ".") {
return document.getElementsByClassName(match.slice(1))[0];
}
if (match[0] === "#") {
return document.getElementById(match.slice(1));
}
} else {
return document.querySelector(match);
}
return null;
};
const observer = new MutationObserver((mutations, observer) => {
mutations.forEach(async () => {
observer.disconnect();
fix();
await remove();
observe();
});
});
const removeFromCache = () => {
chrome.storage.sync.get([document.location.hostname], (value) => {
chrome.storage.local.get([document.location.hostname], (value) => {
const matches = value[document.location.hostname];
if (matches && !!matches.length) {
matches.forEach((match) => {
const element = document.querySelector(match);
const element = retrieveElement(match);
const tagName = element ? element.tagName.toUpperCase() : "";
if (element && !["BODY", "HTML"].includes(tagName)) {
@ -37,33 +44,56 @@ if (!!window.chrome) {
});
};
const saveToCache = (value) => {
chrome.storage.sync.set({ [document.location.hostname]: value });
};
const updateCache = (value) => {
chrome.storage.local.get([document.location.hostname], (store) => {
const matches = store[document.location.hostname];
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();
if (matches && !!matches.length && !matches.includes(value)) {
chrome.storage.local.set({
[document.location.hostname]: [...new Set([...matches, value])],
});
} else {
chrome.storage.local.set({
[document.location.hostname]: [value],
});
}
});
saveToCache(matches);
};
(async () => {
fix();
removeFromCache();
await remove();
observe();
})();
const removeFromFilters = async () => {
if (attempts < 3) {
const text = await fetch(filtersUrl).then((res) => res.text());
const filters = text.split("\n");
filters.forEach((match) => {
const element = retrieveElement(match);
const tagName = element ? element.tagName.toUpperCase() : "";
if (element && !["BODY", "HTML"].includes(tagName)) {
updateCache(match);
element.remove();
}
});
}
};
const observer = new MutationObserver((mutations, observer) => {
mutations.forEach(() => {
observer.disconnect();
fix();
removeFromCache();
removeFromFilters();
attempts += 1;
observer.observe(document.body, options);
});
});
const observe = () => {
observer.observe(document.body, options);
};
fix();
removeFromCache();
removeFromFilters();
observe();
}