--- 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 ---
将图像复制到剪贴板。在将图像写入剪贴板之前,会对图像进行重新编码。如果图像无效,则不会修改剪贴板。
图像被作为包含经过编码的图像的 ArrayBuffer
提供。支持 JPEG 和 PNG 格式。
基于 Chrome 的 clipboard.setImageData()
API,但存在一些差异:
"clipboardWrite"
权限,Chrome 版本需要 "clipboard"
权限。additionalItems
参数。这是一个返回 Promise
的异步函数。
browser.clipboard.setImageData(imageData, imageType)
imageData
ArrayBuffer
containing the encoded image data to copy to the clipboard.imageType
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.