From b277250dfcb53a0580f486a7a65890d5417880b8 Mon Sep 17 00:00:00 2001 From: wanhose Date: Wed, 24 Mar 2021 09:52:35 +0100 Subject: [PATCH] feat(content): improve script --- src/scripts/content.js | 156 +++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 82 deletions(-) diff --git a/src/scripts/content.js b/src/scripts/content.js index 45a94b7..9498679 100644 --- a/src/scripts/content.js +++ b/src/scripts/content.js @@ -1,107 +1,99 @@ -if (!!window.chrome) { - let attempts = 0; - let filters = []; - const options = { - attributes: true, - childList: true, - }; +let attempts = 0; +let filters = []; - const fix = () => { - const body = document.body.style; - const html = document.documentElement.style; +const fix = () => { + const body = document.body.style; + const html = document.documentElement.style; - body.setProperty("overflow-y", "unset", "important"); - html.setProperty("overflow-y", "unset", "important"); - }; + body.setProperty("overflow-y", "unset", "important"); + html.setProperty("overflow-y", "unset", "important"); +}; - const retrieveElement = (match) => { - if (!match.includes("[") && !match.includes(">")) { - if (match.startsWith(".")) { - return document.getElementsByClassName(match.slice(1))[0]; - } - - if (match.startsWith("#")) { - return document.getElementById(match.slice(1)); - } - } else { - return document.querySelector(match); +const retrieveElement = (match) => { + if (!match.includes("[") && !match.includes(">")) { + if (match.startsWith(".")) { + return document.getElementsByClassName(match.slice(1))[0]; } - return null; - }; + if (match.startsWith("#")) { + return document.getElementById(match.slice(1)); + } + } else { + return document.querySelector(match); + } - const removeFromCache = () => { - chrome.storage.local.get([document.location.hostname], (value) => { - const matches = value[document.location.hostname]; + return null; +}; - if (matches && !!matches.length) { - matches.forEach((match) => { - const element = retrieveElement(match); - const tagName = element ? element.tagName.toUpperCase() : ""; +const observe = () => { + observer.observe(document.body, { + attributes: true, + childList: true, + }); +}; - if (element && !["BODY", "HTML"].includes(tagName)) { - element.remove(); - } - }); - } - }); - }; +const removeFromCache = () => { + chrome.storage.local.get([document.location.hostname], (value) => { + const matches = value[document.location.hostname]; - const updateCache = (value) => { - chrome.storage.local.get([document.location.hostname], (store) => { - const matches = store[document.location.hostname]; - - if (!!matches) { - if (!matches.includes(value)) { - chrome.storage.local.set({ - [document.location.hostname]: [...new Set([...matches, value])], - }); - } - } else { - chrome.storage.local.set({ - [document.location.hostname]: [value], - }); - } - }); - }; - - const removeFromFilters = () => { - if (attempts < 5) { - filters.forEach((match) => { - const element = retrieveElement(match); + if (matches && !!matches.length) { + for (let i = 0; i < matches.length; i++) { + const element = retrieveElement(matches[i]); 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 updateCache = (value) => { + chrome.storage.local.get([document.location.hostname], (store) => { + const matches = store[document.location.hostname]; + + chrome.storage.local.set({ + [document.location.hostname]: matches + ? [...new Set([...matches, value])] + : [value], }); }); +}; - const observe = () => { - observer.observe(document.body, options); - }; +const removeFromFilters = () => { + if (attempts < 5) { + for (let i = 0; i < filters.length; i++) { + const match = filters[i]; + const element = retrieveElement(match); + const tagName = element ? element.tagName.toUpperCase() : ""; - (async () => { - const url = chrome.runtime.getURL("filters/index.txt"); - const db = await fetch(url).then((res) => res.text()); - filters = db.split("\n"); + 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; observe(); - })(); -} + }); +}); + +(async () => { + const url = chrome.runtime.getURL("filters/index.txt"); + const db = await fetch(url).then((res) => res.text()); + filters = db.split("\n"); + + fix(); + removeFromCache(); + removeFromFilters(); + observe(); +})();