ClipboardItem - Web APIs 编辑

Draft

This page is not complete.

The ClipboardItem interface of the Clipboard API represents a single item format, used when reading or writing data via the Clipboard API. That is clipboard.read() and clipboard.write() respectively.

The benefit of having the ClipboardItem interface to represent data, is that it enables developers to cope with the varying scope of file types and data easily.

Access to the contents of the clipboard is gated behind the Permissions API: The clipboard-write permission is granted automatically to pages when they are in the active tab. The clipboard-read permission must be requested, which you can do by trying to read data from the clipboard.

Note: To work with text see the Clipboard.readText() and Clipboard.writeText() methods of the Clipboard interface.

Note: You can only pass in one clipboard item at a time.

Constructor

ClipboardItem.ClipboardItem()
Creates a new ClipboardItem object, with the MIME type as the key and Blob as the value

Properties

This interface provides the following properties.

types Read only
Returns an Array of MIME types available within the ClipboardItem.

Methods

This interface defines the following methods.

getType()
Returns a Promise that resolves with a Blob of the requested MIME type, or an error if the MIME type is not found.

Examples

Writing To Clipboard

Here we're writing a new ClipboardItem.ClipboardItem() to the clipboard by requesting a png image using the Fetch API, and in turn, the responses' blob() method, to create the new ClipboardItem.

async function writeClipImg() {
  try {
    const imgURL = '/myimage.png';
    const data = await fetch(imgURL);
    const blob = await data.blob();

    await navigator.clipboard.write([
      new ClipboardItem({
        [blob.type]: blob
      })
    ]);
    console.log('Fetched image copied.');
  } catch(err) {
    console.error(err.name, err.message);
  }
}

Reading From The Clipboard

Here we're returning all items on the clipboard via the clipboard.read() method. Then utilizing the ClipboardItem.types property to set the getType() argument and return the corresponding blob object.

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);
  }
}

Specifications

SpecificationStatusComment
Clipboard API and events
The definition of 'ClipboardItem' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

Note: Image format support varies by browser. See the browser compatibility table for the Clipboard interface.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:53 次

字数:6671

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文