diff --git a/templates/index.html b/templates/index.html index 2741134..7138a42 100644 --- a/templates/index.html +++ b/templates/index.html @@ -249,9 +249,6 @@ div.className = `message ${role}`; const avatar = role === 'user' ? '👤' : '🤖'; - // 准备复制内容 - 转义双引号用于HTML属性 - const contentForCopy = content.replace(/&/g, '&').replace(/"/g, '"'); - let html = `
${avatar}
`; // 思考内容 @@ -272,8 +269,9 @@ } else { html += `
${escapeHtml(content)}
`; } - // 复制按钮 - html += ``; + // 复制按钮 - 使用隐藏input存储原始内容 + html += ``; + html += ``; html += `
`; // Agent信息 @@ -301,19 +299,39 @@ } function copyMessage(btn) { - // 获取存储的内容并解码HTML实体 - let text = btn.dataset.content || ''; - // 解码HTML实体 - text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>').replace(/'/g, "'"); + // 从隐藏input获取原始内容 + const hiddenInput = btn.parentElement.querySelector('.copy-source'); + if (!hiddenInput) { + console.error('找不到复制源'); + return; + } + const text = hiddenInput.value; - navigator.clipboard.writeText(text).then(() => { - btn.innerHTML = ' 已复制'; - btn.classList.add('copied'); - setTimeout(() => { btn.innerHTML = ' 复制'; btn.classList.remove('copied'); }, 2000); - }).catch(err => { + // 创建临时textarea复制 + const textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'fixed'; + textarea.style.left = '-9999px'; + document.body.appendChild(textarea); + textarea.select(); + + try { + const success = document.execCommand('copy'); + if (success) { + btn.innerHTML = ' 已复制'; + btn.classList.add('copied'); + setTimeout(() => { + btn.innerHTML = ' 复制'; + btn.classList.remove('copied'); + }, 2000); + } else { + btn.innerHTML = ' 失败'; + } + } catch (err) { console.error('复制失败:', err); - btn.innerHTML = ' 失败'; - }); + } + + document.body.removeChild(textarea); } function escapeHtml(text) {