Fix copy for insecure context
This commit is contained in:
@ -5,10 +5,21 @@ export const useCopy = (timeout = 2000) => {
|
|||||||
const [copied, setCopied] = useState(false);
|
const [copied, setCopied] = useState(false);
|
||||||
|
|
||||||
const copy = async (text: string) => {
|
const copy = async (text: string) => {
|
||||||
|
setError(false);
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.writeText(text);
|
if (navigator.clipboard && window.isSecureContext) {
|
||||||
|
navigator.clipboard.writeText(text);
|
||||||
|
} else {
|
||||||
|
const textArea = document.createElement("textarea");
|
||||||
|
textArea.value = text;
|
||||||
|
textArea.style.position = "absolute";
|
||||||
|
textArea.style.opacity = "0";
|
||||||
|
document.body.appendChild(textArea);
|
||||||
|
textArea.select();
|
||||||
|
await document.execCommand("copy");
|
||||||
|
textArea.remove();
|
||||||
|
}
|
||||||
setCopied(true);
|
setCopied(true);
|
||||||
setError(false);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(true);
|
setError(true);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user