cx-nexredirect/lib/clipboard.ts

26 lines
698 B
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 {
// fall through
}
try {
const el = document.createElement("textarea");
el.value = text;
el.style.position = "fixed";
el.style.left = "-9999px";
el.style.top = "0";
el.setAttribute("readonly", "");
document.body.appendChild(el);
el.select();
el.setSelectionRange(0, text.length);
const ok = document.execCommand("copy");
document.body.removeChild(el);
return ok;
} catch {
return false;
}
}