I have an image as a DataURL string.
And I want to copy this image programmatically to ClipBoard.
I found two functions, but neither of them works. Though, first function works well when you copy text - copy("Hello!","text");
PS I have a "clipboardWrite" permission.
First:
function copy(str, mimetype) {
document.oncopy = function(event) {
event.clipboardData.setData(mimetype, str);
event.preventDefault();
};
document.execCommand("Copy", false, null);
}
Second:
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');
}
Seems this isn't possible. There has been a bug preventing it in Chrome since 2012! https://bugs.chromium.org/p/chromium/issues/detail?id=150835
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With