--- title: clipboard.setImageData() slug: Mozilla/Add-ons/WebExtensions/API/clipboard/setImageData tags: - API - Clipboard - 剪贴板 - 参考 - 拓展 - 方法 translation_of: Mozilla/Add-ons/WebExtensions/API/clipboard/setImageData original_slug: Mozilla/Add-ons/WebExtensions/API/剪切板/setImageData ---
{{AddonSidebar()}}

将图像复制到剪贴板。在将图像写入剪贴板之前,会对图像进行重新编码。如果图像无效,则不会修改剪贴板。

图像被作为包含经过编码的图像的 ArrayBuffer 提供。支持 JPEG 和 PNG 格式。

基于 Chrome 的 clipboard.setImageData() API,但存在一些差异:

这是一个返回 Promise 的异步函数。

语法

browser.clipboard.setImageData(imageData, imageType)

参数

imageData
An ArrayBuffer containing the encoded image data to copy to the clipboard.
imageType
A {{domxref("DOMString")}} indicating the type of image contained in imageData: "png" or "jpeg".

返回值

A Promise 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).

浏览器兼容性

{{Compat("webextensions.api.clipboard.setImageData", 10)}}

示例

Copy a remote image:

// requires:
// * the host permission for "https://cdn.mdn.mozilla.net/*"
// * the API permission "clipboardWrite"

fetch('https://cdn.mdn.mozilla.net/static/img/favicon144.png')
.then(response => response.arrayBuffer())
.then(buffer => browser.clipboard.setImageData(buffer, 'png'));

Copy an image that was bundled with the extension:

// requires the API permission "clipboardWrite"

fetch(browser.runtime.getURL('image.png'))
.then(response => response.arrayBuffer())
.then(buffer => browser.clipboard.setImageData(buffer, 'png'));

{{WebExtExamples}}

说明

 此 API 基于 Chromium 的 chrome.clipboard API.