refactor(usePreferences): remove debug logging and simplify API detection
This commit is contained in:
parent
00b1083626
commit
3d962c647f
|
@ -14,40 +14,33 @@ export const usePreferences = defineStore('preferences', () => {
|
||||||
|
|
||||||
const isLoaded = ref(false)
|
const isLoaded = ref(false)
|
||||||
const storageType = ref<'chrome' | 'localStorage' | 'memory'>('chrome')
|
const storageType = ref<'chrome' | 'localStorage' | 'memory'>('chrome')
|
||||||
const debugInfo = ref<string[]>([])
|
|
||||||
|
|
||||||
// 添加调试日志
|
// 默认偏好设置
|
||||||
const addDebugInfo = (info: string) => {
|
const defaultPreferences = {
|
||||||
debugInfo.value.push(`[${new Date().toISOString()}] ${info}`)
|
displayTimeLeft: false,
|
||||||
console.log(info)
|
presentLyrics: false
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检测可用的 API
|
// 检测可用的 API
|
||||||
const detectAvailableAPIs = () => {
|
const detectAvailableAPIs = () => {
|
||||||
addDebugInfo('开始检测可用的存储 API...')
|
|
||||||
|
|
||||||
// 检查原生 chrome API
|
// 检查原生 chrome API
|
||||||
try {
|
try {
|
||||||
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.local) {
|
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.sync) {
|
||||||
addDebugInfo('✅ 检测到 chrome.storage.local')
|
|
||||||
storageType.value = 'chrome'
|
storageType.value = 'chrome'
|
||||||
return 'chrome'
|
return 'chrome'
|
||||||
} else {
|
|
||||||
addDebugInfo('❌ 未检测到原生 chrome.storage.local')
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addDebugInfo(`❌ chrome API 检测失败: ${error}`)
|
// Silent fail
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查 window.chrome
|
// 检查 window.chrome
|
||||||
try {
|
try {
|
||||||
if (window.chrome && window.chrome.storage && window.chrome.storage.local) {
|
if (window.chrome && window.chrome.storage && window.chrome.storage.sync) {
|
||||||
addDebugInfo('✅ 检测到 window.chrome.storage.local')
|
|
||||||
storageType.value = 'chrome'
|
storageType.value = 'chrome'
|
||||||
return 'chrome'
|
return 'chrome'
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addDebugInfo(`❌ window.chrome API 检测失败: ${error}`)
|
// Silent fail
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查 localStorage
|
// 检查 localStorage
|
||||||
|
@ -55,16 +48,14 @@ export const usePreferences = defineStore('preferences', () => {
|
||||||
if (typeof localStorage !== 'undefined') {
|
if (typeof localStorage !== 'undefined') {
|
||||||
localStorage.setItem('msr_test', 'test')
|
localStorage.setItem('msr_test', 'test')
|
||||||
localStorage.removeItem('msr_test')
|
localStorage.removeItem('msr_test')
|
||||||
addDebugInfo('✅ 检测到 localStorage')
|
|
||||||
storageType.value = 'localStorage'
|
storageType.value = 'localStorage'
|
||||||
return 'localStorage'
|
return 'localStorage'
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addDebugInfo(`❌ localStorage 检测失败: ${error}`)
|
// Silent fail
|
||||||
}
|
}
|
||||||
|
|
||||||
// 都不可用,使用内存存储
|
// 都不可用,使用内存存储
|
||||||
addDebugInfo('⚠️ 使用内存存储(不持久化)')
|
|
||||||
storageType.value = 'memory'
|
storageType.value = 'memory'
|
||||||
return 'memory'
|
return 'memory'
|
||||||
}
|
}
|
||||||
|
@ -77,14 +68,12 @@ export const usePreferences = defineStore('preferences', () => {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'chrome':
|
case 'chrome':
|
||||||
return await new Promise((resolve) => {
|
return await new Promise((resolve) => {
|
||||||
const api = chrome?.storage?.local || window.chrome?.storage?.local
|
const api = chrome?.storage?.sync || window.chrome?.storage?.sync
|
||||||
if (api) {
|
if (api) {
|
||||||
api.get({ [key]: defaultValue }, (result) => {
|
api.get({ [key]: defaultValue }, (result) => {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
addDebugInfo(`Chrome storage 错误: ${chrome.runtime.lastError.message}`)
|
|
||||||
resolve(defaultValue)
|
resolve(defaultValue)
|
||||||
} else {
|
} else {
|
||||||
addDebugInfo(`从 Chrome storage 读取: ${key} = ${result[key]}`)
|
|
||||||
resolve(result[key])
|
resolve(result[key])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -96,16 +85,13 @@ export const usePreferences = defineStore('preferences', () => {
|
||||||
case 'localStorage':
|
case 'localStorage':
|
||||||
const stored = localStorage.getItem(`msr_${key}`)
|
const stored = localStorage.getItem(`msr_${key}`)
|
||||||
const value = stored ? JSON.parse(stored) : defaultValue
|
const value = stored ? JSON.parse(stored) : defaultValue
|
||||||
addDebugInfo(`从 localStorage 读取: ${key} = ${value}`)
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
case 'memory':
|
case 'memory':
|
||||||
default:
|
default:
|
||||||
addDebugInfo(`从内存返回默认值: ${key} = ${defaultValue}`)
|
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addDebugInfo(`获取存储值失败 (${type}): ${error}`)
|
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,15 +104,12 @@ export const usePreferences = defineStore('preferences', () => {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'chrome':
|
case 'chrome':
|
||||||
return await new Promise<void>((resolve, reject) => {
|
return await new Promise<void>((resolve, reject) => {
|
||||||
const api = chrome?.storage?.local || window.chrome?.storage?.local
|
const api = chrome?.storage?.sync || window.chrome?.storage?.sync
|
||||||
if (api) {
|
if (api) {
|
||||||
api.set({ [key]: value }, () => {
|
api.set({ [key]: value }, () => {
|
||||||
if (chrome.runtime.lastError) {
|
if (chrome.runtime.lastError) {
|
||||||
const error = `Chrome storage 保存错误: ${chrome.runtime.lastError.message}`
|
reject(new Error(chrome.runtime.lastError.message))
|
||||||
addDebugInfo(error)
|
|
||||||
reject(new Error(error))
|
|
||||||
} else {
|
} else {
|
||||||
addDebugInfo(`保存到 Chrome storage: ${key} = ${value}`)
|
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -137,79 +120,55 @@ export const usePreferences = defineStore('preferences', () => {
|
||||||
|
|
||||||
case 'localStorage':
|
case 'localStorage':
|
||||||
localStorage.setItem(`msr_${key}`, JSON.stringify(value))
|
localStorage.setItem(`msr_${key}`, JSON.stringify(value))
|
||||||
addDebugInfo(`保存到 localStorage: ${key} = ${value}`)
|
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'memory':
|
case 'memory':
|
||||||
addDebugInfo(`内存存储(不持久化): ${key} = ${value}`)
|
// 内存存储(不持久化)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addDebugInfo(`保存设置失败 (${type}): ${error}`)
|
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取所有偏好设置
|
||||||
|
const getPreferences = async () => {
|
||||||
|
return await getStoredValue('preferences', defaultPreferences)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保存所有偏好设置
|
||||||
|
const savePreferences = async () => {
|
||||||
|
const preferences = {
|
||||||
|
displayTimeLeft: displayTimeLeft.value,
|
||||||
|
presentLyrics: presentLyrics.value
|
||||||
|
}
|
||||||
|
await setStoredValue('preferences', preferences)
|
||||||
|
}
|
||||||
|
|
||||||
// 异步初始化函数
|
// 异步初始化函数
|
||||||
const initializePreferences = async () => {
|
const initializePreferences = async () => {
|
||||||
addDebugInfo('开始初始化偏好设置...')
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const displayTimeLeftValue = await getStoredValue('displayTimeLeft', false)
|
const preferences = await getPreferences()
|
||||||
displayTimeLeft.value = displayTimeLeftValue as boolean
|
displayTimeLeft.value = preferences.displayTimeLeft
|
||||||
const presentLyricsValue = await getStoredValue('presentLyrics', false)
|
presentLyrics.value = preferences.presentLyrics
|
||||||
presentLyrics.value = presentLyricsValue as boolean
|
|
||||||
isLoaded.value = true
|
isLoaded.value = true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addDebugInfo(`❌ 初始化失败: ${error}`)
|
|
||||||
displayTimeLeft.value = false
|
displayTimeLeft.value = false
|
||||||
|
presentLyrics.value = false
|
||||||
isLoaded.value = true
|
isLoaded.value = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 监听变化并保存
|
// 监听变化并保存
|
||||||
watch(displayTimeLeft, async (val) => {
|
watch([displayTimeLeft, presentLyrics], async () => {
|
||||||
if (isLoaded.value) {
|
if (isLoaded.value) {
|
||||||
try {
|
try {
|
||||||
await setStoredValue('displayTimeLeft', val)
|
await savePreferences()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addDebugInfo(`❌ 监听器保存失败: ${error}`)
|
// Silent fail
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
watch(presentLyrics, async (val) => {
|
|
||||||
if (isLoaded.value) {
|
|
||||||
try {
|
|
||||||
await setStoredValue('presentLyrics', val)
|
|
||||||
} catch (error) {
|
|
||||||
addDebugInfo(`❌ 监听器保存失败: ${error}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 手动保存函数(用于调试)
|
|
||||||
const manualSave = async () => {
|
|
||||||
try {
|
|
||||||
await setStoredValue('displayTimeLeft', displayTimeLeft.value)
|
|
||||||
addDebugInfo(`✅ 手动保存成功`)
|
|
||||||
} catch (error) {
|
|
||||||
addDebugInfo(`❌ 手动保存失败: ${error}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取调试信息
|
|
||||||
const getDebugInfo = () => {
|
|
||||||
return {
|
|
||||||
storageType: storageType.value,
|
|
||||||
isLoaded: isLoaded.value,
|
|
||||||
displayTimeLeft: displayTimeLeft.value,
|
|
||||||
logs: debugInfo.value,
|
|
||||||
chromeAvailable: typeof chrome !== 'undefined',
|
|
||||||
chromeStorageAvailable: !!(chrome?.storage?.local),
|
|
||||||
windowChromeAvailable: !!(window.chrome?.storage?.local),
|
|
||||||
localStorageAvailable: typeof localStorage !== 'undefined'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 立即初始化
|
// 立即初始化
|
||||||
initializePreferences()
|
initializePreferences()
|
||||||
|
@ -219,11 +178,10 @@ export const usePreferences = defineStore('preferences', () => {
|
||||||
presentLyrics,
|
presentLyrics,
|
||||||
isLoaded,
|
isLoaded,
|
||||||
storageType,
|
storageType,
|
||||||
debugInfo,
|
|
||||||
initializePreferences,
|
initializePreferences,
|
||||||
getStoredValue,
|
getStoredValue,
|
||||||
setStoredValue,
|
setStoredValue,
|
||||||
manualSave,
|
getPreferences,
|
||||||
getDebugInfo
|
savePreferences
|
||||||
}
|
}
|
||||||
})
|
})
|
Loading…
Reference in New Issue
Block a user