使用firefox webextension替代插件剪贴板SDK复制图像 [英] firefox webextension alternative to addon clipboard sdk for copying images
问题描述
我正在尝试开发Firefox扩展,其中涉及将图像复制到剪贴板。过去,似乎使用 剪贴板
插件SDK。但是,此方法已被弃用,因此我需要找到另一种将图像复制到剪贴板的方法。 提到的文档使用 document.execCommand('copy')
但我无法将其用于复制图像。
I am trying to develop a Firefox extension which involves copying an image to the clipboard. In the past, it appears that this was accomplished using the clipboard
addon sdk. However, this is being deprecated so I need to find another way to copy an image to the clipboard. The docs mentioned using document.execCommand('copy')
but I cant get that to work for copying images.
从网上搜索似乎通常无法将图像复制到Javascript剪贴板中,但我想知道Firefox是否具有某种webextensions API来访问
From searching the web it seems that its normally not possible to copy an image to the clipboard in Javascript but I was wondering if Firefox has some sort of webextensions API to access the clipboard.
编辑:这是我一直在尝试复制图像的代码:
here is the code I have been using to try to copy images:
document.body.appendChild(img);
let range = document.createRange();
range.setStartBefore(img);
range.setEndAfter(img);
range.selectNode(img);
window.getSelection().addRange(range);
var successful = document.execCommand('copy');
window.getSelection().removeAllRanges();
document.body.removeChild(img);
img
是HTML图像元素。
运行时什么也没有发生
img
is an HTML image element.
Nothing happens when it runs, though
推荐答案
WebExtensions具有 setImageData()
。
WebExtensions has setImageData()
.
复制与扩展名:
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'));
clipboard.setImageData()-Mozilla | MDN
这篇关于使用firefox webextension替代插件剪贴板SDK复制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!