概述`navigator.clipboard.read()` 允许读取剪贴板项目(图片/富文本/纯文本),需在安全上下文并获得用户授权。应用应仅读取必要类型并进行清理。示例if (navigator.clipboard.read) {
const items = await navigator.clipboard.read()
for (const item of items) {
if (item.types.includes('image/png')) {
const blob = await item.getType('image/png')
preview(blob)
} else if (item.types.includes('text/plain')) {
const text = await (await item.getType('text/plain')).text()
processText(text)
}
}
} else {
// 回退到输入选择与粘贴事件
}
工程建议权限与手势:在用户手势后读取;提示用途与类型。安全与清理:对 HTML 进行 Sanitizer 清理;限制图片大小与类型。兼容:不支持时回退到 `paste` 事件与输入方案。参考与验证MDN Clipboard API 文档:https://developer.mozilla.org/docs/Web/API/Clipboard/readChrome 平台文档:https://developer.chrome.com/docs/web-platform/async-clipboard/

发表评论 取消回复