我有一个图像作为 DataURL 字符串。
我想以编程方式将此图像复制到剪贴板。
我找到了两个函数,但都不起作用。 虽然,当您复制文本时,第一个函数运行良好 - copy("Hello!","text");
PS 我有“clipboardWrite”权限。
首先:
function copy(str, mimetype) {
document.oncopy = function(event) {
event.clipboardData.setData(mimetype, str);
event.preventDefault();
};
document.execCommand("Copy", false, null);
}
第二个:
function copyImage(url){
var img=document.createElement('img');
img.src=url;
document.body.appendChild(img);
var r = document.createRange();
r.setStartBefore(img);
r.setEndAfter(img);
r.selectNode(img);
var sel = window.getSelection();
sel.addRange(r);
document.execCommand('Copy');
}
最佳答案
这似乎是不可能的。自 2012 年以来,Chrome 中一直存在阻止它的错误! https://bugs.chromium.org/p/chromium/issues/detail?id=150835
关于javascript - 将图像复制到剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25096284/