Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed BrowserStorage #173

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/composables/useBrowserStorage.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
import { ref, watch } from 'vue'

import { ref, watch } from "vue"
export function useBrowserSyncStorage<T>(key: string, defaultValue: T) {
const data = ref<T>(defaultValue)

// Initialize storage with the value from chrome.storage.sync
chrome.storage.sync.get(key, (result) => {
if (result[key] !== undefined) {
if (
result?.[key] != undefined &&
typeof result[key] === typeof defaultValue
) {
data.value = result[key]
}
})

// Watch for changes in the storage and update chrome.storage.sync
watch(data, (newValue) => {
chrome.storage.sync.set({ [key]: newValue })
})

watch(
data,
(newValue) => {
chrome.storage.sync.set({ [key]: toRaw(newValue) })
},
{ deep: true },
)
return data
}

export function useBrowserLocalStorage<T>(key: string, defaultValue: T) {
const data = ref<T>(defaultValue)

// Initialize storage with the value from chrome.storage.local
chrome.storage.local.get(key, (result) => {
if (result[key] !== undefined) {
if (
result?.[key] != undefined &&
typeof result[key] === typeof defaultValue
) {
data.value = result[key]
}
})

// Watch for changes in the storage and update chrome.storage.sync
watch(data, (newValue) => {
chrome.storage.local.set({ [key]: newValue })
})

// Watch for changes in the storage and update chrome.storage.local
watch(
data,
(newValue) => {
chrome.storage.local.set({ [key]: toRaw(newValue) })
},
{ deep: true },
)
return data
}