From d3b2949ff7b9aa22eb683663b5b3fb166097e146 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Thu, 2 Oct 2025 10:49:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=AA=E8=B2=BC=E8=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/ui/copyToClipboard.ts | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/client/ui/copyToClipboard.ts b/client/ui/copyToClipboard.ts index a9cfa2b..98e65ba 100644 --- a/client/ui/copyToClipboard.ts +++ b/client/ui/copyToClipboard.ts @@ -1,14 +1,20 @@ -import { $ } from 'mdui/jq' - export default function copyToClipboard(text: string) { - if (navigator.clipboard) { - navigator.clipboard.writeText(text) - } else { - const input = $('#copy_to_clipboard_fallback').get(0) as HTMLInputElement - input.value = text - input.select() - input.setSelectionRange(0, 1145141919810) - document.execCommand('copy') - input.setSelectionRange(null, null) - } -} \ No newline at end of file + if (navigator.clipboard) + return navigator.clipboard.writeText(text) + return new Promise((res rej) => { + if (document.hasFocus()) { + const a = document.createElement("textarea") + document.body.appendChild(a) + a.style.position = "fixed" + a.style.clip = "rect(0 0 0 0)" + a.style.top = "10px" + a.value = text + a.select() + document.execCommand("copy", true) + document.body.removeChild(a) + res() + } else { + rej() + } + }) +}