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 } });
popup.js
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.」
想请教的问题
- content.js里的选择器是不是不匹配ChatGPT现在的DOM结构?
- 有没有更靠谱的方法处理对话消息的异步加载?
- 我是不是漏掉了某些必要的权限或者设置?
备注:内容来源于stack exchange,提问作者bhavya shah
相关产品推荐
相关产品推荐

