Lesson 8 of 9 · 20m
Clipboard, Files, and User Activation
Async clipboard access, paste formats, file handles, and why browser APIs are intentionally tied to visible user intent.
The clipboard sounds trivial until you implement "copy" correctly in a real app: plaintext and HTML variants, permissions, async user activation, Safari quirks, and the security reason a page cannot silently read whatever you copied in your password manager. Modern clipboard APIs are powerful precisely because the browser constrains when and how they run.
Writing is easier than reading
await navigator.clipboard.writeText(reportUrl);writeText generally requires a secure context and a user gesture. For richer content, ClipboardItem can carry multiple representations:
const item = new ClipboardItem({
"text/plain": new Blob([markdown], { type: "text/plain" }),
"text/html": new Blob([html], { type: "text/html" }),
});
await navigator.clipboard.write([item]);Multiple representations matter. Copying a table as plaintext, HTML, and an image are different products for different paste targets. Provide what users actually need rather than pretending clipboard data has one universal shape.
Reading is deliberately hard
navigator.clipboard.readText() often prompts or is denied unless the user paste-intents through keyboard or browser UI. That is correct behavior. Clipboard contents are credentials, medical data, and private messages. Design around explicit paste events when possible:
element.addEventListener("paste", event => {
const text = event.clipboardData.getData("text/plain");
const html = event.clipboardData.getData("text/html");
importClipboardPayload({ text, html });
});Paste events arrive with formats chosen by the source application. Sanitize HTML before inserting it anywhere; clipboard HTML is untrusted input like any other.
Files are capability objects now
showOpenFilePicker and the File System Access API let supporting browsers hand your app a file handle that can be re-read or written with permission. This is excellent for local-first editors. It is not universal: Firefox support is absent or partial depending on surface, and mobile behavior varies. Always keep the <input type="file"> and download fallback path.
User activation is a design constraint
The browser asks, in effect, "did the user just do something that plausibly means this action?" Copy buttons, paste flows, fullscreen, popups, device access — all versions of the same anti-abuse model. Chain too many async steps after a click and activation can expire. Capture user intent early, perform permission-sensitive calls close to the event, and do not hide actions behind artificial delays.
Field notes
- Clipboard API support differs across Safari and embedded webviews; feature-detect and keep
execCommandfallback only where truly necessary. - Never read the clipboard "just to help." Users interpret that as surveillance because it is.
- File handles may survive in IndexedDB in supporting browsers, but permission can be revoked.
- Test copy/paste into the real target apps — spreadsheets, email clients, terminals — not only back into your own textarea.
Was this lesson helpful?