From 33c579d0c1f13be86d80a98a4d2787d9e9722862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jose=CC=81=20Vi=CC=81lchez?= Date: Wed, 27 Jan 2021 20:28:31 +0100 Subject: [PATCH] feat(content): implement cache --- src/scripts/content.js | 44 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/scripts/content.js b/src/scripts/content.js index 67446bd..1975c1f 100644 --- a/src/scripts/content.js +++ b/src/scripts/content.js @@ -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) => { - const element = document.querySelector(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") { - element.remove(); + 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(); })();