mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-28 03:23:39 +00:00
* fix(frontend): guard message copy clipboard access * fix(frontend): reuse clipboard guard across copy actions
32 lines
811 B
TypeScript
32 lines
811 B
TypeScript
export async function writeTextToClipboard(text: string): Promise<boolean> {
|
|
try {
|
|
const clipboard = globalThis.navigator?.clipboard;
|
|
if (clipboard?.writeText) {
|
|
await clipboard.writeText(text);
|
|
return true;
|
|
}
|
|
|
|
const document = globalThis.document;
|
|
if (!document?.body?.appendChild || !document.execCommand) {
|
|
return false;
|
|
}
|
|
|
|
const textarea = document.createElement("textarea");
|
|
textarea.value = text;
|
|
textarea.setAttribute("readonly", "");
|
|
textarea.style.position = "fixed";
|
|
textarea.style.top = "-9999px";
|
|
textarea.style.left = "-9999px";
|
|
document.body.appendChild(textarea);
|
|
textarea.select();
|
|
|
|
try {
|
|
return document.execCommand("copy");
|
|
} finally {
|
|
textarea.remove();
|
|
}
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|