- "frontend/src/utils/clipboard.ts" - "frontend/src/pages/EmbedPlayer.tsx" - "frontend/src/pages/EmbedPlayer.module.css" - "frontend/src/pages/ShortPlayer.tsx" GSD-Task: S03/T01
24 lines
682 B
TypeScript
24 lines
682 B
TypeScript
/**
|
|
* Copy text to clipboard with execCommand fallback for older browsers.
|
|
* Returns true on success.
|
|
*/
|
|
export async function copyToClipboard(text: string): Promise<boolean> {
|
|
if (navigator.clipboard) {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
return true;
|
|
} catch {
|
|
// Clipboard API failed — fall through to fallback
|
|
}
|
|
}
|
|
// Fallback: hidden textarea + execCommand
|
|
const ta = document.createElement("textarea");
|
|
ta.value = text;
|
|
ta.style.position = "fixed";
|
|
ta.style.left = "-9999px";
|
|
document.body.appendChild(ta);
|
|
ta.select();
|
|
const ok = document.execCommand("copy");
|
|
document.body.removeChild(ta);
|
|
return ok;
|
|
}
|