chd 6 months ago
parent
commit
f22c16a98a
2 changed files with 87 additions and 36 deletions
  1. 10 9
      js/chat-ui.js
  2. 77 27
      js/content.js

+ 10 - 9
js/chat-ui.js

@@ -194,6 +194,8 @@ class ChatUI {
 
         // 发送分析请求到父页面
         window.parent.postMessage({ type: "ANALYZE_PAGE" }, "*");
+        console.log(2222285);
+        
       });
       console.log(this.iframeInfo);
       this.fileInput.click();
@@ -736,7 +738,6 @@ ${this.iframeInfo}`;
         // 发送分析请求到父页面
         window.parent.postMessage({ type: "ANALYZE_PAGE" }, "*");
       });
-      return
       // 构建提示词
       const prompt = this.aiService.getSummaryPrompt(pageInfo);
 
@@ -849,7 +850,7 @@ ${this.iframeInfo}`;
     const headers = data[0];
     const rows = data.slice(1);
     const sampleRows = rows.slice(0, 2);
-    console.log(data);
+    console.log(); 
     data[0].forEach((header, i) => {
       if (!this.excelData[header]) this.excelData[header] = []
       this.excelData[header].push(data[1][i])
@@ -866,23 +867,23 @@ ${sampleRows
           return `第${index + 1}行: ${row
             .map((cell, i) => `${headers[i]}=${cell}`)
             .join(", ")}`;
-        })
+        })                                                                                                  
         .join("\n")}
   
 表单内容:
 ${this.iframeInfo}
-
+             
 要求:
 1. 请分析表单中实际可操作的表单项,
-2. 并根据实际可操作的表单项的所有信息与上传excel文件中的列标题进行匹配,生成表单项与excel文件中列标题的数组,并使用findBy字段告诉我通过什么信息匹配到的,使用findByValue字段告诉我匹配到的值,使用excelColumn字段告诉我excel文件中列标题的值。在一个字段内返回
-3. 并去除没有匹配到的表单项和excel文件中没有匹配到的列
-4. 如果表单项的输入项有id元素,同时返回id,通过type字段告诉我输入项的类型
+2. 并根据实际可操作的表单项的所有信息与上传excel文件中的列标题进行匹配,生成表单项与excel文件中列标题的数组,并使用findBy告诉我通过表单项的什么字段信息匹配到的,使用findByValue告诉我匹配到的表单项字段值,使用excelColumn字段告诉我excel文件中列标题的值。在一个字段内返回
+3. 并去除没有匹配到的表单项和excel文件中没有匹配到的列,
+4. 通过type字段告诉我输入项的类型
+5. 如果表单项有label标签,同时返回label,通过label字段告诉我label元素的文本
 5. 仅返回数组类型的数组,不要带换行符,不要返回任何其他内容。`
   }
 
   /**
-   * 读取Excel文件
-   * @param {File} file
+   * 读取Excel文件 * @param {File} file
    * @returns {Promise<Array>}
    */
   async readExcelFile(file) {

+ 77 - 27
js/content.js

@@ -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;
   }
 };
 
 
+
+