Scripts2026年7月21日·1 分钟阅读

Clipboard.js — Modern Copy to Clipboard with No Flash

A tiny 3KB library that provides clean, modern copy-to-clipboard functionality using Selection and execCommand APIs, with no Flash fallback needed.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 29/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
Clipboard.js Overview
安全暂存命令
npx -y tokrepo@latest install 1ed6887e-8547-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Clipboard.js removes the pain of implementing copy-to-clipboard in the browser. Before it existed, developers relied on Flash-based solutions or fragile hacks. At just 3KB gzipped, it uses the Selection API and execCommand under the hood and works across all modern browsers without any framework dependency.

What Clipboard.js Does

  • Copies text, HTML, or input values to the system clipboard on click
  • Works with any trigger element using data-clipboard-* attributes
  • Supports programmatic usage with a clean constructor API
  • Fires success and error events for custom feedback UIs
  • Requires zero dependencies and no Flash plugins

Architecture Overview

Clipboard.js listens for click events on elements matching a given selector. When triggered, it creates a temporary, off-screen textarea, populates it with the target content, executes document.execCommand('copy'), and removes the element. Modern browsers that support the Clipboard API can be leveraged as well. The library is written in vanilla JavaScript and distributed as a UMD module.

Self-Hosting & Configuration

  • Install via npm (clipboard) or include the CDN script tag
  • Instantiate with new ClipboardJS(selector) or new ClipboardJS(selector, options)
  • Use data-clipboard-target to copy content from another element by CSS selector
  • Use data-clipboard-action to choose between copy (default) and cut
  • Call .destroy() to remove all event listeners when done

Key Features

  • Tiny footprint at 3KB gzipped with zero dependencies
  • Declarative API through data attributes for no-JS-needed setup
  • Programmatic control via text and target option functions
  • Event-based feedback with success and error callbacks
  • Works in all modern browsers including IE9+ via graceful degradation

Comparison with Similar Tools

  • Navigator.clipboard API — native but requires HTTPS, user gesture, and permissions; Clipboard.js abstracts these concerns
  • document.execCommand — the raw API Clipboard.js wraps; using it directly requires boilerplate textarea management
  • copy-to-clipboard — similar npm package but Clipboard.js offers richer declarative attributes
  • react-copy-to-clipboard — React-specific wrapper; Clipboard.js is framework-agnostic

FAQ

Q: Does it work without HTTPS? A: Yes. Unlike the native Clipboard API, Clipboard.js uses execCommand which works on HTTP pages in most browsers.

Q: Can I use it with React or Vue? A: Absolutely. Instantiate it in a lifecycle hook or useEffect, passing a ref or selector. Community wrappers also exist for both frameworks.

Q: What happens if copying fails? A: The error event fires, letting you show a fallback message like "Press Ctrl+C to copy." The selected text remains highlighted for manual copying.

Q: Is Flash required? A: No. Clipboard.js was created specifically to eliminate Flash-based clipboard solutions like ZeroClipboard.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产