mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-06-16 21:03:58 +08:00
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
export async function copyToClipboard(text: string): Promise<boolean> {
|
|
try {
|
|
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
|
|
await navigator.clipboard.writeText(text);
|
|
return true;
|
|
}
|
|
} catch {
|
|
// fallback below
|
|
}
|
|
|
|
try {
|
|
if (typeof document === 'undefined') return false;
|
|
if (!document.body) return false;
|
|
|
|
const activeElement = document.activeElement as HTMLElement | null;
|
|
|
|
const 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';
|
|
textarea.style.top = '0';
|
|
textarea.style.width = '1px';
|
|
textarea.style.height = '1px';
|
|
textarea.style.padding = '0';
|
|
textarea.style.border = '0';
|
|
|
|
document.body.appendChild(textarea);
|
|
textarea.focus();
|
|
textarea.select();
|
|
textarea.setSelectionRange(0, textarea.value.length);
|
|
const copied = document.execCommand('copy');
|
|
document.body.removeChild(textarea);
|
|
|
|
if (activeElement?.focus) {
|
|
try {
|
|
activeElement.focus();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
return copied;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|