feat(scripts): minor content script changes

This commit is contained in:
wanhose 2021-06-23 13:26:00 +02:00
parent 61cb55fa39
commit b0966d62dd

View File

@ -15,12 +15,12 @@ let attempts = 1;
let intervalId = 0; let intervalId = 0;
/** /**
* @var ready * @var loading
* @description Is extension ready? * @description Is extension loading?
* @type {boolean} * @type {boolean}
*/ */
let ready = false; let loading = true;
/** /**
* @var selectorsFromCache * @var selectorsFromCache
@ -51,7 +51,7 @@ const fix = () => {
if (body) body.style.setProperty("overflow-y", "unset", "important"); if (body) body.style.setProperty("overflow-y", "unset", "important");
if (facebook) facebook.style.setProperty("position", "unset", "important"); if (facebook) facebook.style.setProperty("position", "unset", "important");
if (html) html.style.setProperty("overflow-y", "unset", "important"); if (html) html.style.setProperty("overflow-y", "unset", "important");
if (ready) html.style.setProperty("opacity", "1", "important"); if (!loading) html.style.setProperty("opacity", "1", "important");
}; };
/** /**
@ -93,7 +93,7 @@ const removeFromCache = () => {
if (!["BODY", "HTML"].includes(tagName)) { if (!["BODY", "HTML"].includes(tagName)) {
element.remove(); element.remove();
ready = true; loading = false;
} }
} }
} }
@ -114,10 +114,10 @@ const removeFromNetwork = () => {
if (!["BODY", "HTML"].includes(tagName)) { if (!["BODY", "HTML"].includes(tagName)) {
element.remove(); element.remove();
ready = true; loading = false;
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
hostname: document.location.hostname, hostname: document.location.hostname,
selector, state: { matches: [selector] },
type: "UPDATE_CACHE", type: "UPDATE_CACHE",
}); });
} }
@ -131,13 +131,15 @@ const removeFromNetwork = () => {
*/ */
const runTasks = () => { const runTasks = () => {
if (attempts >= 5 || selectorsFromCache.length === 0) ready = true; if (attempts >= 5 || selectorsFromCache.length === 0) {
loading = false;
}
if (attempts <= 20) { if (attempts <= 20) {
fix(); fix();
removeFromCache(); removeFromCache();
if (attempts <= 5) removeFromNetwork(); if (attempts <= 5) removeFromNetwork();
attempts += 1; if (document.readyState !== "loading") attempts += 1;
} }
if (attempts > 20) { if (attempts > 20) {
@ -152,18 +154,17 @@ const runTasks = () => {
chrome.runtime.sendMessage( chrome.runtime.sendMessage(
{ hostname: document.location.hostname, type: "GET_CACHE" }, { hostname: document.location.hostname, type: "GET_CACHE" },
null, null,
async (cacheResponse) => { async ({ enabled, matches }) => {
console.log(cacheResponse);
chrome.runtime.sendMessage({ type: "ENABLE_POPUP" }); chrome.runtime.sendMessage({ type: "ENABLE_POPUP" });
if (cacheResponse.enabled) { if (enabled) {
selectorsFromCache = cacheResponse.matches; selectorsFromCache = matches;
chrome.runtime.sendMessage({ type: "ENABLE_ICON" }); chrome.runtime.sendMessage({ type: "ENABLE_ICON" });
chrome.runtime.sendMessage( chrome.runtime.sendMessage(
{ type: "GET_LIST" }, { type: "GET_LIST" },
null, null,
async (networkResponse) => { async ({ selectors }) => {
selectorsFromNetwork = networkResponse.matches; selectorsFromNetwork = selectors;
intervalId = setInterval(runTasks, 500); intervalId = setInterval(runTasks, 500);
} }
); );