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