properly rewrite sessionStorage

This commit is contained in:
Avad3 2024-07-18 21:55:17 -04:00
parent eba3e759fd
commit 4b6261188e

View file

@ -8,8 +8,10 @@ const store = new IDBMapSync(locationProxy.host, {
await store.sync(); await store.sync();
function storageProxy(scope: Storage): Storage { delete window.localStorage;
return new Proxy(scope, { delete window.sessionStorage;
window.localStorage = new Proxy(localStorage, {
get(target, prop) { get(target, prop) {
switch (prop) { switch (prop) {
case "getItem": case "getItem":
@ -62,13 +64,56 @@ function storageProxy(scope: Storage): Storage {
return true; return true;
}, },
}); });
window.sessionStorage = new Proxy(sessionStorage, {
get(target, prop) {
switch (prop) {
case "getItem":
return (key: string) => {
return target.getItem(locationProxy.host + "@" + key);
} }
const localStorageProxy = storageProxy(window.localStorage); case "setItem":
const sessionStorageProxy = storageProxy(window.sessionStorage); return (key: string, value: string) => {
target.setItem(locationProxy.host + "@" + key, value);
}
delete window.localStorage; case "removeItem":
delete window.sessionStorage; return (key: string) => {
target.removeItem(locationProxy.host + "@" + key);
}
window.localStorage = localStorageProxy; case "clear":
window.sessionStorage = sessionStorageProxy; return () => {
for (const key in Object.keys(target)) {
if (key.startsWith(locationProxy.host)) {
target.removeItem(key);
}
}
}
case "key":
return (index: number) => {
const keys = Object.keys(target).filter((key) => key.startsWith(locationProxy.host));
return target.getItem(keys[index]);
}
case "length":
return target.length;
default:
if (prop in Object.prototype) {
return Reflect.get(target, prop);
}
return target.getItem(locationProxy.host + "@" + (prop as string));
}
},
defineProperty(target, property, attributes) {
target.setItem(locationProxy.host + "@" + (property as string), attributes.value);
return true;
},
});