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