fix: 复制按钮修复 - 使用隐藏input存储原始内容
This commit is contained in:
@@ -249,9 +249,6 @@
|
||||
div.className = `message ${role}`;
|
||||
|
||||
const avatar = role === 'user' ? '👤' : '🤖';
|
||||
// 准备复制内容 - 转义双引号用于HTML属性
|
||||
const contentForCopy = content.replace(/&/g, '&').replace(/"/g, '"');
|
||||
|
||||
let html = `<div class="message-avatar">${avatar}</div><div class="message-body">`;
|
||||
|
||||
// 思考内容
|
||||
@@ -272,8 +269,9 @@
|
||||
} else {
|
||||
html += `<div class="user-message-text">${escapeHtml(content)}</div>`;
|
||||
}
|
||||
// 复制按钮
|
||||
html += `<button class="copy-btn" data-content="${contentForCopy}" onclick="copyMessage(this)"><i class="ri-copy-line"></i> 复制</button>`;
|
||||
// 复制按钮 - 使用隐藏input存储原始内容
|
||||
html += `<input type="hidden" class="copy-source" value="${content.replace(/"/g, '"')}">`;
|
||||
html += `<button class="copy-btn" onclick="copyMessage(this)"><i class="ri-file-copy-line"></i> 复制</button>`;
|
||||
html += `</div>`;
|
||||
|
||||
// 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 = '<i class="ri-check-line"></i> 已复制';
|
||||
btn.classList.add('copied');
|
||||
setTimeout(() => { btn.innerHTML = '<i class="ri-copy-line"></i> 复制'; 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 = '<i class="ri-check-line"></i> 已复制';
|
||||
btn.classList.add('copied');
|
||||
setTimeout(() => {
|
||||
btn.innerHTML = '<i class="ri-file-copy-line"></i> 复制';
|
||||
btn.classList.remove('copied');
|
||||
}, 2000);
|
||||
} else {
|
||||
btn.innerHTML = '<i class="ri-error-line"></i> 失败';
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('复制失败:', err);
|
||||
btn.innerHTML = '<i class="ri-error-line"></i> 失败';
|
||||
});
|
||||
}
|
||||
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
|
||||
Reference in New Issue
Block a user