feat(scripts): add chrome.runtime.sendMessage shortcut, fix facebook fixed position and fix not enabled state in content scripts

This commit is contained in:
wanhose 2021-06-27 13:17:53 +02:00
parent c86ff53fe8
commit fb297758da

View File

@ -6,6 +6,14 @@
let attempts = 1;
/**
* @constant dispatch
* @description Shortcut to send messages to background script
* @type {void}
*/
const dispatch = chrome.runtime.sendMessage;
/**
* @var intervalId
* @description Task interval identifier
@ -46,7 +54,7 @@ let selectorsFromNetwork = [];
const fix = () => {
const html = document.documentElement;
const body = document.body;
const facebook = document.getElementsByClassName("._31e")[0];
const facebook = document.getElementsByClassName("_31e")[0];
if (body) body.style.setProperty("overflow-y", "unset", "important");
if (facebook) facebook.style.setProperty("position", "unset", "important");
@ -116,7 +124,7 @@ const removeFromNetwork = () => {
if (!["BODY", "HTML"].includes(tagName)) {
element.remove();
loading = false;
chrome.runtime.sendMessage({
dispatch({
hostname: document.location.hostname,
state: { matches: [selector] },
type: "UPDATE_CACHE",
@ -152,25 +160,23 @@ const runTasks = () => {
* @description Setup extension context
*/
chrome.runtime.sendMessage(
dispatch(
{ hostname: document.location.hostname, type: "GET_CACHE" },
null,
async ({ enabled, matches }) => {
chrome.runtime.sendMessage({ type: "ENABLE_POPUP" });
dispatch({ type: "ENABLE_POPUP" });
if (enabled) {
selectorsFromCache = matches;
chrome.runtime.sendMessage({ type: "ENABLE_ICON" });
chrome.runtime.sendMessage(
{ type: "GET_LIST" },
null,
async ({ selectors }) => {
selectorsFromNetwork = selectors;
intervalId = setInterval(runTasks, 500);
}
);
dispatch({ type: "ENABLE_ICON" });
dispatch({ type: "GET_LIST" }, null, async ({ selectors }) => {
selectorsFromNetwork = selectors;
intervalId = setInterval(runTasks, 500);
});
} else {
document.documentElement.style.setProperty("opacity", "1", "important");
document.addEventListener("DOMContentLoaded", () => {
document.body.style.setProperty("opacity", "1", "important");
});
}
}
);