diff --git a/components/pin-collector.ts b/components/pin-collector.ts index b5be84e..f33539c 100644 --- a/components/pin-collector.ts +++ b/components/pin-collector.ts @@ -5,6 +5,8 @@ export interface PinData { /** 相对路径,如 "/pins/7118240882" */ url: string; + /** 图片缩略图地址 */ + imgSmallSrc: string; /** 图片源地址 */ imgSrc: string; /** 图片原始宽度 */ @@ -120,9 +122,12 @@ export class PinCollector { const width = parseFloat(img.getAttribute('width') || '0'); const height = parseFloat(img.getAttribute('height') || '0'); + const originalSrc = img.src.replace(/_fw\d+(webp)?/, ''); + return { url: href, - imgSrc: img.src, + imgSmallSrc: img.src, + imgSrc: originalSrc, imgWidth: width, imgHeight: height, alt: altText, diff --git a/entrypoints/content.ts b/entrypoints/content.ts index 3f63259..50e05e7 100644 --- a/entrypoints/content.ts +++ b/entrypoints/content.ts @@ -9,15 +9,12 @@ export default defineContentScript({ const collector = new PinCollector(); // 首次加载自动收集并存储 - const pins = collector.collect(); - if (pins.length > 0) { - collector.saveToStorage(); - } + // 允许 popup 主动触发重新收集 browser.runtime.onMessage.addListener((msg, sender, sendResponse) => { if (msg.type === 'COLLECT_PINS') { - const updatedPins = collector.collect(); - sendResponse({ success: true, count: updatedPins.length }); + const pins = collector.collect(); + sendResponse({ success: true, count: pins.length, pins }); return true; // 异步响应 } }); diff --git a/entrypoints/popup/main.ts b/entrypoints/popup/main.ts index d1229b0..3821bfd 100644 --- a/entrypoints/popup/main.ts +++ b/entrypoints/popup/main.ts @@ -26,11 +26,13 @@ document.getElementById('collectBtn')!.addEventListener('click', async () => { type: 'COLLECT_PINS', }); if (response?.success) { - await loadAndRender(); + await browser.storage.local.set({ collectedPins: response.pins }); + renderPins(response.pins as PinData[]); showStatus(`✅ 已收集 ${response.count} 个 Pin`, 'success'); } - } catch { - showStatus('请在花瓣网页面使用此功能', 'error'); + } catch (e) { + const errMsg = e instanceof Error ? e.message : String(e); + showStatus(`错误: ${errMsg}`, 'error'); } }); @@ -64,11 +66,12 @@ function renderPins(pins: PinData[]): void { .map( (pin) => `
- ${escapeHtml(pin.alt)} + ${escapeHtml(pin.alt)}
${escapeHtml(pin.author || '未知作者')} · ${escapeHtml(pin.time || '')}
${pin.tags.map(t => `#${escapeHtml(t)}`).join(' ')}
- 查看原图 + 查看详情 + 查看原图
` @@ -80,7 +83,7 @@ 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); + setTimeout(() => (statusEl.textContent = ''), 8000); } function escapeHtml(str: string): string {