本文目录导读:

浏览器环境
使用 Clipboard API(现代浏览器)
async function getClipboardType() {
try {
const clipItems = await navigator.clipboard.read();
if (clipItems.length > 0) {
const item = clipItems[0];
const types = item.types;
return types;
}
return [];
} catch (err) {
console.error('读取剪贴板失败:', err);
return [];
}
}
// 使用示例
getClipboardType().then(types => {
if (types.includes('image/png')) {
console.log('剪贴板包含 PNG 图片');
} else if (types.includes('text/plain')) {
console.log('剪贴板包含纯文本');
} else if (types.includes('text/html')) {
console.log('剪贴板包含 HTML');
}
});
使用 ClipboardEvent(监听粘贴事件)
document.addEventListener('paste', (event) => {
const clipData = event.clipboardData || window.clipboardData;
const items = clipData.items;
for (let item of items) {
console.log(`MIME类型: ${item.type}, 类型: ${item.kind}`);
// kind 可能是 'string' 或 'file'
}
// 获取所有支持的格式
const types = clipData.types;
console.log('所有格式:', types);
});
Node.js 环境
使用 clipboardy 库
const clipboardy = require('clipboardy');
// 检查是否有特殊格式(需要额外库)
async function getClipboardTypes() {
try {
// 只能获取文本内容
const text = clipboardy.readSync();
if (text) {
return ['text/plain'];
}
return [];
} catch (err) {
return [];
}
}
使用 clipboards 库(支持更多格式)
const clipboards = require('clipboards');
async function getClipboardTypes() {
try {
const clipboard = new clipboards.Clipboard();
const data = await clipboard.read();
const types = Object.keys(data);
return types;
} catch (err) {
console.error('读取失败:', err);
return [];
}
}
Python 环境
使用 pyperclip
import pyperclip
def get_clipboard_type():
try:
# pyperclip 只能处理文本
text = pyperclip.paste()
if text:
return ['text/plain']
return []
except Exception as e:
print(f"读取剪贴板失败: {e}")
return []
使用 PIL + pyperclip(检查图片)
import pyperclip
from PIL import ImageGrab
def get_clipboard_types():
types = []
# 检查文本
try:
text = pyperclip.paste()
if text:
types.append('text/plain')
except:
pass
# 检查图片
try:
img = ImageGrab.grabclipboard()
if img:
types.append('image/png')
except:
pass
return types
常见剪贴板类型
| MIME类型 | 描述 | 类型标识 |
|---|---|---|
| text/plain | 纯文本 | 'string' |
| text/html | HTML | 'string' |
| image/png | PNG图片 | 'file' |
| image/jpeg | JPEG图片 | 'file' |
| text/uri-list | URL | 'string' |
| application/rtf | RTF格式 | 'string' |
完整示例(浏览器版)
async function checkClipboardContent() {
// 请求权限
try {
await navigator.permissions.query({ name: 'clipboard-read' });
} catch (err) {
console.log('可能需要用户交互触发');
}
// 获取剪贴板内容
const clipItems = await navigator.clipboard.read();
if (clipItems.length === 0) {
console.log('剪贴板为空');
return;
}
const clipItem = clipItems[0];
// 获取所有类型
const types = clipItem.types;
console.log('剪贴板包含以下类型:');
for (const type of types) {
try {
if (type.startsWith('text/')) {
const text = await clipItem.getType(type);
const reader = new FileReader();
reader.onload = () => console.log(`${type}: ${reader.result.substring(0, 100)}...`);
reader.readAsText(text);
} else if (type.startsWith('image/')) {
const blob = await clipItem.getType(type);
console.log(`${type}: 图片大小 ${blob.size} 字节`);
}
} catch (err) {
console.log(`无法读取 ${type}:`, err);
}
}
return types;
}
注意事项
- 浏览器权限:Clipboard API 需要用户交互和 HTTPS 环境
- 跨平台限制:不同操作系统对剪贴板格式支持不同
- 安全考虑:不要读取用户不期望读取的剪贴板内容
- 兼容性:
navigator.clipboard.read()在较新浏览器才支持
选择哪种方法取决于你的运行环境和需求,浏览器环境推荐使用 Clipboard API,如果需要跨平台支持则考虑第三方库。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播