Movatterモバイル変換


[0]ホーム

URL:


  1. Mozilla
  2. Add-ons
  3. Browser extensions
  4. JavaScript APIs
  5. clipboard
  6. clipboard.setImageData()

clipboard.setImageData()

Copies an image to the clipboard. The image is re-encoded before it is written to the clipboard. If the image is invalid, the clipboard is not modified.

The image is provided as anArrayBuffer containing the encoded image. JPEG and PNG formats are supported.

Although this API is based on Chrome'sclipboard.setImageData() API, there are some differences:

  • The Chrome API is only for apps, not extensions.
  • This API requires only the"clipboardWrite" permission, while the Chrome version also requires the"clipboard" permission.
  • Chrome's API uses callbacks, and this API only supports promises.
  • This API does not support theadditionalItems parameter.

This is an asynchronous function that returns aPromise.

Syntax

js
browser.clipboard.setImageData(imageData, imageType)

Parameters

imageData

AnArrayBuffer containing the encoded image data to copy to the clipboard.

imageType

A string indicating the type of image contained inimageData:"png" or"jpeg".

Return value

APromise that will be resolved with no arguments if the operation succeeded, or rejected if there was an error (for example, because the data did not represent a valid image).

Examples

Copy a remote image:

js
// requires:// * the host permission for "https://mdn.github.io/*"// * the API permission "clipboardWrite"fetch("https://mdn.github.io/shared-assets/images/examples/favicon144.png")  .then((response) => response.arrayBuffer())  .then((buffer) => browser.clipboard.setImageData(buffer, "png"));

Copy an image that was bundled with the extension:

js
// requires the API permission "clipboardWrite"fetch(browser.runtime.getURL("image.png"))  .then((response) => response.arrayBuffer())  .then((buffer) => browser.clipboard.setImageData(buffer, "png"));

Browser compatibility

Note:This API is based on Chromium'schrome.clipboard API.

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp