I want to know if it is possible to copy an image from my website to the clipboard in the same way that you can copy an image from google images making right click in the image and selecting "Copy image", in this case you can paste the image in every places (whatsapp web, gmail, paint, file system...)
I am trying to copy an image through a button to the clipboard but I only can paste this image to some places (gmail, twitter) it does not work in whatsapp web or paint for example.
This is the link to the website:
https://project1-evercam.herokuapp.com/image-editor/example01-basic.html
This is the js code that I am using:
var $clipboard = $('#clipboard');
$clipboard.on('click',function(e){
var img = document.createElement('img');
img.src = canvas.toDataURL('image/jpeg', 0.1);
var div1 = document.createElement('div');
div1.contentEditable = true;
div1.appendChild(img);
document.body.appendChild(div1);
SelectText(div1);
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(div1);
});
function SelectText(element) {
var doc = document;
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
I use this solution here, and it is the only solution that I could find in stackoverflow. I would like to know if it is the best way to do this and it does not work correctly or there are a better way.