// ==UserScript== // @name 导出DeepSeek回答为图片 | Export DeepSeek Answer to Image // @namespace http://github.com/byronleeeee/exportDeepseek // @version 1.1 // @description 将DeepSeek的回答导出为一张图片 // @author ByronLeeeee // @match *://chat.deepseek.com/* // @grant none // @require https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; // i18n translations const i18n = { 'zh': { exportBtn: '导出AI回答为图片', notFound: 'AI回答区域未找到!', selectScheme: '选择配色方案:', footer: '回答来自DeepSeek,仅供参考', lastAnswer: '最新回答', colorSchemes: [ { name: '白色-蓝色', top: '#FFFFFF', bottom: '#4D6BFE', textTop: '#000000', textBottom: '#FFFFFF' }, { name: '黑色-金色', top: '#121212', bottom: '#FFD700', textTop: '#FFFFFF', textBottom: '#000000' }, { name: '浅灰-青色', top: '#F5F5F5', bottom: '#008080', textTop: '#000000', textBottom: '#FFFFFF' }, { name: '深灰-紫色', top: '#333333', bottom: '#800080', textTop: '#FFFFFF', textBottom: '#FFFFFF' } ], error: '生成图片失败,请查看控制台了解详情。', cancel: '取消', export: '导出', addCreator: '添加生成者信息', creatorName: '生成者名称', creatorPlaceholder: '输入你的名字' }, 'en': { exportBtn: 'Export AI Answer to Image', notFound: 'AI answer div not found!', selectScheme: 'Select a color scheme:', footer: 'Answer from DeepSeek, for reference only', lastAnswer: 'Last Answer', colorSchemes: [ { name: 'White-Blue', top: '#FFFFFF', bottom: '#4D6BFE', textTop: '#000000', textBottom: '#FFFFFF' }, { name: 'Black-Gold', top: '#121212', bottom: '#FFD700', textTop: '#FFFFFF', textBottom: '#000000' }, { name: 'Light Gray-Teal', top: '#F5F5F5', bottom: '#008080', textTop: '#000000', textBottom: '#FFFFFF' }, { name: 'Dark Gray-Purple', top: '#333333', bottom: '#800080', textTop: '#FFFFFF', textBottom: '#FFFFFF' } ], error: 'Failed to generate image. Check console for details.', cancel: 'Cancel', export: 'Export', addCreator: 'Add creator info', creatorName: 'Creator name', creatorPlaceholder: 'Enter your name' } }; const userLang = (navigator.language || navigator.userLanguage).split('-')[0]; const lang = i18n[userLang] ? userLang : 'en'; const texts = i18n[lang]; // Add styles with dark mode support function addStyles() { const style = document.createElement('style'); style.textContent = ` .ai-export-fab { position: fixed; bottom: 24px; right: 24px; width: 56px; height: 56px; border-radius: 50%; background-color: #4D6BFE; color: white; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 4px 8px rgba(0,0,0,0.2); z-index: 9999; transition: all 0.3s ease; } .ai-export-fab:hover { transform: scale(1.05); box-shadow: 0 6px 12px rgba(0,0,0,0.3); } .ai-export-fab-icon { display: flex; align-items: center; justify-content: center; } .ai-export-tooltip { position: absolute; background: rgba(0,0,0,0.7); color: white; padding: 5px 10px; border-radius: 4px; font-size: 12px; white-space: nowrap; right: 70px; opacity: 0; transition: opacity 0.3s; pointer-events: none; } .ai-export-fab:hover .ai-export-tooltip { opacity: 1; } .ai-color-scheme-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 10000; } .ai-modal-content { background-color: white; border-radius: 8px; padding: 20px; width: 300px; max-width: 90%; color: #000000; /* Default for light mode */ } body.dark .ai-modal-content { background-color: #1e1e1e; /* Dark mode background */ color: #ffffff; /* Dark mode text */ } .ai-modal-header { font-size: 18px; font-weight: bold; margin-bottom: 15px; } .ai-scheme-options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 15px; } .ai-scheme-option { border: 2px solid transparent; border-radius: 6px; overflow: hidden; cursor: pointer; transition: all 0.2s; } .ai-scheme-option:hover { transform: translateY(-2px); } .ai-scheme-option.selected { border-color: #4D6BFE; } .ai-scheme-preview { display: flex; flex-direction: column; height: 100px; } .ai-scheme-top { flex: 3; display: flex; align-items: center; justify-content: center; font-weight: bold; } .ai-scheme-bottom { flex: 1; display: flex; align-items: center; justify-content: center; font-size: 12px; } .ai-modal-buttons { display: flex; justify-content: flex-end; gap: 10px; } .ai-modal-button { padding: 8px 16px; border-radius: 4px; border: none; cursor: pointer; font-weight: bold; } .ai-modal-button.primary { background-color: #4D6BFE; color: white; } .ai-modal-button.secondary { background-color: #f1f1f1; color: #333; } body.dark .ai-modal-button.secondary { background-color: #333333; color: #ffffff; } .ai-export-button { position: absolute; top: 10px; right: 10px; background-color: #4D6BFE; color: white; border: none; border-radius: 4px; padding: 4px 8px; font-size: 12px; cursor: pointer; opacity: 0; transition: opacity 0.2s; z-index: 100; } ._4f9bf79:hover .ai-export-button { opacity: 1; } .ai-creator-option { margin: 15px 0; } .ai-creator-checkbox { margin-right: 8px; } .ai-creator-input { margin-top: 8px; width: 100%; padding: 8px; border-radius: 4px; border: 1px solid #ccc; box-sizing: border-box; display: none; } body.dark .ai-creator-input { background-color: #333; color: #fff; border-color: #555; } `; document.head.appendChild(style); } // Create FAB function createFAB() { const fab = document.createElement('div'); fab.innerHTML = `
${texts.exportBtn} (${texts.lastAnswer})
`; document.body.appendChild(fab); fab.querySelector('.ai-export-fab').addEventListener('click', async () => { const aiDivs = document.querySelectorAll('div._4f9bf79._43c05b5'); if (!aiDivs.length) { alert(texts.notFound); return; } showColorSchemeModal(aiDivs[aiDivs.length - 1]); }); } // Add export buttons to each AI answer function addExportButtonsToAnswers() { const observer = new MutationObserver(() => { const aiDivs = document.querySelectorAll('div._4f9bf79._43c05b5'); aiDivs.forEach(aiDiv => { if (!aiDiv.querySelector('.ai-export-button')) { const exportButton = document.createElement('button'); exportButton.className = 'ai-export-button'; exportButton.textContent = texts.exportBtn; exportButton.addEventListener('click', () => { showColorSchemeModal(aiDiv); }); const parentDiv = aiDiv.closest('.ds-relative') || aiDiv; parentDiv.style.position = 'relative'; parentDiv.appendChild(exportButton); } }); }); observer.observe(document.body, { childList: true, subtree: true }); const aiDivs = document.querySelectorAll('div._4f9bf79._43c05b5'); aiDivs.forEach(aiDiv => { if (!aiDiv.querySelector('.ai-export-button')) { const exportButton = document.createElement('button'); exportButton.className = 'ai-export-button'; exportButton.textContent = texts.exportBtn; exportButton.addEventListener('click', () => { showColorSchemeModal(aiDiv); }); const parentDiv = aiDiv.closest('.ds-relative') || aiDiv; parentDiv.style.position = 'relative'; parentDiv.appendChild(exportButton); } }); } // Show color scheme modal function showColorSchemeModal(aiDiv) { const modal = document.createElement('div'); modal.className = 'ai-color-scheme-modal'; let selectedSchemeIndex = 0; let creatorName = ''; let addCreatorInfo = false; // Try to get default creator name from user profile const profileNameElement = document.querySelector('.ds-dropdown-menu-option__label'); const defaultCreatorName = profileNameElement ? profileNameElement.textContent.trim() : ''; modal.innerHTML = `
${texts.selectScheme}
${texts.colorSchemes.map((scheme, index) => `
AI
DeepSeek
`).join('')}
`; document.body.appendChild(modal); const schemeOptions = modal.querySelectorAll('.ai-scheme-option'); schemeOptions.forEach(option => { option.addEventListener('click', () => { schemeOptions.forEach(opt => opt.classList.remove('selected')); option.classList.add('selected'); selectedSchemeIndex = parseInt(option.dataset.index); }); }); const creatorCheckbox = modal.querySelector('.ai-creator-checkbox'); const creatorInput = modal.querySelector('.ai-creator-input'); creatorCheckbox.addEventListener('change', () => { addCreatorInfo = creatorCheckbox.checked; creatorInput.disabled = !addCreatorInfo; creatorInput.style.display = addCreatorInfo ? 'block' : 'none'; }); // Initialize the input field state addCreatorInfo = creatorCheckbox.checked; creatorInput.style.display = addCreatorInfo ? 'block' : 'none'; modal.querySelector('#ai-cancel-btn').addEventListener('click', () => { document.body.removeChild(modal); }); modal.querySelector('#ai-export-btn').addEventListener('click', async () => { creatorName = creatorInput.value.trim(); document.body.removeChild(modal); await generateAndDownloadImage(aiDiv, texts.colorSchemes[selectedSchemeIndex], texts, addCreatorInfo, creatorName); }); } // Generate and download image async function generateAndDownloadImage(aiDiv, colorScheme, texts, addCreatorInfo, creatorName) { const clonedDiv = aiDiv.cloneNode(true); const buttonDiv = clonedDiv.querySelector('.ds-flex[style*="margin-top"]'); if (buttonDiv) buttonDiv.remove(); const container = document.createElement('div'); container.style.width = '600px'; container.style.borderRadius = '10px'; container.style.overflow = 'hidden'; container.style.boxShadow = '0 4px 8px rgba(0,0,0,0.1)'; container.style.display = 'flex'; container.style.flexDirection = 'column'; const topSection = document.createElement('div'); topSection.style.padding = '20px'; topSection.style.backgroundColor = colorScheme.top; topSection.style.color = colorScheme.textTop; topSection.appendChild(clonedDiv); let footerText = texts.footer; if (addCreatorInfo && creatorName) { footerText += ` — By ${creatorName}`; } const bottomSection = document.createElement('div'); bottomSection.textContent = footerText; bottomSection.style.padding = '10px'; bottomSection.style.textAlign = 'center'; bottomSection.style.fontSize = '14px'; bottomSection.style.fontFamily = 'Arial, sans-serif'; bottomSection.style.backgroundColor = colorScheme.bottom; bottomSection.style.color = colorScheme.textBottom; container.appendChild(topSection); container.appendChild(bottomSection); container.style.position = 'absolute'; container.style.left = '-9999px'; document.body.appendChild(container); try { const canvas = await html2canvas(container, { scale: 2, backgroundColor: null, useCORS: true, logging: false }); const link = document.createElement('a'); link.download = `ai_answer_${new Date().toISOString().split('T')[0]}.png`; link.href = canvas.toDataURL('image/png'); link.click(); } catch (error) { console.error('Error generating image:', error); alert(texts.error); } document.body.removeChild(container); } // Initialize function init() { addStyles(); createFAB(); addExportButtonsToAnswers(); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();