Skip to content

Commit

Permalink
fix(frontend): fix copyToClipboard on non-HTTPS site (#2046)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSingee committed Aug 23, 2023
1 parent a419bc4 commit 95ea0e8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion frontend/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 95ea0e8

Please sign in to comment.