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];
const remove = async () => { if (matches && !!matches.length && !matches.includes(value)) {
const filtersUrl = chrome.runtime.getURL("filters/index.txt"); chrome.storage.local.set({
const text = await fetch(filtersUrl).then((res) => res.text()); [document.location.hostname]: [...new Set([...matches, value])],
const filters = text.split("\n"); });
const matches = []; } else {
chrome.storage.local.set({
filters.forEach((match) => { [document.location.hostname]: [value],
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 () => { const removeFromFilters = async () => {
fix(); if (attempts < 3) {
removeFromCache(); const text = await fetch(filtersUrl).then((res) => res.text());
await remove(); const filters = text.split("\n");
observe();
})(); 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();
} }