Skip to content

Commit

Permalink
feat: add storage context
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Jun 19, 2024
1 parent 9f799ec commit 6c662c7
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
20 changes: 10 additions & 10 deletions src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ async function removeMenuItem(menuItemId, {throwError = false} = {}) {
}

async function createMenu() {
const menuKey = browser.extension.inIncognitoContext
? 'privateMenuItems'
: 'menuItems';
const context = {
name: 'private',
active: browser.extension.inIncognitoContext
};

const {showInContextMenu, [menuKey]: currentItems} = await storage.get([
'showInContextMenu',
menuKey
]);
const {menuItems: currentItems} = await storage.get('menuItems', {context});

for (const itemId of currentItems) {
await removeMenuItem(itemId);
}

const {showInContextMenu} = await storage.get('showInContextMenu');
const newItems = showInContextMenu ? await getMenuItems() : [];
await storage.set({[menuKey]: newItems.map(item => item.id)});

await storage.set({menuItems: newItems.map(item => item.id)}, {context});

try {
for (const item of newItems) {
Expand Down Expand Up @@ -599,7 +599,7 @@ async function execEngine(tabId, engine, taskId) {
await executeScript({
func: taskId => (self.taskId = taskId),
args: [taskId],
code: `var taskId = '${taskId}';`,
code: `self.taskId = '${taskId}'`,
tabId
});
await executeScript({files: ['/src/commons-engine/script.js'], tabId});
Expand Down Expand Up @@ -637,7 +637,7 @@ async function openCurrentDoc({linkUrl} = {}) {
if (await hasModule({tabId: activeTab.id, module: 'tools', insert: true})) {
await executeScript({
func: () => self.openCurrentDoc(),
code: `openCurrentDoc()`,
code: `self.openCurrentDoc()`,
tabId: activeTab.id
});
} else {
Expand Down
63 changes: 56 additions & 7 deletions src/storage/storage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {capitalizeFirstLetter, lowercaseFirstLetter} from 'utils/common';
import {storageRevisions} from 'utils/config';

async function isStorageArea({area = 'local'} = {}) {
Expand Down Expand Up @@ -49,19 +50,67 @@ async function ensureStorageReady({area = 'local'} = {}) {
}
}

async function get(keys = null, {area = 'local'} = {}) {
function encodeStorageData(data, context) {
if (context?.active) {
if (typeof data === 'string') {
return `${context.name}${capitalizeFirstLetter(data)}`;
} else if (Array.isArray(data)) {
const items = [];

for (const item of data) {
items.push(`${context.name}${capitalizeFirstLetter(item)}`);
}

return items;
} else {
const items = {};

for (const [key, value] of Object.entries(data)) {
items[`${context.name}${capitalizeFirstLetter(key)}`] = value;
}

return items;
}
}

return data;
}

function decodeStorageData(data, context) {
if (context?.active) {
const items = {};

for (const [key, value] of Object.entries(data)) {
items[
lowercaseFirstLetter(key.replace(new RegExp(`^${context.name}`), ''))
] = value;
}

return items;
}

return data;
}

async function get(keys = null, {area = 'local', context = null} = {}) {
await ensureStorageReady({area});
return browser.storage[area].get(keys);

return decodeStorageData(
await browser.storage[area].get(encodeStorageData(keys, context)),
context
);
}

async function set(obj, {area = 'local'} = {}) {
async function set(obj, {area = 'local', context = null} = {}) {
await ensureStorageReady({area});
return browser.storage[area].set(obj);

return browser.storage[area].set(encodeStorageData(obj, context));
}

async function remove(keys, {area = 'local'} = {}) {
async function remove(keys, {area = 'local', context = null} = {}) {
await ensureStorageReady({area});
return browser.storage[area].remove(keys);

return browser.storage[area].remove(encodeStorageData(keys, context));
}

async function clear({area = 'local'} = {}) {
Expand All @@ -70,4 +119,4 @@ async function clear({area = 'local'} = {}) {
}

export default {get, set, remove, clear};
export {isStorageArea, isStorageReady, ensureStorageReady};
export {isStorageArea, isStorageReady};
10 changes: 10 additions & 0 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function capitalizeFirstLetter(string, {locale = 'en-US'} = {}) {
return string.replace(/^\p{CWU}/u, char => char.toLocaleUpperCase(locale));
}

function lowercaseFirstLetter(string, {locale = 'en-US'} = {}) {
return string.replace(/^\p{CWL}/u, char => char.toLocaleLowerCase(locale));
}

function getCharCount(string) {
return [...string].length;
}
Expand Down Expand Up @@ -547,6 +555,8 @@ export {
isBackgroundPageContext,
getExtensionDomain,
getRandomInt,
capitalizeFirstLetter,
lowercaseFirstLetter,
getCharCount,
querySelectorXpath,
nodeQuerySelector,
Expand Down

0 comments on commit 6c662c7

Please sign in to comment.