← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Clipboard API

JavaScript HTML5 Beginner
debt(d3/e3/b3/t5)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list ESLint with a specific code_pattern `execCommand('copy')`, meaning the deprecated legacy usage is automatically flagged. However, the subtler issues (missing try/catch, calling readText without user gesture) are not caught by default linting rules.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing `document.execCommand('copy')` with `await navigator.clipboard.writeText(text)`, wrapping in try/catch, and providing a fallback. This is slightly more than a one-line swap since it requires adding async/await scaffolding and error handling, but it stays within a single component or call site.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The Clipboard API applies only to web contexts and is typically used in isolated UI components or utility functions. It doesn't impose a cross-cutting architectural constraint; only the specific component handling clipboard operations pays the cost of the HTTPS requirement, permission handling, and fallback logic.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field explicitly states that `navigator.clipboard.writeText()` appears to work universally but silently fails or throws in non-secure contexts (non-HTTPS) and without a user gesture. A developer who's never used this API would naturally assume a standard browser API is broadly available, matching a documented gotcha that most devs eventually encounter.

About DEBT scoring →

TL;DR

The async Clipboard API (navigator.clipboard) is the modern replacement for document.execCommand('copy') — supports text, images, and rich content with proper permission handling.

Explanation

navigator.clipboard.writeText(text) and navigator.clipboard.readText() are Promise-based. For rich content: clipboard.write([new ClipboardItem({'text/html': blob})]).Read requires user permission (clipboard-read) and is only available in focused pages. Write usually doesn't require permission if triggered by user action. The old document.execCommand('copy') is deprecated — it only works synchronously and only with text. Security: clipboard access is restricted to secure contexts (HTTPS) and requires user gesture for write in some browsers.

Common Misconception

navigator.clipboard.writeText() works everywhere without permissions — it requires HTTPS (secure context) and a user gesture in some browsers.

Why It Matters

The async Clipboard API enables rich clipboard operations (images, HTML) that were impossible with the legacy execCommand approach.

Common Mistakes

  • Using document.execCommand('copy') — deprecated and unreliable across browsers.
  • Not wrapping clipboard operations in try/catch — permission denied throws.
  • Calling readText() without user interaction — blocked by browsers.

Code Examples

✗ Vulnerable
// Deprecated approach:
document.execCommand('copy'); // Deprecated, unreliable
✓ Fixed
async function copyToClipboard(text) {
    try {
        await navigator.clipboard.writeText(text);
        showToast('Copied!');
    } catch (err) {
        // Fallback for older browsers:
        const el = document.createElement('textarea');
        el.value = text;
        document.body.appendChild(el);
        el.select();
        document.execCommand('copy');
        document.body.removeChild(el);
    }
}

Added 23 Mar 2026
Edited 5 Apr 2026
Views 52
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 1 ping S 1 ping M 2 pings T 0 pings W 1 ping T 1 ping F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 Google 6 Perplexity 6 ChatGPT 4 Unknown AI 4 Scrapy 4 Ahrefs 3 SEMrush 3 Meta AI 2 Majestic 1 Claude 1
crawler 37 crawler_json 2 pre-tracking 2
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Replace document.execCommand('copy') with await navigator.clipboard.writeText(text). Always wrap in try/catch and provide a fallback.
📦 Applies To
javascript HTML5 web
🔗 Prerequisites
🔍 Detection Hints
execCommand\('copy'\)
Auto-detectable: ✓ Yes eslint
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Function


✓ schema.org compliant