feat(content): improve script
This commit is contained in:
parent
2df5530dea
commit
b277250dfc
@ -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();
|
||||
})();
|
||||
|
Loading…
Reference in New Issue
Block a user