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

24
src/shared/message.ts Normal file
View File

@@ -0,0 +1,24 @@
export type MessageAction =
| "GET_CRAWL_STATE" // 获取爬虫的当前状态
| "START_CRAWL" // 开始爬取
interface BackgroundMessage<T = any> {
action: MessageAction; // 标识要执行的操作
payload?: T; // 附带的数据
}
interface BackgroundResponse<T = any> {
data: T | null
}
/**
* 定义发送给 Background Script 的消息类型
*/
export function sendBackgroundMessage<T>(data: BackgroundMessage): Promise<BackgroundResponse<T>> {
// 检查是否在 Chrome 扩展环境中运行
if (typeof chrome === 'undefined' || !chrome.runtime?.sendMessage) {
return Promise.resolve({data: null});
}
return chrome.runtime.sendMessage(data);
}