Add getContext

This commit is contained in:
johnathan 2025-06-06 17:11:48 +08:00
parent 127fb5866a
commit eee860f804
3 changed files with 20 additions and 5 deletions

View File

@ -46,6 +46,13 @@ const storageInterface: StorageLikeAsync = {
}, },
}; };
/**
* Get page app context
*/
function getContext() {
return document.location.pathname.split('/')[2] as 'sidepanel' | 'options';
}
/** /**
* https://github.com/vueuse/vueuse/blob/658444bf9f8b96118dbd06eba411bb6639e24e88/packages/core/useStorageAsync/index.ts * https://github.com/vueuse/vueuse/blob/658444bf9f8b96118dbd06eba411bb6639e24e88/packages/core/useStorageAsync/index.ts
* *
@ -73,8 +80,8 @@ export function useWebExtensionStorage<T>(
initialValue: MaybeRef<T>, initialValue: MaybeRef<T>,
options: Pick< options: Pick<
WebExtensionStorageOptions<T>, WebExtensionStorageOptions<T>,
'shallow' | 'serializer' | 'listenToStorageChanges' | 'flush' | 'deep' | 'eventFilter' 'shallow' | 'serializer' | 'flush' | 'deep' | 'eventFilter'
> = {}, > & { listenToStorageChanges?: boolean | 'sidepanel' | 'options' } = {},
): RemovableRef<T> { ): RemovableRef<T> {
const { const {
shallow = false, shallow = false,
@ -119,6 +126,12 @@ export function useWebExtensionStorage<T>(
if (!(key in changes)) { if (!(key in changes)) {
return; return;
} }
if (typeof listenToStorageChanges === 'string') {
const context = getContext();
if (listenToStorageChanges !== context) {
return;
}
}
try { try {
pauseWatch(); pauseWatch();
await pullFromStorage(); await pullFromStorage();

View File

@ -5,9 +5,11 @@ import type { App } from 'vue';
* @param app Vue app * @param app Vue app
*/ */
export function setupApp(app: App) { export function setupApp(app: App) {
const context = document.location.pathname.split('/')[2];
// Inject a globally available `$app` object in template // Inject a globally available `$app` object in template
app.config.globalProperties.$app = { app.config.globalProperties.$app = {
context: '', context,
}; };
// Provide access to `app` in script setup with `const app = inject('app')` // Provide access to `app` in script setup with `const app = inject('app')`

View File

@ -18,7 +18,7 @@ export const detailItems = useWebExtensionStorage<Map<string, AmazonDetailItem>>
'detailItems', 'detailItems',
new Map(), new Map(),
{ {
listenToStorageChanges: false, listenToStorageChanges: 'options',
}, },
); );
@ -26,7 +26,7 @@ export const reviewItems = useWebExtensionStorage<Map<string, AmazonReview[]>>(
'reviewItems', 'reviewItems',
new Map(), new Map(),
{ {
listenToStorageChanges: false, listenToStorageChanges: 'options',
}, },
); );