fix(frontend): fix copyToClipboard on non-HTTPS site (#2046)

This commit is contained in:
Bryan
2023-08-23 20:53:10 +08:00
committed by GitHub
parent da12a65925
commit daa2c75853
+19 -1
View File
@@ -341,12 +341,30 @@ export async function copyToClipboard(value?: string, sendToast = true): Promise
}
let success = false
if (navigator?.clipboard) {
if (navigator?.clipboard && window.isSecureContext) {
success = await navigator.clipboard
.writeText(value)
.then(() => true)
.catch(() => false)
} else {
const textArea = document.createElement("textarea");
textArea.value = value;
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
success = true;
} catch (error) {
// ignore (success = false)
} finally {
textArea.remove();
}
}
sendToast &&
sendUserToast(success ? 'Copied to clipboard!' : "Couldn't copy to clipboard", !success)
return success