diff --git a/src/i18n/translations.ts b/src/i18n/translations.ts index fa4c56a..fddeb81 100644 --- a/src/i18n/translations.ts +++ b/src/i18n/translations.ts @@ -62,8 +62,8 @@ const translations: Record> = { 'options.fields.theme.dark': '深色主题', 'options.fields.enableHistory.label': '启用搜索历史', 'options.fields.enableHistory.description': '保存搜索记录以便后续查看和重用', - 'options.fields.autoOpen.label': '自动在新标签页打开搜索结果', - 'options.fields.autoOpen.description': '执行搜索时自动在新标签页打开结果页面', + 'options.fields.autoOpen.label': '在新标签页打开搜索结果', + 'options.fields.autoOpen.description': '开启时在新标签页打开搜索结果,关闭时在当前标签页打开', 'options.fields.historyLimit.label': '历史记录限制', 'options.fields.historyLimit.description': '最多保存的历史记录数量(建议不超过1000条)', 'options.actions.export': '导出数据', @@ -75,8 +75,10 @@ const translations: Record> = { 'options.actions.saving': '保存中...', 'options.messages.saveSuccess': '设置已保存', 'options.messages.saveFailure': '保存失败', + 'options.messages.resetSuccess': '设置已重置为默认值并自动保存', + 'options.messages.resetFailure': '重置设置失败', 'options.status.loadError': '加载设置失败', - 'options.confirm.reset': '确定要重置所有设置吗?这将清除所有自定义配置。', + 'options.confirm.reset': '确定要重置所有设置吗?\n\n这将恢复默认配置,但会保留您的搜索历史记录。\n操作将立即生效并自动保存。', 'options.confirm.clearAll': '确定要清除所有数据吗?这将删除搜索历史、设置等所有信息,且无法恢复。', 'options.confirm.clearAllSecond': '请再次确认:此操作不可撤销!', 'options.alert.importSuccess': '数据导入成功!页面将重新加载。', @@ -150,8 +152,8 @@ const translations: Record> = { 'options.fields.theme.dark': 'Dark theme', 'options.fields.enableHistory.label': 'Enable search history', 'options.fields.enableHistory.description': 'Keep recent searches for quick access.', - 'options.fields.autoOpen.label': 'Open results in a new tab automatically', - 'options.fields.autoOpen.description': 'Open the search results in a new tab when executing a search.', + 'options.fields.autoOpen.label': 'Open search results in new tab', + 'options.fields.autoOpen.description': 'When enabled, opens results in a new tab; when disabled, opens in current tab', 'options.fields.historyLimit.label': 'History limit', 'options.fields.historyLimit.description': 'Maximum number of saved history entries (recommended ≤ 1000).', 'options.actions.export': 'Export data', @@ -163,8 +165,10 @@ const translations: Record> = { 'options.actions.saving': 'Saving...', 'options.messages.saveSuccess': 'Settings saved', 'options.messages.saveFailure': 'Failed to save settings', + 'options.messages.resetSuccess': 'Settings have been reset to defaults and automatically saved', + 'options.messages.resetFailure': 'Failed to reset settings', 'options.status.loadError': 'Failed to load settings', - 'options.confirm.reset': 'Reset all settings? This will remove your custom configuration.', + 'options.confirm.reset': 'Reset all settings?\n\nThis will restore default configuration but keep your search history.\nChanges will take effect immediately and be saved automatically.', 'options.confirm.clearAll': 'Are you sure you want to clear all data? This will delete history, settings, and cannot be undone.', 'options.confirm.clearAllSecond': 'Please confirm again: this action cannot be undone!', 'options.alert.importSuccess': 'Data imported successfully! The page will reload.', diff --git a/src/options/App.tsx b/src/options/App.tsx index 0099480..70bae56 100644 --- a/src/options/App.tsx +++ b/src/options/App.tsx @@ -14,13 +14,11 @@ function App() { const [message, setMessage] = useState<{ type: 'success' | 'error'; key: string } | null>(null) // 从存储中加载设置 - const { data: storedSettings, save: saveSettings } = useStorage('user_settings') + const { data: storedSettings, save: saveSettings } = useStorage('user_settings', DEFAULT_SETTINGS) useEffect(() => { - if (storedSettings) { - setSettings(storedSettings) - setLoading(false) - } + setSettings(storedSettings || DEFAULT_SETTINGS) + setLoading(false) }, [storedSettings]) const language = settings?.language ?? 'zh-CN' @@ -50,9 +48,30 @@ function App() { } // 重置设置 - const handleResetSettings = () => { + const handleResetSettings = async () => { if (confirm(translate(language, 'options.confirm.reset'))) { - setSettings({ ...DEFAULT_SETTINGS }) + const defaultSettings = { ...DEFAULT_SETTINGS } + setSettings(defaultSettings) + + // 自动保存重置后的设置 + setSaving(true) + setMessage(null) + + try { + const success = await saveSettings(defaultSettings) + if (success) { + setMessage({ type: 'success', key: 'options.messages.resetSuccess' }) + } else { + setMessage({ type: 'error', key: 'options.messages.resetFailure' }) + } + } catch (error) { + setMessage({ type: 'error', key: 'options.messages.resetFailure' }) + } finally { + setSaving(false) + } + + // 3秒后清除消息 + setTimeout(() => setMessage(null), 3000) } } diff --git a/src/popup/App.tsx b/src/popup/App.tsx index 72497a8..e9d9f5b 100644 --- a/src/popup/App.tsx +++ b/src/popup/App.tsx @@ -9,6 +9,7 @@ import { ThemeProvider } from '@/hooks/useTheme' import { SearchAdapterFactory } from '@/services/adapters' import { TranslationProvider, useTranslation } from '@/i18n' import type { SearchParams, SearchHistory as SearchHistoryType, UserSettings, ValidationResult } from '@/types' +import { DEFAULT_SETTINGS } from '@/types' function App() { const [searchParams, setSearchParams] = useState({ @@ -26,17 +27,16 @@ function App() { const [settings, setSettings] = useState(null) // 从存储中加载用户设置和历史记录 - const { data: storedSettings } = useStorage('user_settings') + const { data: storedSettings } = useStorage('user_settings', DEFAULT_SETTINGS) const { data: storedHistory } = useStorage('search_history') useEffect(() => { - if (storedSettings) { - setSettings(storedSettings) - setSearchParams(prev => ({ - ...prev, - engine: storedSettings.defaultEngine - })) - } + const effectiveSettings = storedSettings || DEFAULT_SETTINGS + setSettings(effectiveSettings) + setSearchParams(prev => ({ + ...prev, + engine: effectiveSettings.defaultEngine + })) }, [storedSettings]) useEffect(() => { @@ -101,8 +101,21 @@ function App() { }) } - // 在新标签页打开搜索结果 - chrome.tabs.create({ url: searchUrl }) + // 根据用户设置选择打开方式 + if (settings?.autoOpenInNewTab) { + // 在新标签页打开搜索结果 + chrome.tabs.create({ url: searchUrl }) + } else { + // 在当前标签页打开搜索结果 + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + if (tabs[0]?.id) { + chrome.tabs.update(tabs[0].id, { url: searchUrl }) + } else { + // 如果无法获取当前标签页,回退到创建新标签页 + chrome.tabs.create({ url: searchUrl }) + } + }) + } // 关闭弹窗 window.close()