c039475119
* docs: 添加组件文档注释和类型导入 refactor: 统一使用 SnackbarOptions 类型 style: 优化导入语句顺序和格式 * refactor: 简化假数据生成器中的faker导入和使用 Co-authored-by: Copilot <copilot@github.com> * feat(form-recognizer): 增强表单识别功能并优化UI交互 - 新增字段类型偏好设置功能,支持按域名保存字段类型 - 重构FieldList组件,改进字段选择和类型修改体验 - 添加字段定位闪烁功能,便于在页面上快速找到对应字段 - 优化表单填充逻辑,支持单个字段覆盖默认填充模式 - 移除独立的侧边栏页面,统一使用主页面组件 - 改进useStorageState钩子,增加加载状态管理和防抖处理 * refactor: 移除未使用的组件文件 * refactor(页面头部): 提取通用 PageHeader 组件并替换各页面头部实现 重构各页面头部为统一的 PageHeader 组件,提高代码复用性和维护性 * style(组件): 调整自动刷新开关和存储选项网格的样式 优化自动刷新开关的文本内边距,重构存储选项网格的布局结构,调整间距和边框样式 * style(ui): 调整时间戳页面和结果视图的样式 - 为时区选择器添加圆角 - 优化结果视图的布局和对齐方式 - 调整结果项的内边距和文本样式 * ci(workflow): 移除Firefox测试以简化CI流程 仅保留Chrome浏览器的构建步骤,减少CI运行时间和资源消耗
110 lines
2.7 KiB
TypeScript
110 lines
2.7 KiB
TypeScript
import { FormFieldInfo, FillMode } from './dummyDataGenerator';
|
|
|
|
/**
|
|
* 字段数据接口(用于消息传递)
|
|
*/
|
|
interface MessageFieldData extends Omit<FormFieldInfo, 'element'> {
|
|
useInvalidData?: boolean;
|
|
}
|
|
|
|
/**
|
|
* 消息动作类型
|
|
*/
|
|
export enum MessageAction {
|
|
// 标签页操作
|
|
RELOAD_TAB = 'reloadTab',
|
|
|
|
// 表单相关操作
|
|
SCAN_FORM_FIELDS = 'scanFormFields',
|
|
FILL_VALID_DATA = 'fillValidData',
|
|
FILL_INVALID_DATA = 'fillInvalidData',
|
|
FILL_SELECTED_FIELDS = 'fillSelectedFields',
|
|
CLEAR_ALL_FIELDS = 'clearAllFields',
|
|
|
|
// 字段高亮操作
|
|
HIGHLIGHT_FIELD = 'highlightField',
|
|
UNHIGHLIGHT_FIELD = 'unhighlightField',
|
|
HIGHLIGHT_ALL_FIELDS = 'highlightAllFields',
|
|
UNHIGHLIGHT_ALL_FIELDS = 'unhighlightAllFields',
|
|
|
|
// 字段定位/闪烁
|
|
FLASH_FIELD = 'flashField',
|
|
}
|
|
|
|
/**
|
|
* 消息载荷接口
|
|
*/
|
|
export interface MessagePayload {
|
|
action: MessageAction | string;
|
|
tabId?: number;
|
|
delay?: number;
|
|
fields?: MessageFieldData[];
|
|
mode?: FillMode;
|
|
includeHidden?: boolean;
|
|
fieldId?: string;
|
|
fieldIds?: string[];
|
|
}
|
|
|
|
/**
|
|
* 消息响应接口
|
|
*/
|
|
export interface MessageResponse {
|
|
success: boolean;
|
|
message?: string;
|
|
fields?: Omit<FormFieldInfo, 'element'>[];
|
|
totalCount?: number;
|
|
validCount?: number;
|
|
hasModal?: boolean;
|
|
}
|
|
|
|
/**
|
|
* 发送消息到内容脚本
|
|
*/
|
|
export async function sendMessageToContent(
|
|
action: MessageAction,
|
|
payload?: Omit<MessagePayload, 'action'>,
|
|
): Promise<MessageResponse> {
|
|
try {
|
|
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
if (!tab?.id) {
|
|
return { success: false, message: '无法获取当前标签页' };
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
chrome.tabs.sendMessage(tab.id!, { action, ...payload }, (response) => {
|
|
if (chrome.runtime.lastError) {
|
|
console.error('消息发送失败:', chrome.runtime.lastError);
|
|
resolve({ success: false, message: '无法连接到页面,请确保页面已加载' });
|
|
} else {
|
|
resolve((response as MessageResponse) || { success: false, message: '未收到响应' });
|
|
}
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.error('获取标签页失败:', error);
|
|
return { success: false, message: '无法获取当前标签页' };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 注入内容脚本
|
|
*/
|
|
export async function injectContentScript(): Promise<boolean> {
|
|
try {
|
|
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
|
|
if (!tab?.id) {
|
|
return false;
|
|
}
|
|
|
|
// 尝试注入内容脚本
|
|
await chrome.scripting.executeScript({
|
|
target: { tabId: tab.id },
|
|
files: ['/content-scripts/content.js'],
|
|
});
|
|
return true;
|
|
} catch (error) {
|
|
console.error('注入内容脚本失败:', error);
|
|
return false;
|
|
}
|
|
}
|