Copy text to the clipboard
JavascriptThe modern API needs a secure context and a real user gesture, so the failure path matters as much as the success one.
async function copy(text, button) {
try {
await navigator.clipboard.writeText(text);
button.textContent = 'Copied';
} catch {
button.textContent = 'Press Ctrl+C';
}
setTimeout(() => { button.textContent = 'Copy'; }, 2000);
}