diff --git a/src/background/main.js b/src/background/main.js index 3fecec5..f7c1e34 100644 --- a/src/background/main.js +++ b/src/background/main.js @@ -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) { @@ -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}); @@ -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 { diff --git a/src/storage/storage.js b/src/storage/storage.js index 2e86a4b..b218be6 100755 --- a/src/storage/storage.js +++ b/src/storage/storage.js @@ -1,3 +1,4 @@ +import {capitalizeFirstLetter, lowercaseFirstLetter} from 'utils/common'; import {storageRevisions} from 'utils/config'; async function isStorageArea({area = 'local'} = {}) { @@ -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'} = {}) { @@ -70,4 +119,4 @@ async function clear({area = 'local'} = {}) { } export default {get, set, remove, clear}; -export {isStorageArea, isStorageReady, ensureStorageReady}; +export {isStorageArea, isStorageReady}; diff --git a/src/utils/common.js b/src/utils/common.js index 440a2fe..1e79fdd 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -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; } @@ -547,6 +555,8 @@ export { isBackgroundPageContext, getExtensionDomain, getRandomInt, + capitalizeFirstLetter, + lowercaseFirstLetter, getCharCount, querySelectorXpath, nodeQuerySelector,