24 lines
704 B
TypeScript
24 lines
704 B
TypeScript
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);
|
|
} |