Browse Source

修复url变动时总结卡片内容没有跟随变动的问题

alibct 7 months ago
parent
commit
07ae438893
3 changed files with 72 additions and 16 deletions
  1. 13 0
      CHANGELOG.md
  2. 58 15
      js/content.js
  3. 1 1
      manifest.json

+ 13 - 0
CHANGELOG.md

@@ -1,5 +1,18 @@
 # 更新日志
 
+## [0.1.1] - 2024-03-xx
+
+### 优化
+
+- 改进页面信息监听机制
+  - 优化标题变化检测
+  - 改进 URL 变化监听
+  - 完善 favicon 获取逻辑
+- 优化单页应用支持
+  - 添加动态标题更新
+  - 改进图标自动更新
+  - 优化页面信息同步
+
 ## [0.1.0] - 2024-03-xx
 
 ### 新增

+ 58 - 15
js/content.js

@@ -15,6 +15,8 @@ class SidebarManager {
     this.sidebarId = SidebarManager.CONFIG.SIDEBAR_ID;
     this.wrapperId = SidebarManager.CONFIG.WRAPPER_ID;
     this.isOpen = false;
+    this.currentUrl = window.location.href;
+    this.currentTitle = document.title;
     this.init();
   }
 
@@ -58,12 +60,29 @@ class SidebarManager {
       this.checkAndRestoreState();
     });
 
+    // 监听标题变化
+    const titleObserver = new MutationObserver(() => {
+      if (document.title !== this.currentTitle) {
+        this.currentTitle = document.title;
+        this.updatePageInfo();
+      }
+    });
+
+    titleObserver.observe(
+      document.querySelector("head > title") || document.head,
+      {
+        subtree: true,
+        characterData: true,
+        childList: true,
+      }
+    );
+
     // 监听页面 URL 变化
-    let lastUrl = location.href;
     new MutationObserver(() => {
       const url = location.href;
-      if (url !== lastUrl) {
-        lastUrl = url;
+      if (url !== this.currentUrl) {
+        this.currentUrl = url;
+        this.updatePageInfo();
         this.checkAndRestoreState();
       }
     }).observe(document, { subtree: true, childList: true });
@@ -210,26 +229,25 @@ class SidebarManager {
    * 发送页面信息到iframe
    */
   sendPageInfo() {
+    this.updatePageInfo();
+  }
+
+  /**
+   * 更新页面信息
+   */
+  updatePageInfo() {
     const iframe = document.getElementById(this.sidebarId);
     if (!iframe) return;
 
-    // 获取页面favicon
-    let favicon = "";
-    const iconLink = document.querySelector('link[rel*="icon"]');
-    if (iconLink) {
-      favicon = iconLink.href;
-    } else {
-      // 如果没有找到,使用网站根目录的favicon.ico
-      const url = new URL(window.location.href);
-      favicon = `${url.protocol}//${url.hostname}/favicon.ico`;
-    }
+    // 获取最新的favicon
+    const favicon = this.getFavicon();
 
-    // 发送页面信息到iframe
+    // 发送更新后的页面信息
     iframe.contentWindow.postMessage(
       {
         type: "PAGE_INFO",
         pageInfo: {
-          favicon: favicon,
+          favicon,
           title: document.title,
           url: window.location.href,
         },
@@ -238,6 +256,31 @@ class SidebarManager {
     );
   }
 
+  /**
+   * 获取最新的favicon
+   */
+  getFavicon() {
+    // 尝试获取动态favicon
+    const iconLinks = Array.from(
+      document.querySelectorAll('link[rel*="icon"]')
+    );
+    const favicon = iconLinks
+      .sort((a, b) => {
+        // 优先使用大尺寸图标
+        const sizeA = parseInt(a.sizes?.value) || 0;
+        const sizeB = parseInt(b.sizes?.value) || 0;
+        return sizeB - sizeA;
+      })
+      .map((link) => link.href)
+      .find(Boolean);
+
+    if (favicon) return favicon;
+
+    // 如果没有找到,返回网站根目录的favicon.ico
+    const url = new URL(window.location.href);
+    return `${url.protocol}//${url.hostname}/favicon.ico`;
+  }
+
   /**
    * 创建侧边栏
    */

+ 1 - 1
manifest.json

@@ -1,7 +1,7 @@
 {
   "manifest_version": 3,
   "name": "派维斯智能体助手",
-  "version": "0.1.0",
+  "version": "0.1.1",
   "description": "一个辅助用户处理业务流程的智能体助手",
   "author": "Paiwise Team",
   "permissions": [