Skip to content

Commit 13ba7c8

Browse files
authored
Merge pull request #5 from knktc/bugfix-copy-button-not-working
Bugfix copy button not working
2 parents 40100c7 + 0b2a3ac commit 13ba7c8

2 files changed

Lines changed: 84 additions & 2 deletions

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: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@
344344
li.className = `request-link block p-4 hover:bg-gray-50 ${currentViewId == request.ID ? 'bg-indigo-50 border-l-4 border-indigo-500' : ''}`;
345345

346346
const methodColor = getMethodColor(request.Method);
347-
const timestamp = new Date(request.Timestamp).toLocaleString();
347+
const timestamp = formatTime(request.Timestamp);
348348

349349
li.innerHTML = `
350350
<div class="flex justify-between items-center mb-1">
@@ -393,6 +393,18 @@
393393
}
394394
}
395395

396+
// Helper function to format time in the same format as server-side formatTime
397+
function formatTime(dateString) {
398+
const date = new Date(dateString);
399+
const year = date.getFullYear();
400+
const month = String(date.getMonth() + 1).padStart(2, '0');
401+
const day = String(date.getDate()).padStart(2, '0');
402+
const hours = String(date.getHours()).padStart(2, '0');
403+
const minutes = String(date.getMinutes()).padStart(2, '0');
404+
const seconds = String(date.getSeconds()).padStart(2, '0');
405+
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
406+
}
407+
396408
// Pagination button event listeners
397409
prevPageBtn.addEventListener('click', () => {
398410
if (currentPage > 1) {
@@ -534,6 +546,73 @@
534546
setCollapsedState(initiallyCollapsed, true);
535547
}
536548
});
549+
550+
// --- Copy button functionality ---
551+
const copyBodyBtn = document.getElementById('copy-body-btn');
552+
if (copyBodyBtn) {
553+
copyBodyBtn.addEventListener('click', async () => {
554+
const bodyText = document.getElementById('body-text');
555+
if (bodyText) {
556+
try {
557+
// Get the text content without HTML tags
558+
const textToCopy = bodyText.textContent || bodyText.innerText;
559+
560+
// Use the modern clipboard API if available
561+
if (navigator.clipboard && window.isSecureContext) {
562+
await navigator.clipboard.writeText(textToCopy);
563+
} else {
564+
// Fallback for older browsers or non-HTTPS contexts
565+
const textArea = document.createElement('textarea');
566+
textArea.value = textToCopy;
567+
textArea.style.position = 'fixed';
568+
textArea.style.opacity = '0';
569+
document.body.appendChild(textArea);
570+
textArea.focus();
571+
textArea.select();
572+
document.execCommand('copy');
573+
document.body.removeChild(textArea);
574+
}
575+
576+
// Visual feedback
577+
const originalContent = copyBodyBtn.innerHTML;
578+
copyBodyBtn.innerHTML = `
579+
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
580+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
581+
</svg>
582+
<span data-i18n="copied">${translations[currentLang]['copied']}</span>
583+
`;
584+
copyBodyBtn.classList.remove('bg-blue-600', 'hover:bg-blue-700');
585+
copyBodyBtn.classList.add('bg-green-600', 'hover:bg-green-700');
586+
587+
// Reset after 2 seconds
588+
setTimeout(() => {
589+
copyBodyBtn.innerHTML = originalContent;
590+
copyBodyBtn.classList.remove('bg-green-600', 'hover:bg-green-700');
591+
copyBodyBtn.classList.add('bg-blue-600', 'hover:bg-blue-700');
592+
}, 2000);
593+
594+
} catch (err) {
595+
console.error('Failed to copy text: ', err);
596+
// Show error feedback
597+
const originalContent = copyBodyBtn.innerHTML;
598+
copyBodyBtn.innerHTML = `
599+
<svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
600+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
601+
</svg>
602+
<span>失败</span>
603+
`;
604+
copyBodyBtn.classList.remove('bg-blue-600', 'hover:bg-blue-700');
605+
copyBodyBtn.classList.add('bg-red-600', 'hover:bg-red-700');
606+
607+
setTimeout(() => {
608+
copyBodyBtn.innerHTML = originalContent;
609+
copyBodyBtn.classList.remove('bg-red-600', 'hover:bg-red-700');
610+
copyBodyBtn.classList.add('bg-blue-600', 'hover:bg-blue-700');
611+
}, 2000);
612+
}
613+
}
614+
});
615+
}
537616
});
538617
</script>
539618
</body>

0 commit comments

Comments
 (0)