This commit is contained in:
zhu
2026-05-09 17:16:08 +08:00
parent 6587b0c1d9
commit 30f9467cc8
14 changed files with 256 additions and 181 deletions

View File

@@ -225,4 +225,4 @@ async function processTable(config: PlatformFieldConfig, rootDom: ParentNode) {
}
return allTableData;
}
}

View File

@@ -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});
}
}

View File

@@ -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);