firefox webextension alternative to addon clipboard sdk for copying images

笑着哭i 提交于 2019-12-13 00:02:39

问题


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.

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.

Edit: 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 is an HTML image element. Nothing happens when it runs, though


回答1:


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



来源:https://stackoverflow.com/questions/43007323/firefox-webextension-alternative-to-addon-clipboard-sdk-for-copying-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!