From 99beac808072f19dd1ca48a7299fa864275eb292 Mon Sep 17 00:00:00 2001 From: wanhose Date: Thu, 19 May 2022 16:50:14 +0200 Subject: [PATCH] refactor(browser-extension): cache to store avoiding duplicate names --- .../src/scripts/background.js | 19 ++++++++++--------- .../browser-extension/src/scripts/content.js | 4 ++-- .../browser-extension/src/scripts/popup.js | 10 +++------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/browser-extension/src/scripts/background.js b/packages/browser-extension/src/scripts/background.js index 09f062b..1ee567c 100644 --- a/packages/browser-extension/src/scripts/background.js +++ b/packages/browser-extension/src/scripts/background.js @@ -57,13 +57,13 @@ const enableIcon = (tabId) => const enablePopup = (tabId) => chrome.browserAction.setPopup({ popup: 'popup.html', tabId }); /** - * @description Retrieves cache state + * @description Retrieves store * @param {string} hostname * @param {void} callback * @returns {{ enabled: boolean }} */ -const getCache = (hostname, callback) => { +const getStore = (hostname, callback) => { chrome.storage.local.get(null, (store) => { callback(store[hostname] ?? initial); }); @@ -178,12 +178,12 @@ const report = () => { }; /** - * @description Update cache state + * @description Update store * @param {string} [hostname] * @param {object} [state] */ -const updateCache = (hostname, state) => { +const updateStore = (hostname, state) => { chrome.storage.local.get(null, (cache) => { const current = cache[hostname]; @@ -214,17 +214,17 @@ chrome.runtime.onMessage.addListener((request, sender, callback) => { case 'ENABLE_POPUP': if (tabId) enablePopup(tabId); break; - case 'GET_CACHE': - getCache(hostname, callback); - break; case 'GET_DATA': getData(callback); break; + case 'GET_STORE': + getStore(hostname, callback); + break; case 'GET_TAB': getTab(callback); break; - case 'UPDATE_CACHE': - updateCache(hostname, state); + case 'UPDATE_STORE': + updateStore(hostname, state); break; default: break; @@ -239,6 +239,7 @@ chrome.runtime.onMessage.addListener((request, sender, callback) => { chrome.contextMenus.create({ contexts: ['all'], + documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches, id: contextMenuId, title: chrome.i18n.getMessage('contextMenuText'), }); diff --git a/packages/browser-extension/src/scripts/content.js b/packages/browser-extension/src/scripts/content.js index 1a422df..da2e93f 100644 --- a/packages/browser-extension/src/scripts/content.js +++ b/packages/browser-extension/src/scripts/content.js @@ -147,7 +147,7 @@ const observer = new MutationObserver((mutations, instance) => { */ document.addEventListener('readystatechange', () => { - dispatch({ hostname, type: 'GET_CACHE' }, null, async ({ enabled }) => { + dispatch({ hostname, type: 'GET_STORE' }, null, async ({ enabled }) => { if (document.readyState === 'complete' && enabled && !preview) { const nodes = selectors.length ? Array.from(document.querySelectorAll(selectors)) : []; @@ -169,7 +169,7 @@ window.addEventListener('unload', () => {}); * @description Setups everything and starts to observe if enabled */ -dispatch({ hostname, type: 'GET_CACHE' }, null, ({ enabled }) => { +dispatch({ hostname, type: 'GET_STORE' }, null, ({ enabled }) => { dispatch({ type: 'ENABLE_POPUP' }); if (enabled) { diff --git a/packages/browser-extension/src/scripts/popup.js b/packages/browser-extension/src/scripts/popup.js index 0e2db4f..2d1d375 100644 --- a/packages/browser-extension/src/scripts/popup.js +++ b/packages/browser-extension/src/scripts/popup.js @@ -36,14 +36,10 @@ const isChromium = chrome.runtime.getURL('').startsWith('chrome-extension://'); const handlePowerChange = () => { dispatch({ type: 'GET_TAB' }, null, ({ hostname, id }) => { - dispatch({ hostname, type: 'GET_CACHE' }, null, ({ enabled }) => { + dispatch({ hostname, type: 'GET_STORE' }, null, ({ enabled }) => { const power = document.getElementById('power'); - dispatch({ - hostname, - state: { enabled: !enabled }, - type: 'UPDATE_CACHE', - }); + dispatch({ hostname, state: { enabled: !enabled }, type: 'UPDATE_STORE' }); if (!enabled === false) power.removeAttribute('checked'); if (!enabled === true) power.setAttribute('checked', 'checked'); chrome.tabs.reload(id, { bypassCache: true }); @@ -90,7 +86,7 @@ const handleRate = (event) => { const handleContentLoaded = () => { dispatch({ type: 'GET_TAB' }, null, ({ hostname }) => { - dispatch({ hostname, type: 'GET_CACHE' }, null, ({ enabled }) => { + dispatch({ hostname, type: 'GET_STORE' }, null, ({ enabled }) => { translate(); const host = document.getElementById('host');