ClipboardItem: types 属性

Baseline 2024
新推出

自 2024 年 6 月起,此功能已在最新的设备和浏览器版本中可用。此功能可能不适用于较旧的设备或浏览器。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

ClipboardItem 接口的只读属性 types 返回一个 Array,其中包含 ClipboardItem 中可用的 MIME 类型

一个包含可用 MIME 类型Array

示例

在下面的示例中,我们使用 Clipboard.read() 方法返回剪贴板上的所有项目,然后检查 types 属性以获取可用类型,最后使用 ClipboardItem.getType() 方法将每个数据项作为 Blob 返回。如果未找到指定类型的剪贴板内容,则返回错误。

js
async function getClipboardContents() {
  try {
    const clipboardItems = await navigator.clipboard.read();

    for (const clipboardItem of clipboardItems) {
      for (const type of clipboardItem.types) {
        const blob = await clipboardItem.getType(type);
        // we can now use blob here
      }
    }
  } catch (err) {
    console.error(err.name, err.message);
  }
}

规范

规范
Clipboard API 和事件
# dom-clipboarditem-types

浏览器兼容性

另见