ClipboardItem.getType()

getType()ClipboardItem インターフェイスのメソッドで、要求された MIME タイプBlob で解決するプロミス (Promise) を返します。 MIME タイプが見つからない場合はエラーになります。

構文

js
getType(type);

引数

type

有効な MIME タイプです。

返値

Blob オブジェクトで解決するプロミス (Promise)です。

例外

NotFoundError DOMException

type が既知の MIME タイプに一致しない。

TypeError

引数が指定されなかった、または typeClipboardItem のものではない。

次の例では、 clipboard.read() メソッドによってクリップボード上のすべてのアイテムを返しています。そして、 ClipboardItem.types プロパティを利用して 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);
  }
}

仕様書

Specification
Clipboard API and events
# dom-clipboarditem-gettype

ブラウザーの互換性

BCD tables only load in the browser

関連情報