2021-04-08 21:50:24 +00:00
|
|
|
/**
|
|
|
|
* @description Number of attempts
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
|
|
|
|
let attempts = 1;
|
|
|
|
|
2021-06-27 11:17:53 +00:00
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Array of selectors
|
|
|
|
* @type {string[]}
|
2021-06-27 11:17:53 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
let classesFromNetwork = [];
|
2021-06-27 11:17:53 +00:00
|
|
|
|
2021-04-08 21:50:24 +00:00
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Shortcut to send messages to background script
|
|
|
|
* @type {void}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
const dispatch = chrome.runtime.sendMessage;
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
/**
|
2021-06-22 22:01:56 +00:00
|
|
|
* @description Array of selectors
|
|
|
|
* @type {string[]}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
let selectorsFromCache = [];
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
/**
|
2021-06-22 22:01:56 +00:00
|
|
|
* @description Array of selectors
|
2021-10-05 07:00:07 +00:00
|
|
|
* @type {Promise<string[]>[]}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-22 22:01:56 +00:00
|
|
|
let selectorsFromNetwork = [];
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Split large arrays into promises
|
|
|
|
* @param {string[]} array
|
|
|
|
*/
|
|
|
|
|
|
|
|
const chunkerize = (array) =>
|
|
|
|
[...Array(Math.ceil(array.length / 300))].map(
|
|
|
|
(_, index) => () =>
|
|
|
|
new Promise((resolve) => {
|
|
|
|
removeElements(array.slice(index * 300, (index + 1) * 300), true);
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Fixes scroll issues
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const fix = () => {
|
|
|
|
const body = document.body;
|
2021-10-05 07:00:07 +00:00
|
|
|
const classes = classesFromNetwork;
|
2021-06-27 11:17:53 +00:00
|
|
|
const facebook = document.getElementsByClassName("_31e")[0];
|
2021-07-29 08:55:19 +00:00
|
|
|
const html = document.documentElement;
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
if (body && classes.length > 0) body.classList.remove(...classes);
|
2021-06-22 22:01:56 +00:00
|
|
|
if (body) body.style.setProperty("overflow-y", "unset", "important");
|
|
|
|
if (facebook) facebook.style.setProperty("position", "unset", "important");
|
|
|
|
if (html) html.style.setProperty("overflow-y", "unset", "important");
|
2021-04-08 21:50:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @function removeElements
|
|
|
|
* @description Removes matched elements from a selectors array
|
|
|
|
* @param {string[]} selectors
|
|
|
|
* @param {boolean} updateCache
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
const removeElements = (selectors, updateCache) => {
|
|
|
|
for (let i = selectors.length; i--; ) {
|
|
|
|
const selector = selectors[i];
|
|
|
|
const element = search(selector);
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
if (element) {
|
|
|
|
const tagName = element.tagName.toUpperCase();
|
|
|
|
|
|
|
|
if (!["BODY", "HTML"].includes(tagName)) {
|
|
|
|
element.remove();
|
|
|
|
|
|
|
|
if (updateCache) {
|
|
|
|
selectorsFromCache = [...selectorsFromCache, selector];
|
|
|
|
dispatch({
|
|
|
|
hostname: document.location.hostname,
|
|
|
|
state: { matches: [selector] },
|
|
|
|
type: "UPDATE_CACHE",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @function runTasks
|
|
|
|
* @description Starts running tasks
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
const runTasks = async () => {
|
|
|
|
if (attempts <= 20) {
|
|
|
|
fix();
|
|
|
|
removeElements(selectorsFromCache);
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
if (selectorsFromNetwork.length > 0) {
|
|
|
|
const selectors = selectorsFromNetwork;
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
if (attempts <= 5) await Promise.all(selectors.map((fn) => fn()));
|
|
|
|
if (document.readyState === "complete") attempts += 1;
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
2021-06-22 22:01:56 +00:00
|
|
|
}
|
2021-04-08 21:50:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @function search
|
|
|
|
* @description Retrieves HTML element if selector exists
|
|
|
|
*
|
|
|
|
* @param {string} selector
|
|
|
|
* @returns {HTMLElement | null} An HTML element or null
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
const search = (selector) => {
|
|
|
|
if (!selector.includes("[") && !selector.includes(">")) {
|
|
|
|
if (selector.startsWith(".")) {
|
|
|
|
return document.getElementsByClassName(selector.slice(1))[0];
|
|
|
|
}
|
2021-04-08 21:50:24 +00:00
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
if (selector.startsWith("#")) {
|
|
|
|
return document.getElementById(selector.slice(1));
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
2021-10-05 07:00:07 +00:00
|
|
|
} else {
|
|
|
|
return document.querySelector(selector);
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
2021-10-05 07:00:07 +00:00
|
|
|
|
|
|
|
return null;
|
2021-04-08 21:50:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Setups classes selectors
|
|
|
|
* @type {Promise<boolean>}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
const setupClasses = new Promise((resolve) => {
|
|
|
|
dispatch({ type: "GET_CLASSES" }, null, ({ classes }) => {
|
|
|
|
classesFromNetwork = classes;
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
});
|
2021-04-08 21:50:24 +00:00
|
|
|
|
|
|
|
/**
|
2021-10-05 07:00:07 +00:00
|
|
|
* @description Setups elements selectors
|
|
|
|
* @type {Promise<boolean>}
|
2021-04-08 21:50:24 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-05 07:00:07 +00:00
|
|
|
const setupSelectors = new Promise((resolve) => {
|
|
|
|
dispatch({ type: "GET_SELECTORS" }, null, ({ selectors }) => {
|
|
|
|
selectorsFromNetwork = chunkerize(selectors);
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-27 11:17:53 +00:00
|
|
|
dispatch(
|
2021-06-22 22:01:56 +00:00
|
|
|
{ hostname: document.location.hostname, type: "GET_CACHE" },
|
|
|
|
null,
|
2021-10-05 07:00:07 +00:00
|
|
|
async ({ enabled, matches }) => {
|
2021-06-27 11:17:53 +00:00
|
|
|
dispatch({ type: "ENABLE_POPUP" });
|
2021-06-22 22:01:56 +00:00
|
|
|
|
2021-06-23 11:26:00 +00:00
|
|
|
if (enabled) {
|
|
|
|
selectorsFromCache = matches;
|
2021-06-27 11:17:53 +00:00
|
|
|
dispatch({ type: "ENABLE_ICON" });
|
2021-10-05 07:00:07 +00:00
|
|
|
await Promise.all([setupClasses, setupSelectors]);
|
2021-10-05 07:06:13 +00:00
|
|
|
await runTasks();
|
2021-10-05 07:00:07 +00:00
|
|
|
setInterval(runTasks, 500);
|
2021-04-08 21:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-22 22:01:56 +00:00
|
|
|
);
|