npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@fuzzyos/clipboard

v0.3.6

Published

Cross-platform clipboard operations for Node.js

Readme

Clipboard

NPM Version CI

NPM Package: https://www.npmjs.com/package/@crosscopy/clipboard

GitHub: https://github.com/crosscopy/clipboard

跨平台原生剪贴板库,适用于 Node.js。支持读取、写入和监听系统剪贴板,兼容多种数据格式。

支持的格式

  • 纯文本
  • HTML
  • 富文本 (RTF)
  • 图片 (PNG 二进制 / base64)
  • 文件
  • 任意二进制数据 (自定义格式字符串)

安装

npm install @crosscopy/clipboard
# 或
pnpm add @crosscopy/clipboard

快速开始

import {
  getText, setText,
  hasText, hasImage,
  getImageBase64,
  startWatch,
} from '@crosscopy/clipboard'

// 读写文本
await setText('hello world')
console.log(await getText()) // "hello world"

// 检查剪贴板内容类型
console.log(hasText())  // true
console.log(hasImage()) // false

// 监听剪贴板变化
const watcher = startWatch(() => {
  console.log('剪贴板内容已变化!')
})
// ... 之后停止监听
watcher.stop()

API

完整的类型声明请参阅 index.d.ts

纯文本

| 函数 | 签名 | 说明 | |---|---|---| | hasText | () => boolean | 检查剪贴板是否包含文本 | | getText | () => Promise<string> | 从剪贴板读取文本 | | setText | (text: string) => Promise<void> | 将文本写入剪贴板 |

import { hasText, getText, setText } from '@crosscopy/clipboard'

await setText('已复制!')
if (hasText()) {
  console.log(await getText())
}

HTML

| 函数 | 签名 | 说明 | |---|---|---| | hasHtml | () => boolean | 检查剪贴板是否包含 HTML | | getHtml | () => Promise<string> | 从剪贴板读取 HTML 字符串 | | setHtml | (html: string) => Promise<void> | 将 HTML 字符串写入剪贴板 |

import { setHtml, getHtml } from '@crosscopy/clipboard'

await setHtml('<b>粗体</b>')
console.log(await getHtml())

富文本 (RTF)

| 函数 | 签名 | 说明 | |---|---|---| | hasRtf | () => boolean | 检查剪贴板是否包含 RTF | | getRtf | () => Promise<string> | 从剪贴板读取 RTF 字符串 | | setRtf | (rtf: string) => Promise<void> | 将 RTF 字符串写入剪贴板 |

图片

| 函数 | 签名 | 说明 | |---|---|---| | hasImage | () => boolean | 检查剪贴板是否包含图片 | | getImageBinary | () => Promise<Array<number>> | 以 PNG 字节数组形式读取图片 | | getImageBase64 | () => Promise<string> | 以 base64 编码的 PNG 形式读取图片 | | setImageBinary | (imageBytes: Array<number>) => Promise<void> | 将 PNG 字节数组写入剪贴板 | | setImageBase64 | (base64Str: string) => Promise<void> | 将 base64 编码的 PNG 写入剪贴板 |

import { hasImage, getImageBase64, setImageBinary } from '@crosscopy/clipboard'
import { readFileSync } from 'fs'

// 将 PNG 文件写入剪贴板
const png = readFileSync('screenshot.png')
await setImageBinary(Array.from(png))

// 以 base64 形式读取
if (hasImage()) {
  const b64 = await getImageBase64()
  console.log(`data:image/png;base64,${b64}`)
}

文件

| 函数 | 签名 | 说明 | |---|---|---| | hasFiles | () => boolean | 检查剪贴板是否包含文件引用 | | getFiles | () => Promise<Array<string>> | 从剪贴板读取文件路径列表 | | setFiles | (files: Array<string>) => Promise<void> | 将文件路径列表写入剪贴板 |

import { hasFiles, getFiles } from '@crosscopy/clipboard'

if (hasFiles()) {
  const paths = await getFiles()
  console.log(paths) // ["/Users/you/file.txt", ...]
}

Buffer (自定义格式)

| 函数 | 签名 | 说明 | |---|---|---| | getBuffer | (format: string) => Promise<Array<number>> | 按指定格式读取原始字节 | | setBuffer | (format: string, buffer: Array<number>) => Promise<void> | 按指定格式写入原始字节 |

import { setBuffer, getBuffer } from '@crosscopy/clipboard'

await setBuffer('com.myapp.custom', [0x01, 0x02, 0x03])
const data = await getBuffer('com.myapp.custom')

工具函数

| 函数 | 签名 | 说明 | |---|---|---| | availableFormats | () => Array<string> | 列出剪贴板中当前所有的数据格式 | | clear | () => Promise<void> | 清空剪贴板 |

剪贴板监听

使用操作系统原生通知机制监听剪贴板变化。

| 函数 / 方法 | 签名 | 说明 | |---|---|---| | startWatch | (callback: () => void) => ClipboardWatcherJs | 开始监听,返回监听句柄 | | watcher.stop() | () => void | 停止监听 | | watcher.isRunning | boolean (getter) | 监听器是否正在运行 |

import { startWatch, getText } from '@crosscopy/clipboard'

const watcher = startWatch(async () => {
  console.log('剪贴板已变化:', await getText())
})

console.log(watcher.isRunning) // true

// 不再需要时停止监听
watcher.stop()
console.log(watcher.isRunning) // false

监听器在后台线程中运行,使用操作系统原生的剪贴板变化事件,JavaScript 侧无需轮询。使用完毕后调用 stop() 释放资源。

平台支持

| 平台 | 架构 | |---|---| | macOS | x64, arm64 (Apple Silicon), universal | | Windows | x64, arm64 | | Linux | x64 (glibc) |

致谢

发布

所有构建和发布流程由 GitHub Actions 完成。

执行 npm version patch 升级版本号,然后 git push --follow-tags 推送到 GitHub,GitHub Actions 会自动构建并发布。