/** * @description Cache initial state * @type {{ enabled: boolean, matches: string[] }} */ const initial = { enabled: true, matches: [], }; /** * @description Check cache validity * @param {object} [cache] */ const check = (cache) => typeof cache.enabled === "boolean" && Array.isArray(cache.matches) && cache.matches.every((match) => typeof match === "string"); /** * @description Disables icon * @param {string} [tabId] */ const disableIcon = (tabId) => { chrome.browserAction.setIcon({ path: "assets/icons/disabled.png", tabId, }); }; /** * @description Disables popup * @param {string} [tabId] */ const disablePopup = (tabId) => { chrome.browserAction.setPopup({ popup: "", tabId, }); }; /** * @description Enables icon * @param {string} [tabId] */ const enableIcon = (tabId) => { chrome.browserAction.setIcon({ path: "assets/icons/enabled.png", tabId, }); }; /** * @description Enables popup * @param {string} [tabId] */ const enablePopup = (tabId) => { chrome.browserAction.setPopup({ popup: "popup.html", tabId, }); }; /** * @description Retrieves cache state * @param {string} [hostname] * @param {void} [callback] * @returns {Promise<{ enabled: boolean, matches: string[] }>} Cache state */ const getCache = (hostname, callback) => { chrome.storage.local.get(null, (store) => { try { const cache = store[hostname]; if (!check(cache)) throw new Error(); callback(cache); } catch { chrome.storage.local.set({ [hostname]: initial }); callback(initial); } }); }; /** * @async * @description Retrieves a selectors list * @param {void} [callback] * @returns {Promise<{ matches: string[] }>} A selectors list */ const getClasses = async (callback) => { try { const url = "https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/classes.txt"; const response = await fetch(url); const data = await response.text(); if (response.status !== 200) throw new Error(); callback({ classes: data.split("\n") }); } catch { callback({ classes: [] }); } }; /** * @async * @description Retrieves a selectors list * @param {void} [callback] * @returns {Promise<{ matches: string[] }>} A selectors list */ const getSelectors = async (callback) => { try { const url = "https://raw.githubusercontent.com/wanhose/cookie-dialog-monster/master/data/elements.txt"; const response = await fetch(url); const data = await response.text(); if (response.status !== 200) throw new Error(); callback({ selectors: data.split("\n") }); } catch { callback({ selectors: [] }); } }; /** * @description Retrieves current tab information * @param {void} [callback] * @returns {Promise<{ id: string, location: string }>} Current tab information */ const getTab = (callback) => { chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { callback({ id: tabs[0].id, hostname: new URL(tabs[0].url).hostname, }); }); }; /** * @description Update cache state * @param {string} [hostname] * @param {object} [state] */ const updateCache = (hostname, state) => { chrome.storage.local.get(null, (cache) => { const current = cache[hostname]; chrome.storage.local.set({ [hostname]: { enabled: typeof state.enabled === "undefined" ? current.enabled : state.enabled, matches: typeof state.matches === "undefined" ? current.matches : [...new Set([...current.matches, ...state.matches])], }, }); }); }; /** * @description Listens to messages */ chrome.runtime.onMessage.addListener((request, sender, callback) => { const hasPermission = !sender.frameId || sender.frameId === 0; let tabId = sender.tab ? sender.tab.id : undefined; if (!tabId) { chrome.tabs.query( { active: true, currentWindow: true }, (tabs) => (tabId = tabs[0] ? tabs[0].id : 0) ); } switch (request.type) { case "DISABLE_ICON": if (hasPermission) disableIcon(tabId); break; case "DISABLE_POPUP": if (hasPermission) disablePopup(tabId); break; case "ENABLE_ICON": if (hasPermission) enableIcon(tabId); break; case "ENABLE_POPUP": if (hasPermission) enablePopup(tabId); break; case "GET_CACHE": getCache(request.hostname, callback); break; case "GET_CLASSES": getClasses(callback); break; case "GET_SELECTORS": getSelectors(callback); break; case "GET_TAB": getTab(callback); break; case "UPDATE_CACHE": updateCache(request.hostname, request.state); break; default: break; } return true; }); /** * @description Listens to updates */ chrome.runtime.onInstalled.addListener(({ reason }) => { if (reason === chrome.runtime.OnInstalledReason.UPDATE) { chrome.storage.local.clear(); } });