Selaa lähdekoodia

Initial commit

chd 7 kuukautta sitten
vanhempi
commit
fc4112b7bf
4 muutettua tiedostoa jossa 74 lisäystä ja 6 poistoa
  1. 11 0
      css/chat.css
  2. 13 2
      js/background.js
  3. 28 3
      js/chat-ui.js
  4. 22 1
      js/content.js

+ 11 - 0
css/chat.css

@@ -15,7 +15,18 @@
   scroll-behavior: smooth;
   background: #fcfcfd; /* 与容器背景保持一致 */
 }
+.fill-button {
+  padding: 4px 8px;
+  margin-left: 8px;
+  border-radius: 4px;
+  border: 1px solid #ddd;
+  background: #fff;
+  cursor: pointer;
+}
 
+.fill-button:hover {
+  background: #f5f5f5;
+}
 .message {
   display: flex;
   max-width: 85%;

+ 13 - 2
js/background.js

@@ -1,4 +1,15 @@
 // 监听扩展图标点击事件
-chrome.action.onClicked.addListener((tab) => {
-  chrome.tabs.sendMessage(tab.id, { action: "toggleSidebar" });
+chrome.action.onClicked.addListener(async (tab) => {
+  try {
+    // 确保tab存在且有效
+    if (!tab || !tab.id) {
+      console.error('无效的标签页');
+      return;
+    }
+
+    await chrome.tabs.sendMessage(tab.id, { action: "toggleSidebar" });
+  } catch (error) {
+    // 处理可能的错误,比如content script未加载等情况
+    console.error('发送消息失败:', error);
+  }
 });

+ 28 - 3
js/chat-ui.js

@@ -8,7 +8,11 @@ class ChatUI {
     this.promptPanel = document.getElementById("prompt-panel");
     this.uploadButton = document.getElementById("upload-button");
     this.fileInput = document.getElementById("file-input");
-
+    this.inputs = document.querySelectorAll('input')
+    this.ContentInputs = []
+    console.log(this.inputs);
+    
+    this.excelData = [];
     // 确保AI服务已经初始化
     if (!window.aiService) {
       throw new Error("AI Service not initialized");
@@ -307,6 +311,8 @@ ${message}`;
     if (type === "assistant" && !isInterrupted) {
       const copyButton = this.createCopyButton(content);
       actionsDiv.appendChild(copyButton);
+      actionsDiv.appendChild(this.createFillButton());
+
     }
 
     // 添加时间戳
@@ -519,7 +525,22 @@ ${message}`;
         .replace(/^>\s+(.+)$/gm, "<blockquote>$1</blockquote>")
     );
   }
-
+  createFillButton() {
+    const button = document.createElement("button");
+    button.className = "fill-button";
+    button.innerHTML = `填充`;
+    button.addEventListener("click", () => {
+      window.parent.postMessage(
+        {
+          type: "FILL_INPUT",
+          data: this.excelData,
+        },
+        "*"
+      );
+      //从夫窗口获取信息
+    });
+    return button;
+  }
   /**
    * 创建复制按钮
    * @param {string} content 要复制的内容
@@ -634,6 +655,7 @@ ${message}`;
     window.addEventListener("message", (event) => {
       if (event.data.type === "PAGE_INFO") {
         const pageInfo = event.data.pageInfo;
+        this.ContentInputs = pageInfo.inputs
 
         // 更新卡片内容
         const favicon = document.querySelector(".page-favicon");
@@ -795,7 +817,8 @@ ${message}`;
     const headers = data[0];
     const rows = data.slice(1);
     const sampleRows = rows.slice(0, 2);
-
+    console.log(data);
+    this.excelData = data
     return `我将向你展示一个通过SheetJS库读取的Excel文件内容。请帮我理解这些数据:
 
 文件名:${fileName}
@@ -865,6 +888,8 @@ ${sampleRows
 document.addEventListener("DOMContentLoaded", () => {
   try {
     const chatUI = new ChatUI();
+  
+    
   } catch (error) {
     console.error("Failed to initialize ChatUI:", error);
   }

+ 22 - 1
js/content.js

@@ -18,6 +18,7 @@ class SidebarManager {
     this.currentUrl = window.location.href;
     this.currentTitle = document.title;
     this.init();
+    
   }
 
   /**
@@ -111,6 +112,10 @@ class SidebarManager {
           );
         }
       }
+      if (event.data.type === "FILL_INPUT") {
+        inputs.find(input => input.placeholder.includes('账号')).value = event.data.data[1][0]
+        inputs.find(input => input.placeholder.includes('密码')).value = event.data.data[1][1]
+      }
     });
 
     // 监听来自iframe的消息
@@ -120,7 +125,17 @@ class SidebarManager {
         event.source ===
         document.getElementById("paiwise-sidebar")?.contentWindow
       ) {
+        if (event.data.type === "FILL_INPUT") {
+          event.source.postMessage(
+            {
+              type: "GE_T",
+              pageInfo: pageInfo,
+            },
+            "*"
+          );
+        }
         if (event.data.type === "ANALYZE_PAGE") {
+       
           // 分析页面并返回结果
           const pageInfo = window.pageAnalyzer.analyzePage();
 
@@ -237,8 +252,9 @@ class SidebarManager {
    */
   updatePageInfo() {
     const iframe = document.getElementById(this.sidebarId);
-    if (!iframe) return;
 
+    if (!iframe) return;
+    
     // 获取最新的favicon
     const favicon = this.getFavicon();
 
@@ -381,6 +397,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
     sidebarManager.toggle();
     sendResponse({ success: true });
   }
+  sendResponse({ success: true });
 });
+window.onload = () => {
+  inputs = [...document.querySelectorAll('input')]
+}
 
+let inputs = []
 // 创建一个新的js文件用于处理侧边栏内部的关闭按钮事件