refactor(scripts): improve background code

This commit is contained in:
wanhose 2021-10-28 13:25:02 +02:00
parent 7ab81f0969
commit 1bb01ed265

View File

@ -1,30 +1,25 @@
/** /**
* @var cacheInitialState
* @description Cache initial state * @description Cache initial state
* @type {{ enabled: boolean, matches: string[] }} * @type {{ enabled: boolean, matches: string[] }}
*/ */
const cacheInitialState = { const initial = {
enabled: true, enabled: true,
matches: [], matches: [],
}; };
/** /**
* @function isValid
* @description Check cache validity * @description Check cache validity
*
* @param {object} [cache] * @param {object} [cache]
*/ */
const isValid = (cache) => const check = (cache) =>
typeof cache.enabled === "boolean" && typeof cache.enabled === "boolean" &&
Array.isArray(cache.matches) && Array.isArray(cache.matches) &&
cache.matches.every((match) => typeof match === "string"); cache.matches.every((match) => typeof match === "string");
/** /**
* @function disableIcon
* @description Disables icon * @description Disables icon
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -36,9 +31,7 @@ const disableIcon = (tabId) => {
}; };
/** /**
* @function disablePopup
* @description Disables popup * @description Disables popup
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -50,9 +43,7 @@ const disablePopup = (tabId) => {
}; };
/** /**
* @function enableIcon
* @description Enables icon * @description Enables icon
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -64,9 +55,7 @@ const enableIcon = (tabId) => {
}; };
/** /**
* @function enablePopup
* @description Enables popup * @description Enables popup
*
* @param {string} [tabId] * @param {string} [tabId]
*/ */
@ -78,39 +67,35 @@ const enablePopup = (tabId) => {
}; };
/** /**
* @function getCache
* @description Retrieves cache state * @description Retrieves cache state
*
* @param {string} [hostname] * @param {string} [hostname]
* @param {void} [responseCallback] * @param {void} [callback]
* @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state * @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state
*/ */
const getCache = (hostname, responseCallback) => { const getCache = (hostname, callback) => {
chrome.storage.local.get(null, (store) => { chrome.storage.local.get(null, (store) => {
try { try {
const cache = store[hostname]; const cache = store[hostname];
if (!isValid(cache)) throw new Error(); if (!check(cache)) throw new Error();
responseCallback(cache); callback(cache);
} catch { } catch {
chrome.storage.local.set({ [hostname]: cacheInitialState }); chrome.storage.local.set({ [hostname]: initial });
responseCallback(cacheInitialState); callback(initial);
} }
}); });
}; };
/** /**
* @async * @async
* @function getClasses
* @description Retrieves a selectors list * @description Retrieves a selectors list
* * @param {void} [callback]
* @param {void} [responseCallback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ matches: string[] }>} A selectors list
*/ */
const getClasses = async (responseCallback) => { const getClasses = async (callback) => {
try { try {
const url = const url =
"https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/classes.txt"; "https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/classes.txt";
@ -119,22 +104,20 @@ const getClasses = async (responseCallback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
responseCallback({ classes: data.split("\n") }); callback({ classes: data.split("\n") });
} catch { } catch {
responseCallback({ classes: [] }); callback({ classes: [] });
} }
}; };
/** /**
* @async * @async
* @function getSelectors
* @description Retrieves a selectors list * @description Retrieves a selectors list
* * @param {void} [callback]
* @param {void} [responseCallback]
* @returns {Promise<{ matches: string[] }>} A selectors list * @returns {Promise<{ matches: string[] }>} A selectors list
*/ */
const getSelectors = async (responseCallback) => { const getSelectors = async (callback) => {
try { try {
const url = const url =
"https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/elements.txt"; "https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/elements.txt";
@ -143,23 +126,21 @@ const getSelectors = async (responseCallback) => {
if (response.status !== 200) throw new Error(); if (response.status !== 200) throw new Error();
responseCallback({ selectors: data.split("\n") }); callback({ selectors: data.split("\n") });
} catch { } catch {
responseCallback({ selectors: [] }); callback({ selectors: [] });
} }
}; };
/** /**
* @function getTab
* @description Retrieves current tab information * @description Retrieves current tab information
* * @param {void} [callback]
* @param {void} [responseCallback]
* @returns {Promise<{ id: string, location: string }>} Current tab information * @returns {Promise<{ id: string, location: string }>} Current tab information
*/ */
const getTab = (responseCallback) => { const getTab = (callback) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
responseCallback({ callback({
id: tabs[0].id, id: tabs[0].id,
hostname: new URL(tabs[0].url).hostname, hostname: new URL(tabs[0].url).hostname,
}); });
@ -167,9 +148,7 @@ const getTab = (responseCallback) => {
}; };
/** /**
* @function updateCache
* @description Update cache state * @description Update cache state
*
* @param {string} [hostname] * @param {string} [hostname]
* @param {object} [state] * @param {object} [state]
*/ */
@ -197,7 +176,7 @@ const updateCache = (hostname, state) => {
* @description Listens to messages * @description Listens to messages
*/ */
chrome.runtime.onMessage.addListener((request, sender, responseCallback) => { chrome.runtime.onMessage.addListener((request, sender, callback) => {
const hasPermission = !sender.frameId || sender.frameId === 0; const hasPermission = !sender.frameId || sender.frameId === 0;
let tabId = sender.tab ? sender.tab.id : undefined; let tabId = sender.tab ? sender.tab.id : undefined;
@ -222,16 +201,16 @@ chrome.runtime.onMessage.addListener((request, sender, responseCallback) => {
if (hasPermission) enablePopup(tabId); if (hasPermission) enablePopup(tabId);
break; break;
case "GET_CACHE": case "GET_CACHE":
getCache(request.hostname, responseCallback); getCache(request.hostname, callback);
break; break;
case "GET_CLASSES": case "GET_CLASSES":
getClasses(responseCallback); getClasses(callback);
break; break;
case "GET_SELECTORS": case "GET_SELECTORS":
getSelectors(responseCallback); getSelectors(callback);
break; break;
case "GET_TAB": case "GET_TAB":
getTab(responseCallback); getTab(callback);
break; break;
case "UPDATE_CACHE": case "UPDATE_CACHE":
updateCache(request.hostname, request.state); updateCache(request.hostname, request.state);