1
This commit is contained in:
24
src/shared/message.ts
Normal file
24
src/shared/message.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user