be5e2f02ee
* feat: optimize popup standalone window layout and enhance storage cleaner synchronization * docs: 更新README文档并删除过时文件 - 更新README文档,添加项目结构、功能特性和路由系统等详细信息 - 删除不再使用的文档文件,包括CLAUDE.md、GEMINI.md和多个设计规范文档 - 清理项目中的过时配置文件和计划文档 * feat: 添加 Vitest 测试框架和组件测试 - 添加 Vitest 配置 (vitest.config.ts, vitest.setup.ts) - 创建组件测试: Button, ToolCard, GlobalSnackbar, TopBar, RouterContainer, StorageCleanerConfirm - 创建工具测试: routes, storageCleaner - 修复 background.ts 监听器参数问题 - 修复 options/App.tsx 硬编码默认值 - 更新 lint-staged.config.mjs (添加 .mjs 支持, 添加 --no-warn-ignored) - 更新 tsconfig.json (添加测试类型支持, 移除测试文件排除) - 更新 package.json (添加测试脚本和依赖) * fix: 修复 StorageCleanerPage Chrome API 监听器内存泄漏 使用 useRef 模式存储 loadInfo 函数引用,避免依赖数组变化导致的监听器重复注册问题 * refactor(popup): 优化 OpenUrl 页面样式和导航逻辑 重构 OpenUrl 页面输入框样式,改进聚焦状态效果 移除 RouterProvider 依赖,直接通过存储设置侧边栏路由 在 OpenUrlViewer 页面添加加载状态指示器和错误处理 监听存储变化实现 URL 自动更新 * feat(ui): 优化存储清理页面UI和交互效果 重构存储清理页面组件,增强视觉层次和交互体验: - 使用新的错误提示样式和布局 - 改进选项卡片样式,增加悬停动画和选中状态 - 调整整体间距和排版,提升视觉一致性 - 添加微交互效果如悬停缩放和阴影 - 优化颜色方案和过渡动画 - 统一组件尺寸和字体层级 * feat: 添加二维码工具页面,支持URL转二维码和二维码解析功能 * chore: update package-lock.json (npm audit fix) * refactor(主题): 将页面样式抽离到统一配置文件 将各页面的颜色和样式配置抽离到config/pageTheme.ts中统一管理 优化测试用例中使用each替代forEach 更新路由测试以包含新的qrCode页面 * feat(二维码页面): 添加复制二维码功能并优化样式 添加复制二维码到剪贴板的功能,并调整按钮布局和样式。同时将 ContentCopyIcon 导入位置调整到其他图标导入之后,并修复缩进问题。在 tsconfig.json 中添加 vitest/globals 类型支持。 * feat(theme): 为所有页面添加统一的背景色和卡片背景色 为应用中的所有页面添加了统一的浅灰色背景(#f5f5f5)和白色卡片背景(#ffffff),以保持视觉一致性。修改了ToolCard组件以支持自定义卡片背景色,并更新了所有相关页面使用新的主题配置。 * feat: 添加复制按钮组件并优化现有复制功能 refactor(utils): 创建剪贴板工具函数 feat(components): 新增可复用的CopyButton组件 refactor(pages): 在QrCodePage和TimestampPage中使用CopyButton style: 格式化代码并调整部分样式 * refactor(存储): 统一qrCode相关存储键名 将'qrCode/expanded'重命名为'qrCode/qrExpanded'以保持命名一致性 * feat: 添加二维码工具功能并更新项目配置 - 新增二维码工具页面及相关组件和工具函数 - 添加 MIT 许可证文件 - 更新 package.json 配置为公开项目 - 更新 README 文档说明新功能
382 lines
12 KiB
TypeScript
382 lines
12 KiB
TypeScript
import { useState, useEffect, useCallback, Fragment } from 'react';
|
|
import {
|
|
Box,
|
|
TextField,
|
|
Alert,
|
|
List,
|
|
ListItem,
|
|
IconButton,
|
|
Typography,
|
|
Divider,
|
|
Container,
|
|
Stack,
|
|
alpha,
|
|
Tooltip,
|
|
} from '@mui/material';
|
|
import DeleteIcon from '@mui/icons-material/Delete';
|
|
import OpenInNewIcon from '@mui/icons-material/OpenInNew';
|
|
import VisibilityIcon from '@mui/icons-material/Visibility';
|
|
import AddIcon from '@mui/icons-material/Add';
|
|
import LanguageIcon from '@mui/icons-material/Language';
|
|
import LinkIcon from '@mui/icons-material/Link';
|
|
import Button from '@/components/Button';
|
|
import GlobalSnackbar, { useSnackbar } from '@/components/GlobalSnackbar';
|
|
import { storageUtil } from '@/utils/chromeStorage';
|
|
import type { OpenUrlPreferences, OpenUrlEntry } from '@/types/storage';
|
|
import { openUrlPageStyles, dashboardPageStyles } from '@/config/pageTheme';
|
|
|
|
const THEME_COLOR = openUrlPageStyles.themeColor;
|
|
|
|
const DEFAULT_PREFERENCES: OpenUrlPreferences = {
|
|
entries: [],
|
|
};
|
|
|
|
export default function OpenUrlPage() {
|
|
const [entries, setEntries] = useState<OpenUrlEntry[]>(DEFAULT_PREFERENCES.entries);
|
|
const [newName, setNewName] = useState<string>('');
|
|
const [newUrl, setNewUrl] = useState<string>('');
|
|
const [isLoaded, setIsLoaded] = useState(false);
|
|
const { snackbarProps, showMessage } = useSnackbar();
|
|
|
|
const showMixedContentWarning =
|
|
newUrl.startsWith('http://') && !newUrl.includes('localhost') && !newUrl.includes('127.0.0.1');
|
|
|
|
const isValidUrl = (url: string) => {
|
|
if (!url.trim()) return false;
|
|
try {
|
|
new URL(url);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const loadPreferences = async () => {
|
|
try {
|
|
const saved = await storageUtil.get('openUrl/preferences', DEFAULT_PREFERENCES);
|
|
if (saved && saved.entries) {
|
|
setEntries(saved.entries);
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to load Open Url preferences:', error);
|
|
} finally {
|
|
setIsLoaded(true);
|
|
}
|
|
};
|
|
loadPreferences();
|
|
}, []);
|
|
|
|
const savePreferences = useCallback(() => {
|
|
const preferences: OpenUrlPreferences = { entries };
|
|
storageUtil.set('openUrl/preferences', preferences).catch((error) => {
|
|
console.error('Failed to save Open Url preferences:', error);
|
|
});
|
|
}, [entries]);
|
|
|
|
useEffect(() => {
|
|
if (!isLoaded) return;
|
|
const timer = setTimeout(() => {
|
|
savePreferences();
|
|
}, 500);
|
|
return () => clearTimeout(timer);
|
|
}, [entries, isLoaded, savePreferences]);
|
|
|
|
const handleAddEntry = () => {
|
|
if (!newName.trim()) {
|
|
showMessage('请输入名称', { severity: 'error' });
|
|
return;
|
|
}
|
|
if (!isValidUrl(newUrl)) {
|
|
showMessage('请输入有效的 URL', { severity: 'error' });
|
|
return;
|
|
}
|
|
|
|
setEntries([...entries, { name: newName.trim(), url: newUrl.trim() }]);
|
|
setNewName('');
|
|
setNewUrl('');
|
|
showMessage('添加成功', { severity: 'success' });
|
|
};
|
|
|
|
const handleDeleteEntry = (index: number) => {
|
|
const newEntries = [...entries];
|
|
newEntries.splice(index, 1);
|
|
setEntries(newEntries);
|
|
showMessage('删除成功', { severity: 'success' });
|
|
};
|
|
|
|
const handleOpenInSidebar = async (entry: OpenUrlEntry) => {
|
|
try {
|
|
// 存储目标 URL
|
|
await storageUtil.set('openUrl/currentUrl', entry.url);
|
|
// 直接设置侧边栏的路由,而不是通过 syncNavigation 影响弹窗路由
|
|
await storageUtil.set('app/sidepanelRoute', 'openUrlViewer');
|
|
|
|
const [currentTab] = await chrome.tabs.query({
|
|
active: true,
|
|
currentWindow: true,
|
|
});
|
|
const tabId = currentTab.id;
|
|
if (!tabId) {
|
|
showMessage('无法获取当前标签页', { severity: 'error' });
|
|
return;
|
|
}
|
|
|
|
await chrome.sidePanel.setOptions({
|
|
tabId,
|
|
path: 'sidepanel.html',
|
|
enabled: true,
|
|
});
|
|
await chrome.sidePanel.open({ windowId: currentTab.windowId });
|
|
|
|
// 仅当在 Popup 中时才关闭窗口,防止在侧边栏内点击预览时导致侧边栏关闭
|
|
if (window.location.pathname.includes('popup.html')) {
|
|
window.close();
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to open side panel:', error);
|
|
showMessage(`打开失败: ${(error as Error).message}`, { severity: 'error' });
|
|
}
|
|
};
|
|
|
|
const handleOpenInNewTab = (entry: OpenUrlEntry) => {
|
|
chrome.tabs.create({ url: entry.url });
|
|
window.close();
|
|
};
|
|
|
|
return (
|
|
<Box sx={{ bgcolor: dashboardPageStyles.backgroundColor, minHeight: '100%', pb: 3 }}>
|
|
<Container sx={{ py: 2 }}>
|
|
{/* Header */}
|
|
<Stack direction="row" spacing={1.5} alignItems="center" sx={{ mb: 2.5 }}>
|
|
<Box
|
|
sx={{
|
|
p: 1,
|
|
borderRadius: 2.5,
|
|
bgcolor: alpha(THEME_COLOR, 0.1),
|
|
color: THEME_COLOR,
|
|
display: 'flex',
|
|
}}
|
|
>
|
|
<LanguageIcon sx={{ fontSize: 20 }} />
|
|
</Box>
|
|
<Box sx={{ flex: 1 }}>
|
|
<Typography
|
|
variant="subtitle1"
|
|
fontWeight={900}
|
|
sx={{ letterSpacing: '-0.5px', lineHeight: 1.2 }}
|
|
>
|
|
URL 工具
|
|
</Typography>
|
|
<Typography variant="caption" color="text.secondary" sx={{ fontWeight: 600 }}>
|
|
快速打开 URL 或复制链接
|
|
</Typography>
|
|
</Box>
|
|
</Stack>
|
|
|
|
{/* Form Section */}
|
|
<Box
|
|
sx={{
|
|
bgcolor: 'background.paper',
|
|
p: 2,
|
|
borderRadius: 4,
|
|
border: '1px solid',
|
|
borderColor: 'grey.100',
|
|
mb: 3,
|
|
boxShadow: '0 4px 12px rgba(0,0,0,0.02)',
|
|
}}
|
|
>
|
|
<Stack spacing={2}>
|
|
<TextField
|
|
label="环境名称"
|
|
placeholder="例如: 本地文档"
|
|
value={newName}
|
|
onChange={(e) => setNewName(e.target.value)}
|
|
fullWidth
|
|
variant="outlined"
|
|
sx={openUrlPageStyles.INPUT_STYLE}
|
|
slotProps={{
|
|
inputLabel: {
|
|
shrink: true,
|
|
},
|
|
}}
|
|
/>
|
|
<TextField
|
|
label="目标 URL"
|
|
placeholder="例如: http://localhost:8000/docs"
|
|
value={newUrl}
|
|
onChange={(e) => setNewUrl(e.target.value)}
|
|
fullWidth
|
|
variant="outlined"
|
|
sx={openUrlPageStyles.INPUT_STYLE}
|
|
slotProps={{
|
|
inputLabel: {
|
|
shrink: true,
|
|
},
|
|
}}
|
|
/>
|
|
|
|
{showMixedContentWarning && (
|
|
<Alert
|
|
severity="warning"
|
|
sx={{
|
|
borderRadius: 3,
|
|
'& .MuiAlert-message': { fontSize: '0.7rem', fontWeight: 600, lineHeight: 1.4 },
|
|
}}
|
|
>
|
|
混合内容警告:当前 HTTPS 页面无法加载 HTTP 资源。
|
|
</Alert>
|
|
)}
|
|
|
|
<Button
|
|
variant="contained"
|
|
onClick={handleAddEntry}
|
|
disabled={!newName.trim() || !isValidUrl(newUrl)}
|
|
fullWidth
|
|
startIcon={<AddIcon />}
|
|
sx={{
|
|
py: 1.2,
|
|
borderRadius: 4,
|
|
bgcolor: THEME_COLOR,
|
|
fontWeight: 800,
|
|
boxShadow: 'none',
|
|
'&:hover': {
|
|
bgcolor: alpha(THEME_COLOR, 0.85),
|
|
boxShadow: `0 8px 24px ${alpha(THEME_COLOR, 0.2)}`,
|
|
},
|
|
}}
|
|
>
|
|
添加快捷方式
|
|
</Button>
|
|
</Stack>
|
|
</Box>
|
|
|
|
{/* List Section */}
|
|
<Box>
|
|
<Typography
|
|
variant="caption"
|
|
sx={{ color: 'text.secondary', fontWeight: 800, px: 1, mb: 1, display: 'block' }}
|
|
>
|
|
已保存的快捷方式 ({entries.length})
|
|
</Typography>
|
|
|
|
{entries.length === 0 ? (
|
|
<Box
|
|
sx={{
|
|
textAlign: 'center',
|
|
py: 4,
|
|
bgcolor: 'grey.50',
|
|
borderRadius: 4,
|
|
border: '1px dashed',
|
|
borderColor: 'grey.200',
|
|
}}
|
|
>
|
|
<LinkIcon sx={{ color: 'grey.300', fontSize: 40, mb: 1 }} />
|
|
<Typography
|
|
variant="caption"
|
|
color="text.disabled"
|
|
sx={{ display: 'block', fontWeight: 600 }}
|
|
>
|
|
暂无快捷方式,请在上方添加
|
|
</Typography>
|
|
</Box>
|
|
) : (
|
|
<List
|
|
disablePadding
|
|
sx={{
|
|
bgcolor: 'background.paper',
|
|
borderRadius: 4,
|
|
border: '1px solid',
|
|
borderColor: 'grey.100',
|
|
overflow: 'hidden',
|
|
}}
|
|
>
|
|
{entries.map((entry, index) => (
|
|
<Fragment key={index}>
|
|
<ListItem
|
|
sx={{
|
|
px: 2,
|
|
py: 1.5,
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
gap: 2,
|
|
transition: 'background-color 0.2s',
|
|
'&:hover': { bgcolor: 'grey.50' },
|
|
}}
|
|
>
|
|
<Box sx={{ flex: 1, minWidth: 0 }}>
|
|
<Typography
|
|
variant="body2"
|
|
sx={{ fontWeight: 800, color: 'text.primary' }}
|
|
noWrap
|
|
>
|
|
{entry.name}
|
|
</Typography>
|
|
<Typography
|
|
variant="caption"
|
|
color="text.secondary"
|
|
noWrap
|
|
sx={{
|
|
fontSize: '0.65rem',
|
|
fontWeight: 500,
|
|
display: 'block',
|
|
mt: 0.2,
|
|
fontFamily: 'monospace',
|
|
}}
|
|
>
|
|
{entry.url}
|
|
</Typography>
|
|
</Box>
|
|
<Stack direction="row" spacing={0.5}>
|
|
<Tooltip title="在侧边栏预览">
|
|
<IconButton
|
|
size="small"
|
|
onClick={() => handleOpenInSidebar(entry)}
|
|
sx={{
|
|
color: THEME_COLOR,
|
|
bgcolor: alpha(THEME_COLOR, 0.05),
|
|
'&:hover': { bgcolor: THEME_COLOR, color: '#fff' },
|
|
}}
|
|
>
|
|
<VisibilityIcon sx={{ fontSize: 16 }} />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title="新标签页打开">
|
|
<IconButton
|
|
size="small"
|
|
onClick={() => handleOpenInNewTab(entry)}
|
|
sx={{
|
|
color: 'grey.500',
|
|
bgcolor: 'grey.100',
|
|
'&:hover': { bgcolor: 'grey.600', color: '#fff' },
|
|
}}
|
|
>
|
|
<OpenInNewIcon sx={{ fontSize: 16 }} />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title="删除">
|
|
<IconButton
|
|
size="small"
|
|
onClick={() => handleDeleteEntry(index)}
|
|
sx={{
|
|
color: 'error.main',
|
|
'&:hover': { color: 'error.dark', bgcolor: alpha('#f44336', 0.05) },
|
|
}}
|
|
>
|
|
<DeleteIcon sx={{ fontSize: 16 }} />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</Stack>
|
|
</ListItem>
|
|
{index < entries.length - 1 && <Divider sx={{ mx: 2, borderColor: 'grey.50' }} />}
|
|
</Fragment>
|
|
))}
|
|
</List>
|
|
)}
|
|
</Box>
|
|
</Container>
|
|
<GlobalSnackbar {...snackbarProps} />
|
|
</Box>
|
|
);
|
|
}
|