feat(content): improve script
This commit is contained in:
parent
2df5530dea
commit
b277250dfc
@ -1,107 +1,99 @@
|
|||||||
if (!!window.chrome) {
|
let attempts = 0;
|
||||||
let attempts = 0;
|
let filters = [];
|
||||||
let filters = [];
|
|
||||||
const options = {
|
|
||||||
attributes: true,
|
|
||||||
childList: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
const fix = () => {
|
const fix = () => {
|
||||||
const body = document.body.style;
|
const body = document.body.style;
|
||||||
const html = document.documentElement.style;
|
const html = document.documentElement.style;
|
||||||
|
|
||||||
body.setProperty("overflow-y", "unset", "important");
|
body.setProperty("overflow-y", "unset", "important");
|
||||||
html.setProperty("overflow-y", "unset", "important");
|
html.setProperty("overflow-y", "unset", "important");
|
||||||
};
|
};
|
||||||
|
|
||||||
const retrieveElement = (match) => {
|
const retrieveElement = (match) => {
|
||||||
if (!match.includes("[") && !match.includes(">")) {
|
if (!match.includes("[") && !match.includes(">")) {
|
||||||
if (match.startsWith(".")) {
|
if (match.startsWith(".")) {
|
||||||
return document.getElementsByClassName(match.slice(1))[0];
|
return document.getElementsByClassName(match.slice(1))[0];
|
||||||
}
|
|
||||||
|
|
||||||
if (match.startsWith("#")) {
|
|
||||||
return document.getElementById(match.slice(1));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return document.querySelector(match);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
if (match.startsWith("#")) {
|
||||||
};
|
return document.getElementById(match.slice(1));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return document.querySelector(match);
|
||||||
|
}
|
||||||
|
|
||||||
const removeFromCache = () => {
|
return null;
|
||||||
chrome.storage.local.get([document.location.hostname], (value) => {
|
};
|
||||||
const matches = value[document.location.hostname];
|
|
||||||
|
|
||||||
if (matches && !!matches.length) {
|
const observe = () => {
|
||||||
matches.forEach((match) => {
|
observer.observe(document.body, {
|
||||||
const element = retrieveElement(match);
|
attributes: true,
|
||||||
const tagName = element ? element.tagName.toUpperCase() : "";
|
childList: true,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
if (element && !["BODY", "HTML"].includes(tagName)) {
|
const removeFromCache = () => {
|
||||||
element.remove();
|
chrome.storage.local.get([document.location.hostname], (value) => {
|
||||||
}
|
const matches = value[document.location.hostname];
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const updateCache = (value) => {
|
if (matches && !!matches.length) {
|
||||||
chrome.storage.local.get([document.location.hostname], (store) => {
|
for (let i = 0; i < matches.length; i++) {
|
||||||
const matches = store[document.location.hostname];
|
const element = retrieveElement(matches[i]);
|
||||||
|
|
||||||
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);
|
|
||||||
const tagName = element ? element.tagName.toUpperCase() : "";
|
const tagName = element ? element.tagName.toUpperCase() : "";
|
||||||
|
|
||||||
if (element && !["BODY", "HTML"].includes(tagName)) {
|
if (element && !["BODY", "HTML"].includes(tagName)) {
|
||||||
updateCache(match);
|
|
||||||
element.remove();
|
element.remove();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const observer = new MutationObserver((mutations, observer) => {
|
const updateCache = (value) => {
|
||||||
mutations.forEach(() => {
|
chrome.storage.local.get([document.location.hostname], (store) => {
|
||||||
observer.disconnect();
|
const matches = store[document.location.hostname];
|
||||||
fix();
|
|
||||||
removeFromCache();
|
chrome.storage.local.set({
|
||||||
removeFromFilters();
|
[document.location.hostname]: matches
|
||||||
attempts += 1;
|
? [...new Set([...matches, value])]
|
||||||
observer.observe(document.body, options);
|
: [value],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const observe = () => {
|
const removeFromFilters = () => {
|
||||||
observer.observe(document.body, options);
|
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 () => {
|
if (element && !["BODY", "HTML"].includes(tagName)) {
|
||||||
const url = chrome.runtime.getURL("filters/index.txt");
|
updateCache(match);
|
||||||
const db = await fetch(url).then((res) => res.text());
|
element.remove();
|
||||||
filters = db.split("\n");
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const observer = new MutationObserver((mutations, observer) => {
|
||||||
|
mutations.forEach(() => {
|
||||||
|
observer.disconnect();
|
||||||
fix();
|
fix();
|
||||||
removeFromCache();
|
removeFromCache();
|
||||||
removeFromFilters();
|
removeFromFilters();
|
||||||
|
attempts += 1;
|
||||||
observe();
|
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();
|
||||||
|
})();
|
||||||
|
Loading…
Reference in New Issue
Block a user