Skip to content

Commit 60f99f0

Browse files
committed
add copy button functionality to main template
1 parent 40100c7 commit 60f99f0

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ __debug_*
3030

3131
# Editor/IDE
3232
# .idea/
33-
# .vscode/
33+
.vscode/
34+
35+
# Build output
36+
tiny-requestbin
3437

3538
# macos
3639
.DS_Store

templates/main.tmpl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,73 @@
534534
setCollapsedState(initiallyCollapsed, true);
535535
}
536536
});
537+
538+
// --- Copy button functionality ---
539+
const copyBodyBtn = document.getElementById('copy-body-btn');
540+
if (copyBodyBtn) {
541+
copyBodyBtn.addEventListener('click', async () => {
542+
const bodyText = document.getElementById('body-text');
543+
if (bodyText) {
544+
try {
545+
// Get the text content without HTML tags
546+
const textToCopy = bodyText.textContent || bodyText.innerText;
547+
548+
// Use the modern clipboard API if available
549+
if (navigator.clipboard && window.isSecureContext) {
550+
await navigator.clipboard.writeText(textToCopy);
551+
} else {
552+
// Fallback for older browsers or non-HTTPS contexts
553+
const textArea = document.createElement('textarea');
554+
textArea.value = textToCopy;
555+
textArea.style.position = 'fixed';
556+
textArea.style.opacity = '0';
557+
document.body.appendChild(textArea);
558+
textArea.focus();
559+
textArea.select();
560+
document.execCommand('copy');
561+
document.body.removeChild(textArea);
562+
}
563+
564+
// Visual feedback
565+
const originalContent = copyBodyBtn.innerHTML;
566+
copyBodyBtn.innerHTML = `
567+
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
568+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
569+
</svg>
570+
<span data-i18n="copied">${translations[currentLang]['copied']}</span>
571+
`;
572+
copyBodyBtn.classList.remove('bg-blue-600', 'hover:bg-blue-700');
573+
copyBodyBtn.classList.add('bg-green-600', 'hover:bg-green-700');
574+
575+
// Reset after 2 seconds
576+
setTimeout(() => {
577+
copyBodyBtn.innerHTML = originalContent;
578+
copyBodyBtn.classList.remove('bg-green-600', 'hover:bg-green-700');
579+
copyBodyBtn.classList.add('bg-blue-600', 'hover:bg-blue-700');
580+
}, 2000);
581+
582+
} catch (err) {
583+
console.error('Failed to copy text: ', err);
584+
// Show error feedback
585+
const originalContent = copyBodyBtn.innerHTML;
586+
copyBodyBtn.innerHTML = `
587+
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
588+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
589+
</svg>
590+
<span>失败</span>
591+
`;
592+
copyBodyBtn.classList.remove('bg-blue-600', 'hover:bg-blue-700');
593+
copyBodyBtn.classList.add('bg-red-600', 'hover:bg-red-700');
594+
595+
setTimeout(() => {
596+
copyBodyBtn.innerHTML = originalContent;
597+
copyBodyBtn.classList.remove('bg-red-600', 'hover:bg-red-700');
598+
copyBodyBtn.classList.add('bg-blue-600', 'hover:bg-blue-700');
599+
}, 2000);
600+
}
601+
}
602+
});
603+
}
537604
});
538605
</script>
539606
</body>

0 commit comments

Comments
 (0)