Skip to content

Commit 8d8f0d1

Browse files
committed
fix(ui): add execCommand fallback for copy button in non-secure HTTP contexts
- Fix copy button not working when accessing opencode over HTTP - Add try/catch error handling to prevent silent failures - Fallback to document.execCommand when navigator.clipboard is unavailable - Hide temporary textarea element to avoid UI glitches Fixes anomalyco#14803
1 parent 288eb04 commit 8d8f0d1

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

‎packages/ui/src/components/message-part.tsx‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,9 +1743,27 @@ ToolRegistry.register({
17431743
const handleCopy = async () => {
17441744
const content = text()
17451745
if (!content) return
1746-
await navigator.clipboard.writeText(content)
1747-
setCopied(true)
1748-
setTimeout(() => setCopied(false), 2000)
1746+
1747+
try {
1748+
// Prefer modern clipboard API first
1749+
if (navigator.clipboard) {
1750+
await navigator.clipboard.writeText(content)
1751+
} else {
1752+
// Fallback to execCommand for non-secure contexts (HTTP environments)
1753+
const textArea = document.createElement('textarea')
1754+
textArea.value = content
1755+
textArea.style.position = 'fixed'
1756+
textArea.style.opacity = '0'
1757+
document.body.appendChild(textArea)
1758+
textArea.select()
1759+
document.execCommand('copy')
1760+
document.body.removeChild(textArea)
1761+
}
1762+
setCopied(true)
1763+
setTimeout(() => setCopied(false), 2000)
1764+
} catch (err) {
1765+
console.error("Copy failed:", err)
1766+
}
17491767
}
17501768

17511769
return (

0 commit comments

Comments
 (0)