From 73865651a0095885713b61ae36e8e900158b332c Mon Sep 17 00:00:00 2001 From: Yifei Zhang Date: Mon, 3 Apr 2023 03:16:56 +0000 Subject: [PATCH] fix: #366 use fallback copy --- app/utils.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/utils.ts b/app/utils.ts index 1fb3d316..5fe277c6 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -5,15 +5,19 @@ export function trimTopic(topic: string) { return topic.replace(/[,。!?、,.!?]*$/, ""); } -export function copyToClipboard(text: string) { - navigator.clipboard - .writeText(text) - .then((res) => { - showToast(Locale.Copy.Success); - }) - .catch((err) => { - showToast(Locale.Copy.Failed); - }); +export async function copyToClipboard(text: string) { + try { + await navigator.clipboard.writeText(text); + } catch (error) { + const textarea = document.createElement("textarea"); + textarea.value = text; + document.body.appendChild(textarea); + textarea.select(); + document.execCommand("copy"); + document.body.removeChild(textarea); + } finally { + showToast(Locale.Copy.Success); + } } export function downloadAs(text: string, filename: string) {