Skip to content

Commit

Permalink
feat: retrieve local storage and session storage separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Codetrauma committed Oct 9, 2024
1 parent 36f1e87 commit 3bfbb6b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
14 changes: 11 additions & 3 deletions src/components/PluginInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,19 @@ export function PluginPermissions({
</span>
</PermissionDescription>
)}
{pluginContent.storage && (
{pluginContent.localStorage && (
<PermissionDescription fa="fa-solid fa-database">
<span className="cursor-default">
<span className="mr-1">Access browser storage from</span>
<MultipleParts parts={pluginContent.storage} />
<span className="mr-1">Access local storage storage from</span>
<MultipleParts parts={pluginContent.localStorage} />
</span>
</PermissionDescription>
)}
{pluginContent.sessionStorage && (
<PermissionDescription fa="fa-solid fa-database">
<span className="cursor-default">
<span className="mr-1">Access session storage from</span>
<MultipleParts parts={pluginContent.sessionStorage} />
</span>
</PermissionDescription>
)}
Expand Down
12 changes: 9 additions & 3 deletions src/entries/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ import { setStorage } from './db';
) {
const url = new URL(sender.tab.url);
const hostname = url.hostname;
const storage: { [key: string]: string } = request.storage;
for (const [key, value] of Object.entries(storage)) {
await setStorage(hostname, key, value);
const localStorage: { [key: string]: string } = request.storage.localStorage;
const sessionStorage: { [key: string]: string } = request.storage.sessionStorage;

for (const [key, value] of Object.entries(localStorage || {})) {
await setStorage(hostname, `localStorage:${key}`, value);
}

for (const [key, value] of Object.entries(sessionStorage || {})) {
await setStorage(hostname, `sessionStorage:${key}`, value);
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/entries/Content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { urlify } from '../../utils/misc';
const server = new RPCServer();

const storage = {
...localStorage,
...sessionStorage,
localStorage: localStorage,
sessionStorage: sessionStorage,
};

chrome.runtime.sendMessage({
Expand Down
48 changes: 40 additions & 8 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,41 @@ export const makePlugin = async (
funcs[fn] = HostFunctions[fn];
}
}
if (config?.storage) {
const storage: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.storage) {

if (config?.localStorage) {
const localStorage: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.localStorage) {
const cache = await getStorageByHost(host);

const localStorageEntries: { [key: string]: string } = {};
for (const [key, value] of Object.entries(cache)) {
if (key.startsWith("localStorage")) {
localStorageEntries[key.replace("localStorage:", "")] = value;
}
}
localStorage[host] = localStorageEntries;
}
// @ts-ignore
injectedConfig.localStorage = JSON.stringify(localStorage);
}

if (config?.sessionStorage) {
const sessionStorage: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.sessionStorage) {
const cache = await getStorageByHost(host);
storage[host] = cache;

const sessionStorageEntries: { [key: string]: string } = {};
for (const [key, value] of Object.entries(cache)) {
if (key.startsWith("sessionStorage")) {
sessionStorageEntries[key.replace("sessionStorage:", "")] = value;
}
}
sessionStorage[host] = sessionStorageEntries;
}
// @ts-ignore
injectedConfig.storage = JSON.stringify(storage);
injectedConfig.sessionStorage = JSON.stringify(sessionStorage);
}

if (config?.cookies) {
const cookies: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.cookies) {
Expand Down Expand Up @@ -306,7 +332,8 @@ export type PluginConfig = {
hostFunctions?: string[];
cookies?: string[];
headers?: string[];
storage?: string[];
localStorage: string[];
sessionStorage: string[];
requests: { method: string; url: string }[];
notaryUrls?: string[];
proxyUrls?: string[];
Expand Down Expand Up @@ -356,8 +383,13 @@ export const getPluginConfig = async (
assert(typeof name === 'string' && name.length);
}
}
if (config.storage) {
for (const name of config.storage) {
if (config.localStorage) {
for (const name of config.localStorage) {
assert(typeof name === 'string' && name.length);
}
}
if (config.sessionStorage) {
for (const name of config.sessionStorage) {
assert(typeof name === 'string' && name.length);
}
}
Expand Down

0 comments on commit 3bfbb6b

Please sign in to comment.