diff --git a/manifest.config.ts b/manifest.config.ts index f5ab69b..e260b6f 100644 --- a/manifest.config.ts +++ b/manifest.config.ts @@ -27,4 +27,9 @@ export default defineManifest({ service_worker: 'src/background/index.ts', type: 'module', }, + externally_connectable: { + matches: [ + "http://localhost:3000/*", + ] + } }); diff --git a/src/background/domScraper.ts b/src/background/domScraper.ts index 767111c..7714c8a 100644 --- a/src/background/domScraper.ts +++ b/src/background/domScraper.ts @@ -225,4 +225,4 @@ async function processTable(config: PlatformFieldConfig, rootDom: ParentNode) { } return allTableData; -} +} \ No newline at end of file diff --git a/src/background/index.ts b/src/background/index.ts index 3165e36..deefa26 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,35 +1,41 @@ -import { handleBackgroundCommand, handleInstalled, handleStartup, handleWindowRemoved } from './service'; -import type { BackgroundCommand } from './types'; +import {handleBackgroundCommand, handleWindowRemoved} from './service'; +import type {BackgroundCommand} from './types'; -chrome.runtime.onInstalled.addListener(() => { - void handleInstalled(); -}); -chrome.runtime.onStartup.addListener(() => { - void handleStartup(); -}); chrome.runtime.onMessage.addListener((message: BackgroundCommand, _sender, sendResponse) => { - void handleBackgroundMessage(message, sendResponse); - return true; + void handleBackgroundMessage(message, sendResponse); + return true; }); chrome.windows.onRemoved.addListener((windowId) => { - void handleWindowRemoved(windowId); + void handleWindowRemoved(windowId); +}); + +chrome.runtime.onMessageExternal.addListener((message, sender, sendResponse) => { + if (message.type === "STORE_AI_PING") { + // 返回版本号等信息 + sendResponse({ + success: true, + version: chrome.runtime.getManifest().version + }); + } + // 注意:外部消息处理必须返回 true 才能支持异步 sendResponse + return true; }); /** * 统一包装后台消息处理,确保异步错误能回给调用方。 */ async function handleBackgroundMessage( - message: BackgroundCommand, - sendResponse: (response?: unknown) => void, + message: BackgroundCommand, + sendResponse: (response?: unknown) => void, ) { - try { - const result = await handleBackgroundCommand(message); - sendResponse(result); - } catch (error: unknown) { - const messageText = error instanceof Error ? error.message : 'Unknown error'; - sendResponse({ ok: false, error: messageText }); - } + try { + const result = await handleBackgroundCommand(message); + sendResponse(result); + } catch (error: unknown) { + const messageText = error instanceof Error ? error.message : 'Unknown error'; + sendResponse({ok: false, error: messageText}); + } } diff --git a/src/background/service/crawlTask.ts b/src/background/service/crawlTask.ts index 3d72f5f..cf6ed73 100644 --- a/src/background/service/crawlTask.ts +++ b/src/background/service/crawlTask.ts @@ -1,11 +1,12 @@ import { getPlatformById } from '@/config/platforms'; import type { CrawlPauseInfo, CrawlProgressStep, CrawlTaskState, PlatformConfig, PlatformStepConfig } from '@/types'; +import type { DomScrapeResult } from '../domScraper'; import type { CrawlStateResponse } from '../types'; import { getCrawlTaskState, setCrawlTaskState, updateCrawlTaskState } from './taskState'; interface PageRunnerResponse { ok: boolean; - data?: any | null; + data?: DomScrapeResult | null; interrupt?: CrawlPauseInfo; error?: string; } @@ -225,10 +226,6 @@ async function runCrawlSteps(platform: PlatformConfig, initialState: CrawlTaskSt status: 'completed', steps: state.steps.map((step) => (step.status === 'running' ? { ...step, status: 'success' } : step)), })); - - await chrome.windows.remove(initialState.windowId).catch((error: unknown) => { - console.warn('[crawl] 爬取完成后关闭窗口失败', error); - }); } catch (error: unknown) { console.error('[crawl] 执行失败', error); diff --git a/src/config/platforms.ts b/src/config/platforms.ts index 34d0867..a359e26 100644 --- a/src/config/platforms.ts +++ b/src/config/platforms.ts @@ -1,6 +1,6 @@ import type {PlatformConfig} from '@/types'; -export const PLATFORM_CONFIGS: PlatformConfig[] = [ +export const platformConfigs: PlatformConfig[] = [ { id: 'Shopee', name: 'Shopee 后台', @@ -306,5 +306,5 @@ export const PLATFORM_CONFIGS: PlatformConfig[] = [ * 根据平台 ID 返回对应的平台抓取配置。 */ export function getPlatformById(platformId: string) { - return PLATFORM_CONFIGS.find((item) => item.id === platformId) ?? null; + return platformConfigs.find((item) => item.id === platformId) ?? null; } diff --git a/src/content/pageRunner.ts b/src/content/pageRunner.ts index e45d187..6c9f27a 100644 --- a/src/content/pageRunner.ts +++ b/src/content/pageRunner.ts @@ -1,4 +1,4 @@ -import { processFields} from '@/background/domScraper'; +import { processFields, type DomScrapeResult } from '@/background/domScraper'; import type { CrawlPauseInfo, PlatformFieldConfig } from '@/types'; interface ScrapeStepMessage { @@ -17,7 +17,7 @@ type PageRunnerMessage = ScrapeStepMessage | CheckInterruptMessage; interface PageRunnerResponse { ok: boolean; - data?: any | null; + data?: DomScrapeResult | null; interrupt?: CrawlPauseInfo; error?: string; } diff --git a/src/popup/App.vue b/src/popup/App.vue index 5f0964d..08c318e 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -1,118 +1,44 @@