fix(web): add fallback for clipboard API in insecure contexts (#34092)
- Clipboard API requires secure context (HTTPS) to work - Windows HTTP environments fail silently - Add textarea element fallback for non-secure contexts
This commit is contained in:
parent
43c8cde3de
commit
48f0d08be6
@ -17,11 +17,28 @@ async function copyTextToClipboard(text: string): Promise<boolean> {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try Clipboard API first (works in secure contexts/HTTPS)
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
// Fallback for non-secure contexts (HTTP on Windows/localhost)
|
||||
try {
|
||||
// Use textarea element fallback for insecure contexts
|
||||
const textarea = document.createElement("textarea");
|
||||
textarea.value = text;
|
||||
textarea.style.position = "fixed";
|
||||
textarea.style.left = "-9999px";
|
||||
textarea.style.top = "-9999px";
|
||||
document.body.appendChild(textarea);
|
||||
textarea.focus();
|
||||
textarea.select();
|
||||
const success = document.execCommand("copy");
|
||||
document.body.removeChild(textarea);
|
||||
return success;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user