Skip to content

Commit

Permalink
refactor(browser-extension): cache to store avoiding duplicate names
Browse files Browse the repository at this point in the history
  • Loading branch information
wanhose committed May 19, 2022
1 parent 1463f9b commit 99beac8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
19 changes: 10 additions & 9 deletions packages/browser-extension/src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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;
Expand All @@ -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'),
});
Expand Down
4 changes: 2 additions & 2 deletions packages/browser-extension/src/scripts/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) : [];

Expand All @@ -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) {
Expand Down
10 changes: 3 additions & 7 deletions packages/browser-extension/src/scripts/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 99beac8

Please sign in to comment.