|
@@ -18,13 +18,13 @@ class SidebarManager {
|
|
|
this.currentUrl = window.location.href;
|
|
|
this.currentTitle = document.title;
|
|
|
this.init();
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化侧边栏
|
|
|
*/
|
|
|
async init() {
|
|
|
+
|
|
|
try {
|
|
|
await this.checkAndRestoreState();
|
|
|
this.setupEventListeners();
|
|
@@ -77,8 +77,15 @@ class SidebarManager {
|
|
|
childList: true,
|
|
|
}
|
|
|
);
|
|
|
- const simulateUserInput = (element, value) => {
|
|
|
+
|
|
|
+ const simulateUserInput = async (element, value) => {
|
|
|
// 设置值
|
|
|
+ // if (element.tagName.toLowerCase() === 'textarea') {
|
|
|
+ // element.focus()
|
|
|
+ // element.value = value
|
|
|
+ // element.blur()
|
|
|
+ // return
|
|
|
+ // }
|
|
|
element.value = value;
|
|
|
|
|
|
// 创建并触发 input 事件
|
|
@@ -88,11 +95,33 @@ class SidebarManager {
|
|
|
// 创建并触发 change 事件
|
|
|
const changeEvent = new Event('change', { bubbles: true });
|
|
|
element.dispatchEvent(changeEvent);
|
|
|
-
|
|
|
+
|
|
|
// 可选:如果表单使用了 React/Vue 等框架,可能还需要触发以下事件
|
|
|
element.dispatchEvent(new Event('blur'));
|
|
|
element.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter' }));
|
|
|
}
|
|
|
+ const findLabelForInput = (label) => {
|
|
|
+ let p = label.parentElement
|
|
|
+ for (let i = 0; i < 10; i++) {
|
|
|
+ const input = p.getElementsByTagName('input')
|
|
|
+ if (input.length > 0) {
|
|
|
+ return input[0]
|
|
|
+ }
|
|
|
+ p = p.parentElement
|
|
|
+ }
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ const findLabelForSpan = (label) => {
|
|
|
+ let p = label.parentElement
|
|
|
+ for (let i = 0; i < 10; i++) {
|
|
|
+ const span = p.getElementsByTagName('span')
|
|
|
+ if (span.length > 0) {
|
|
|
+ return [...span]
|
|
|
+ }
|
|
|
+ p = p.parentElement
|
|
|
+ }
|
|
|
+ return null
|
|
|
+ }
|
|
|
// 监听页面 URL 变化
|
|
|
new MutationObserver(() => {
|
|
|
const url = location.href;
|
|
@@ -102,26 +131,55 @@ class SidebarManager {
|
|
|
this.checkAndRestoreState();
|
|
|
}
|
|
|
}).observe(document, { subtree: true, childList: true });
|
|
|
- const handleFillInput = (data, i) => {
|
|
|
- data.forEach(item => {
|
|
|
- if (item.id) {
|
|
|
- const input = formChildren.find(child => child.id === item.id)
|
|
|
- console.log(input);
|
|
|
-
|
|
|
- if (item.type === 'text') {
|
|
|
+
|
|
|
+ const handleFillInput = async (data, index) => {
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
|
+ const item = data[i]
|
|
|
+ if (item.findBy === 'id') {
|
|
|
+ const input = formChildren.find(child => child.id === item.findByValue)
|
|
|
+ if (item.type === 'text' || item.type === 'textarea' || item.type === 'number') {
|
|
|
+ if (!input) {
|
|
|
+ if (item.label) {
|
|
|
+ const label = [...form.getElementsByTagName('label')].find(label => label.innerText.includes(item.label))
|
|
|
+ if (label) {
|
|
|
+ const input = findLabelForInput(label)
|
|
|
+ if (input) {
|
|
|
+ await simulateUserInput(input, excelDataA[item.excelColumn][index])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
if (input) {
|
|
|
- simulateUserInput(input, excelDataA[item.excelColumn][i])
|
|
|
+ await simulateUserInput(input, excelDataA[item.excelColumn][index])
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (item.type === 'radio' || item.type === 'checkbox') {
|
|
|
+ if (item.label) {
|
|
|
+ const label = [...form.getElementsByTagName('label')].find(label => label.innerText.includes(item.label))
|
|
|
+ if (label) {
|
|
|
+ const span = findLabelForSpan(label)
|
|
|
+ span.forEach(span => {
|
|
|
+ span.innerText === excelDataA[item.excelColumn][index] && span.click()
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (item.type === 'date') {
|
|
|
+ const input = formChildren.find(child => child.id === item.findByValue)
|
|
|
+ if (excelDataA[item.excelColumn][index]) {
|
|
|
+ await simulateCompleteUserAction(input, input, excelDataA[item.excelColumn][index], excelDataA[item.excelColumn][index])
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
} else if (item.findBy === 'placeholder') {
|
|
|
const input = formChildren.find(child => child.placeholder === item.findByValue)
|
|
|
if (input) {
|
|
|
- input.value = excelDataA[item.excelColumn][i]
|
|
|
+ simulateUserInput(input, excelDataA[item.excelColumn][index])
|
|
|
}
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
+ let form = null
|
|
|
let formChildren = []
|
|
|
let excelDataA = []
|
|
|
// 监听来自sidebar的消息
|
|
@@ -149,12 +207,10 @@ class SidebarManager {
|
|
|
}
|
|
|
}
|
|
|
if (event.data.type === "HANDLE_FILL_INPUT") {
|
|
|
- console.log(event.data.data)
|
|
|
const { formData, excelData } = event.data.data
|
|
|
excelDataA = excelData
|
|
|
console.log(formChildren, excelDataA);
|
|
|
-
|
|
|
- handleFillInput(formData, 0)
|
|
|
+ await handleFillInput(formData, 0)
|
|
|
}
|
|
|
});
|
|
|
|
|
@@ -175,7 +231,7 @@ class SidebarManager {
|
|
|
);
|
|
|
}
|
|
|
if (event.data.type === "ANALYZE_PAGE") {
|
|
|
- const form = document.querySelectorAll("form")[1];
|
|
|
+ form = document.querySelectorAll("form")[1];
|
|
|
formChildren = [...form.elements]
|
|
|
// 分析页面并返回结果
|
|
|
const pageInfo = window.pageAnalyzer.analyzePage();
|
|
@@ -544,35 +600,29 @@ const simulateCompleteUserAction = async (clickElement, inputElement, inputText,
|
|
|
|
|
|
try {
|
|
|
// 执行操作序列
|
|
|
- console.log('开始模拟用户操作...');
|
|
|
|
|
|
// 1. 触发鼠标弹起
|
|
|
- console.log('触发鼠标弹起事件...');
|
|
|
simulateMouseUp(clickElement);
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
|
|
|
|
|
// 2. 模拟键盘输入
|
|
|
- console.log('开始模拟键盘输入...');
|
|
|
await simulateTyping(inputElement, inputText);
|
|
|
await new Promise(resolve => setTimeout(resolve, 200));
|
|
|
|
|
|
// 3. 查找并点击td元素
|
|
|
- console.log('查找目标td元素...');
|
|
|
const tdElement = await findTdByTitle(tdTitle);
|
|
|
- console.log(tdElement);
|
|
|
|
|
|
|
|
|
- console.log('找到td元素,触发点击事件...');
|
|
|
setTimeout(() => {
|
|
|
tdElement.click()
|
|
|
- }, 1000)
|
|
|
+ }, 100)
|
|
|
|
|
|
- console.log('所有操作完成');
|
|
|
return true;
|
|
|
} catch (error) {
|
|
|
- console.error('操作过程中出现错误:', error);
|
|
|
throw error;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|