54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
export async function copyTextToClipboard(value: string) {
|
||
const text = value.trim();
|
||
if (!text) {
|
||
return false;
|
||
}
|
||
|
||
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
|
||
try {
|
||
await navigator.clipboard.writeText(text);
|
||
return true;
|
||
} catch {
|
||
// 部分内嵌浏览器会暴露 Clipboard API,但会因权限上下文拒绝写入,继续走兼容路径。
|
||
}
|
||
}
|
||
|
||
if (typeof document === 'undefined') {
|
||
return false;
|
||
}
|
||
|
||
const textarea = document.createElement('textarea');
|
||
textarea.value = text;
|
||
textarea.setAttribute('readonly', 'true');
|
||
textarea.style.position = 'fixed';
|
||
textarea.style.left = '-9999px';
|
||
textarea.style.top = '0';
|
||
document.body.appendChild(textarea);
|
||
|
||
const selection = document.getSelection();
|
||
const selectedRange =
|
||
selection && selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
|
||
|
||
textarea.focus();
|
||
textarea.select();
|
||
|
||
let copied = false;
|
||
try {
|
||
copied =
|
||
typeof document.execCommand === 'function' &&
|
||
document.execCommand('copy');
|
||
} catch {
|
||
copied = false;
|
||
} finally {
|
||
document.body.removeChild(textarea);
|
||
if (selection) {
|
||
selection.removeAllRanges();
|
||
if (selectedRange) {
|
||
selection.addRange(selectedRange);
|
||
}
|
||
}
|
||
}
|
||
|
||
return copied;
|
||
}
|