refactor(browser-extension): cache to store avoiding duplicate names

This commit is contained in:
wanhose 2022-05-19 16:50:14 +02:00
parent 1463f9bafe
commit 99beac8080
3 changed files with 15 additions and 18 deletions

View File

@ -57,13 +57,13 @@ const enableIcon = (tabId) =>
const enablePopup = (tabId) => chrome.browserAction.setPopup({ popup: 'popup.html', tabId }); const enablePopup = (tabId) => chrome.browserAction.setPopup({ popup: 'popup.html', tabId });
/** /**
* @description Retrieves cache state * @description Retrieves store
* @param {string} hostname * @param {string} hostname
* @param {void} callback * @param {void} callback
* @returns {{ enabled: boolean }} * @returns {{ enabled: boolean }}
*/ */
const getCache = (hostname, callback) => { const getStore = (hostname, callback) => {
chrome.storage.local.get(null, (store) => { chrome.storage.local.get(null, (store) => {
callback(store[hostname] ?? initial); callback(store[hostname] ?? initial);
}); });
@ -178,12 +178,12 @@ const report = () => {
}; };
/** /**
* @description Update cache state * @description Update store
* @param {string} [hostname] * @param {string} [hostname]
* @param {object} [state] * @param {object} [state]
*/ */
const updateCache = (hostname, state) => { const updateStore = (hostname, state) => {
chrome.storage.local.get(null, (cache) => { chrome.storage.local.get(null, (cache) => {
const current = cache[hostname]; const current = cache[hostname];
@ -214,17 +214,17 @@ chrome.runtime.onMessage.addListener((request, sender, callback) => {
case 'ENABLE_POPUP': case 'ENABLE_POPUP':
if (tabId) enablePopup(tabId); if (tabId) enablePopup(tabId);
break; break;
case 'GET_CACHE':
getCache(hostname, callback);
break;
case 'GET_DATA': case 'GET_DATA':
getData(callback); getData(callback);
break; break;
case 'GET_STORE':
getStore(hostname, callback);
break;
case 'GET_TAB': case 'GET_TAB':
getTab(callback); getTab(callback);
break; break;
case 'UPDATE_CACHE': case 'UPDATE_STORE':
updateCache(hostname, state); updateStore(hostname, state);
break; break;
default: default:
break; break;
@ -239,6 +239,7 @@ chrome.runtime.onMessage.addListener((request, sender, callback) => {
chrome.contextMenus.create({ chrome.contextMenus.create({
contexts: ['all'], contexts: ['all'],
documentUrlPatterns: chrome.runtime.getManifest().content_scripts[0].matches,
id: contextMenuId, id: contextMenuId,
title: chrome.i18n.getMessage('contextMenuText'), title: chrome.i18n.getMessage('contextMenuText'),
}); });

View File

@ -147,7 +147,7 @@ const observer = new MutationObserver((mutations, instance) => {
*/ */
document.addEventListener('readystatechange', () => { 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) { if (document.readyState === 'complete' && enabled && !preview) {
const nodes = selectors.length ? Array.from(document.querySelectorAll(selectors)) : []; 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 * @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' }); dispatch({ type: 'ENABLE_POPUP' });
if (enabled) { if (enabled) {

View File

@ -36,14 +36,10 @@ const isChromium = chrome.runtime.getURL('').startsWith('chrome-extension://');
const handlePowerChange = () => { const handlePowerChange = () => {
dispatch({ type: 'GET_TAB' }, null, ({ hostname, id }) => { 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'); const power = document.getElementById('power');
dispatch({ dispatch({ hostname, state: { enabled: !enabled }, type: 'UPDATE_STORE' });
hostname,
state: { enabled: !enabled },
type: 'UPDATE_CACHE',
});
if (!enabled === false) power.removeAttribute('checked'); if (!enabled === false) power.removeAttribute('checked');
if (!enabled === true) power.setAttribute('checked', 'checked'); if (!enabled === true) power.setAttribute('checked', 'checked');
chrome.tabs.reload(id, { bypassCache: true }); chrome.tabs.reload(id, { bypassCache: true });
@ -90,7 +86,7 @@ const handleRate = (event) => {
const handleContentLoaded = () => { const handleContentLoaded = () => {
dispatch({ type: 'GET_TAB' }, null, ({ hostname }) => { dispatch({ type: 'GET_TAB' }, null, ({ hostname }) => {
dispatch({ hostname, type: 'GET_CACHE' }, null, ({ enabled }) => { dispatch({ hostname, type: 'GET_STORE' }, null, ({ enabled }) => {
translate(); translate();
const host = document.getElementById('host'); const host = document.getElementById('host');