import './style.css'; import type { PinData } from '../../components/pin-collector'; document.querySelector('#app')!.innerHTML = ` `; // 按钮事件 document.getElementById('collectBtn')!.addEventListener('click', async () => { showStatus('收集中...', 'info'); try { const [tab] = await browser.tabs.query({ active: true, currentWindow: true }); if (!tab?.id) { showStatus('无法获取当前标签页', 'error'); return; } const response = await browser.tabs.sendMessage(tab.id, { type: 'COLLECT_PINS', }); if (response?.success) { await loadAndRender(); showStatus(`✅ 已收集 ${response.count} 个 Pin`, 'success'); } } catch { showStatus('请在花瓣网页面使用此功能', 'error'); } }); document.getElementById('clearBtn')!.addEventListener('click', async () => { await browser.storage.local.remove('collectedPins'); renderPins([]); showStatus('已清空', 'info'); }); // 首次打开 popup 时加载并渲染 loadAndRender(); // ---- 辅助函数 ---- async function loadAndRender(): Promise { const result = (await browser.storage.local.get('collectedPins')) as { collectedPins?: PinData[]; }; const pins: PinData[] = result.collectedPins || []; renderPins(pins); } function renderPins(pins: PinData[]): void { const listEl = document.getElementById('pinList')!; if (pins.length === 0) { listEl.innerHTML = '

暂无收集的图片

'; return; } listEl.innerHTML = pins .map( (pin) => `
${escapeHtml(pin.alt)}
${escapeHtml(pin.author || '未知作者')} · ${escapeHtml(pin.time || '')}
${pin.tags.map(t => `#${escapeHtml(t)}`).join(' ')}
查看原图
` ) .join(''); } function showStatus(msg: string, type: 'info' | 'success' | 'error'): void { const statusEl = document.getElementById('status')!; statusEl.textContent = msg; statusEl.className = `status ${type}`; setTimeout(() => (statusEl.textContent = ''), 3000); } function escapeHtml(str: string): string { const div = document.createElement('div'); div.textContent = str; return div.innerHTML; }