fix: add clipboard copy fallback for non-secure contexts in API key modals
This commit is contained in:
parent
54d5b978aa
commit
399666e246
@ -88,7 +88,24 @@ export const ApikeyReissueModal = ({
|
||||
const handleCopyApiKey = async (): Promise<void> => {
|
||||
if (apiKey) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(apiKey);
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(apiKey);
|
||||
} else {
|
||||
// Fallback for non-secure contexts (e.g., HTTP IP/Domain testing)
|
||||
const textArea = document.createElement('textarea');
|
||||
textArea.value = apiKey;
|
||||
textArea.style.top = '0';
|
||||
textArea.style.left = '0';
|
||||
textArea.style.position = 'fixed';
|
||||
document.body.appendChild(textArea);
|
||||
textArea.focus();
|
||||
textArea.select();
|
||||
const successful = document.execCommand('copy');
|
||||
document.body.removeChild(textArea);
|
||||
if (!successful) {
|
||||
throw new Error('Fallback copy failed');
|
||||
}
|
||||
}
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
showToast.success('API key copied to clipboard');
|
||||
|
||||
@ -292,7 +292,24 @@ export const NewModuleModal = ({
|
||||
const handleCopyApiKey = async (): Promise<void> => {
|
||||
if (apiKey) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(apiKey);
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(apiKey);
|
||||
} else {
|
||||
// Fallback for non-secure contexts (e.g., HTTP IP/Domain testing)
|
||||
const textArea = document.createElement("textarea");
|
||||
textArea.value = apiKey;
|
||||
textArea.style.top = "0";
|
||||
textArea.style.left = "0";
|
||||
textArea.style.position = "fixed";
|
||||
document.body.appendChild(textArea);
|
||||
textArea.focus();
|
||||
textArea.select();
|
||||
const successful = document.execCommand("copy");
|
||||
document.body.removeChild(textArea);
|
||||
if (!successful) {
|
||||
throw new Error("Fallback copy failed");
|
||||
}
|
||||
}
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
showToast.success("API key copied to clipboard");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user