// ==UserScript== // @name DeepSeek Chat Exporter (Markdown & PDF) // @namespace http://tampermonkey.net/ // @version 1.1 // @description 导出 DeepSeek 聊天记录为 Markdown 和 PDF 格式 // @author HSyuf/Blueberrycongee // @match https://chat.deepseek.com/* // @grant GM_addStyle // @grant GM_download // @license MIT // @downloadURL none // ==/UserScript== (function () { "use strict"; class AiAnswer { constructor(aiChain, aiAnswer) { this.aiChain = { type: "ai_chain", content: aiChain ? aiChain.textContent.trim() : "[无内容]", }; this.aiAnswer = { type: "ai_formal_reply", content: aiAnswer ? aiAnswer.textContent.trim() : "[无内容]", }; } } // ===================== // 获取用户消息 // ===================== function getUserMessages() { const chatContainer = Array.from(document.querySelectorAll(".fa81")); return chatContainer.map((el) => ({ type: "user", content: el.textContent.trim() || "[无内容]", })); } // ===================== // 获取 AI 消息 // ===================== function getAiMessages() { const aiAnswers = Array.from( document.querySelectorAll(".f9bf7997.c05b5566") ); return aiAnswers.map((el) => { const aiStatus = el.querySelector(".a6d716f5.db5991dd"); const aiChain = el.querySelector(".e1675d8b"); const aiAnswers = el.querySelector(".ds-markdown.ds-markdown--block"); return aiStatus.textContent === "思考已停止" ? new AiAnswer() : new AiAnswer(aiChain, aiAnswers); }); } // ===================== // 生成 Markdown 内容 // ===================== function generateMdContent() { const userMessages = getUserMessages(); const aiMessages = getAiMessages(); if (userMessages.length === 0 && aiMessages.length === 0) { alert("未找到聊天记录!"); return null; } const allMessages = []; for (let i = 0; i < userMessages.length; i++) { allMessages.push(userMessages[i], aiMessages[i].aiChain, aiMessages[i].aiAnswer); } return allMessages .map((msg) => { if (msg.type === "user") return `**用户:**\n${msg.content}`; if (msg.type === "ai_formal_reply") return `**AI 回答:**\n${msg.content}`; if (msg.type === "ai_chain") return `**AI 思维链:**\n${msg.content}`; return ""; }) .join("\n\n---\n\n"); } // ===================== // 导出为 Markdown // ===================== function exportMarkdown() { const mdContent = generateMdContent(); if (!mdContent) return; const blob = new Blob([mdContent], { type: "text/markdown" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `DeepSeek_Chat_${Date.now()}.md`; a.click(); setTimeout(() => URL.revokeObjectURL(url), 5000); } // ===================== // 导出为 PDF // ===================== function exportPDF() { const mdContent = generateMdContent(); if (!mdContent) return; // 创建打印内容 const printContent = `