0379f96e80
* feat(formRecognizer): 添加表单识别功能及相关组件 添加表单识别功能,包括以下内容: 1. 在路由配置中添加表单识别页面 2. 实现表单识别页面和侧边栏面板 3. 添加表单数据生成工具类 4. 实现与内容脚本的通信机制 5. 添加faker-js依赖用于生成测试数据 6. 支持不同入口点(popup/sidepanel)的组件渲染 * refactor(消息通信): 重构消息通信机制并集中管理消息协议 将分散的消息协议和通信逻辑集中到 utils/messages.ts 中 移除旧的 messages.tsx 文件并更新相关引用 添加消息动作枚举和类型定义,提高类型安全性 优化内容脚本注入失败时的处理逻辑
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import '../.wxt/types/imports.d.ts';
|
|
import { browser } from 'wxt/browser';
|
|
import { type MessagePayload, type MessageResponse } from '@/utils/messages';
|
|
|
|
export default defineBackground(() => {
|
|
// 监听扩展图标点击事件,打开侧边栏
|
|
browser.action.onClicked.addListener(async (tab) => {
|
|
if (tab.id) {
|
|
try {
|
|
await browser.sidePanel.open({ tabId: tab.id });
|
|
} catch (err) {
|
|
console.error('Failed to open side panel:', err);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 监听扩展安装或更新事件
|
|
browser.runtime.onInstalled.addListener(async ({ reason }) => {
|
|
if (reason === 'install') {
|
|
console.log('Extension installed for the first time');
|
|
} else if (reason === 'update') {
|
|
console.log('Extension updated to a new version');
|
|
}
|
|
});
|
|
|
|
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
|
|
if (changeInfo.status === 'complete') {
|
|
console.log('加载完成的 Tab ID:', tabId);
|
|
}
|
|
});
|
|
|
|
// 监听来自 popup/sidepanel 的消息
|
|
chrome.runtime.onMessage.addListener(
|
|
(message: MessagePayload, sender, sendResponse: (response: MessageResponse) => void) => {
|
|
try {
|
|
// 处理跨标签页的消息转发
|
|
if (sender.tab) {
|
|
// 从内容脚本或弹出页面发送的消息
|
|
console.log('收到来自标签页的消息:', message.action);
|
|
sendResponse({ success: true, message: '消息已收到' });
|
|
} else {
|
|
// 从扩展其他部分发送的消息
|
|
console.log('收到来自扩展的消息:', message.action);
|
|
sendResponse({ success: true, message: '消息已收到' });
|
|
}
|
|
} catch (error) {
|
|
console.error('处理消息失败:', error);
|
|
sendResponse({ success: false, message: '处理消息失败' });
|
|
}
|
|
},
|
|
);
|
|
});
|