You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展无法捕获ChatGPT对话内容的问题排查求助

Chrome扩展无法捕获ChatGPT对话内容的问题排查求助

我正在开发一个Chrome扩展,用来捕获并保存ChatGPT的对话上下文,预期它能实现这几个功能:

  • 用内容脚本捕获对话内容
  • 把对话保存到chrome.storage.local里
  • 开启新聊天时,把之前保存的对话加载到输入框中

但现在扩展没法正常工作,每次点击「Save Current Context」按钮,都会弹出提示「Failed to capture conversation.」,我已经试了一些方法但没解决,想请大家帮忙排查下问题!

我的代码

manifest.json

{
  "manifest_version": 3,
  "name": "AI Context Manager",
  "version": "1.0",
  "description": "Seamlessly manage conversation context across AI assistant sessions.",
  "permissions": ["storage", "activeTab", "scripting"],
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "icon16.png",
      "48": "icon48.png",
      "128": "icon128.png"
    }
  },
  "icons": {
    "16": "icon16.png",
    "48": "icon48.png",
    "128": "icon128.png"
  }
}

content.js

function captureConversation() {
  return new Promise((resolve) => {
    const selectors = [
      '.group\\/conversation .text-message',
      '.message',
      '.text-base',
      'div[role="dialog"]',
      'div > div > p',
    ];

    const observer = new MutationObserver((mutations) => {
      mutations.forEach((mutation) => {
        for (const selector of selectors) {
          const conversationElements = document.querySelectorAll(selector);
          if (conversationElements.length > 0) {
            const conversation = Array.from(conversationElements).map(el => el.innerText).join('\n');
            console.log('Captured conversation using selector:', selector, conversation); // Debugging
            observer.disconnect(); // Stop observing once the conversation is captured
            resolve(conversation);
            return;
          }
        }
      });
    });

    observer.observe(document.body, { childList: true, subtree: true });
  });
}

// Listen for messages from the popup or background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'captureConversation') {
    captureConversation().then((conversation) => {
      sendResponse({ conversation });
    });
    return true; // Indicates that the response will be sent asynchronously
  }
});
document.getElementById('save-context').addEventListener('click', () => {
    chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        // Inject content.js into the current tab
        chrome.scripting.executeScript({
            target: { tabId: tabs[0].id },
            files: ['content.js']
        }, () => {
            // Send a message to content.js to capture the conversation
            chrome.tabs.sendMessage(tabs[0].id, { action: 'captureConversation' }, (response) => {
                if (response && response.conversation) {
                    const conversation = response.conversation;
                    console.log('Conversation to save:', conversation); // Debugging

                    // Save the conversation to chrome.storage.local
                    chrome.storage.local.set({ conversation }, () => {
                        console.log('Conversation saved to chrome.storage.local'); // Debugging
                        alert('Context saved!');
                    });
                } else {
                    console.log('No conversation captured.'); // Debugging
                    alert('Failed to capture conversation.');
                }
            });
        });
    });
});

background.js

chrome.runtime.onInstalled.addListener(() => {
  console.log('AI Context Manager installed.');
});

我已经尝试过的方法

  • 更新了content.js里的选择器,尽量适配ChatGPT当前的DOM结构
  • 加入了MutationObserver,用来处理对话消息的异步加载情况
  • 确认了manifest.json里已经包含了storage权限

错误信息

点击保存按钮时,控制台会输出「No conversation captured.」,同时弹出提示「Failed to capture conversation.」

想请教的问题

  1. content.js里的选择器是不是不匹配ChatGPT现在的DOM结构?
  2. 有没有更靠谱的方法处理对话消息的异步加载?
  3. 我是不是漏掉了某些必要的权限或者设置?

备注:内容来源于stack exchange,提问作者bhavya shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 15:05:27