根据您提供的调试信息,您在页面中找到了大量的 div
元素,但没有找到包含聊天消息的特定元素。这表明可能需要更具体的选择器来提取聊天内容。以下是一些步骤来帮助您解决这个问题:
步骤 4: 逐步调试
如果您仍然无法找到消息元素,可以尝试逐步调试:
- 选择所有
div
元素:const allDivs = document.querySelectorAll('div'); allDivs.forEach(div => { console.log(div); // 输出每个 div 的内容 });
- 通过特定属性过滤:
如果某些div
有特定的属性(如data-*
属性),您可以使用这些属性来过滤:const userMessages = document.querySelectorAll('div[data-role="message"]'); // 根据实际情况调整
F12打开浏览器调试窗口,输入1中代码,得到了一些html代码,经过分析得到div标签的标识:
获取到的标签
聊天的主题标题:d8ed659a 我的提问:fbb737a4 ai的思考过程:e1675d8b AI的解答过程:ds-markdown ds-markdown--block
问Monica,经过几次调整,最终得到如下结果:
// ==UserScript==
// @name DeepSeek Chat Content Saver
// @match https://chat.deepseek.com/*
// @version 1.6
// @description Save chat content as Markdown with auto-save option
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
const CONFIG = {
BUTTON_ID: 'deepseek-save-btn',
USER_PREFIX: "你",
AI_PREFIX: "AI",
AUTO_SAVE_ENABLED: true, // 配置项:是否启用自动保存
AUTO_SAVE_INTERVAL: 30000 // 自动保存间隔(毫秒),例如30秒
};
function generateMarkdown(messages) {
let md = `# 聊天记录\n\n`;
messages.forEach(msg => {
md += `**${msg.role === 'user' ? CONFIG.USER_PREFIX : CONFIG.AI_PREFIX}**:\n`;
md += `${msg.text}\n\n---\n\n`;
});
md += `> 保存时间: ${new Date().toLocaleString()}\n`;
return md;
}
function downloadFile(content, filename) {
const blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a); // 需要将链接添加到DOM中
a.click();
document.body.removeChild(a); // 下载后移除链接
setTimeout(() => URL.revokeObjectURL(url), 100);
}
function extractMessages() {
const messages = [];
// 提取聊天主题标题
const titleElement = document.querySelector('.d8ed659a');
let title = titleElement ? titleElement.innerText : '聊天记录';
// 提取用户提问、AI思考和AI回答,按顺序排列
const userQuestions = document.querySelectorAll('.fbb737a4');
const aiThoughts = document.querySelectorAll('.e1675d8b');
const aiAnswers = document.querySelectorAll('.ds-markdown.ds-markdown--block');
for (let i = 0; i < userQuestions.length; i++) {
messages.push({
role: 'user',
text: userQuestions[i].innerText
});
if (i < aiThoughts.length) {
messages.push({
role: 'ai',
text: aiThoughts[i].innerText
});
}
if (i < aiAnswers.length) {
messages.push({
role: 'ai',
text: aiAnswers[i].innerText
});
}
}
console.log('提取的消息:', messages); // 调试输出
return { title, messages };
}
function saveChatRecord() {
const { title, messages } = extractMessages();
if (messages.length === 0) throw new Error("未找到对话内容");
const content = generateMarkdown(messages);
//const timestamp = new Date().toISOString().replace(/[-:.]/g, "_"); // 格式化时间戳
//const filename = `${title}_${timestamp}.md`; // 文件名包含时间戳
const filename = `${title}.md`; // 文件名不包含时间戳
// 下载文件
downloadFile(content, filename);
//alert('保存成功!');
}
function startAutoSave() {
if (CONFIG.AUTO_SAVE_ENABLED) {
setInterval(() => {
try {
saveChatRecord();
} catch (error) {
console.error('自动保存失败:', error);
}
}, CONFIG.AUTO_SAVE_INTERVAL);
}
}
function createSaveButton() {
const button = document.createElement('button');
button.id = CONFIG.BUTTON_ID;
button.innerText = '保存对话';
button.style.position = 'fixed';
button.style.bottom = '20px';
button.style.right = '20px';
button.style.padding = '15px 30px';
button.style.fontSize = '16px';
button.style.backgroundColor = '#4CAF50';
button.style.color = 'white';
button.style.border = 'none';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
button.style.zIndex = '1000';
button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.3)';
document.body.appendChild(button);
return button;
}
function setupButtonHandler(btn) {
btn.addEventListener('click', saveChatRecord);
}
function initialize() {
const btn = createSaveButton();
setupButtonHandler(btn);
startAutoSave(); // 启动自动保存功能
}
// 启动
if (document.readyState === 'complete') initialize();
else window.addEventListener('load', initialize);
})();