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 });
/**
* @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'),
});

View File

@ -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) {

View File

@ -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');