-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(dashboard): 修复设置页新建 API Key 后复制失败问题 #5439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -333,12 +333,51 @@ const loadApiKeys = async () => { | |
| } | ||
| }; | ||
|
|
||
| const tryExecCommandCopy = (text) => { | ||
| let textArea = null; | ||
| try { | ||
| if (typeof document === 'undefined') return false; | ||
| textArea = document.createElement('textarea'); | ||
| textArea.value = text; | ||
| textArea.setAttribute('readonly', ''); | ||
| textArea.style.position = 'fixed'; | ||
| textArea.style.opacity = '0'; | ||
| textArea.style.pointerEvents = 'none'; | ||
| textArea.style.left = '-9999px'; | ||
| document.body.appendChild(textArea); | ||
| textArea.focus(); | ||
| textArea.select(); | ||
| textArea.setSelectionRange(0, text.length); | ||
| return document.execCommand('copy'); | ||
| } catch (_) { | ||
| return false; | ||
| } finally { | ||
| try { | ||
| textArea?.remove?.(); | ||
| } catch (_) { | ||
| // ignore cleanup errors | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const copyTextToClipboard = async (text) => { | ||
| if (!text) return false; | ||
| if (tryExecCommandCopy(text)) return true; | ||
| if (typeof navigator === 'undefined' || !navigator.clipboard?.writeText) return false; | ||
| try { | ||
| await navigator.clipboard.writeText(text); | ||
| return true; | ||
| } catch (_) { | ||
| return false; | ||
| } | ||
| }; | ||
|
Comment on lines
+363
to
+373
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
建议调整逻辑,首先尝试使用 |
||
|
|
||
| const copyCreatedApiKey = async () => { | ||
| if (!createdApiKeyPlaintext.value) return; | ||
| try { | ||
| await navigator.clipboard.writeText(createdApiKeyPlaintext.value); | ||
| const ok = await copyTextToClipboard(createdApiKeyPlaintext.value); | ||
| if (ok) { | ||
| showToast(tm('apiKey.messages.copySuccess'), 'success'); | ||
| } catch (_) { | ||
| } else { | ||
| showToast(tm('apiKey.messages.copyFailed'), 'error'); | ||
| } | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
推荐创建完元素复制好删一下(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的,待会会一起加上,感谢建议