Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Image to Clipboard

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');
}
like image 785
holden321 Avatar asked Aug 02 '14 14:08

holden321


1 Answers

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

like image 64
ngstschr Avatar answered Oct 28 '22 23:10

ngstschr