-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from Dreamlinerm/master
fixed BrowserStorage
- Loading branch information
Showing
1 changed file
with
24 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |