Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect a "copy image" browser event?

Using jquery, I can detect when the user copies something (like text) with ctrl-c or through the context menu:

$(document).on('copy',function(e){$('body').prepend('copy event <br>');});

However, the event doesn't seem to trigger when an image is copied. How can I detect image-copy? Specifically I'd like to detect copying from a <canvas> element, but any <img> should do as a starting point for understanding this problem.

Test scenario: http://jsfiddle.net/jm23xe8w/

like image 720
tmpearce Avatar asked Nov 03 '14 15:11

tmpearce


People also ask

How do I copy an image from the browser?

How to copy an image from a web page. To save (download) an image, right-click any image on a website to view the properties menu for that image, as shown below. If you want to copy that image into another document, click the Copy image option from the menu.

How to get data from clipboard in js?

read() The read() method of the Clipboard interface requests a copy of the clipboard's contents, delivering the data to the returned Promise when the promise is resolved. Unlike readText() , the read() method can return arbitrary data, such as images. This method can also return text.

How do you copy an image into HTML?

Copy and paste your image URL into an IMG tag, add a SRC to it. Identify first where you'd like to place your image within the HTML and insert the image tag, <img>. Then take your uploaded image, copy the URL and place it within your img parameters prefaced by a src.

How do I copy an image to the clipboard?

Go to the Images folder and look for the image you want to copy. Long press the image. Tap on the copy icon at the bottom left. Your image is now copied to the clipboard.


1 Answers

Browsers doesn't have copy images event,so you need to simulate it by some tricks.also clipboard doesn't save images in it.Clipboard only saves texts in itself.You need to convert images to a base64 encoded string then save it to clipboard and paste it on your app.But there is an issue with this that Are the clipboard data , the copied images or there are some another data of some another application in it.For this, you can add a unique string to your encoded string at first and or at end and check it wherever you want to paste it.For converting the images to base64encode string you can use this code :

function getImageData(img) {
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    var imgd = canvas.toDataURL("image/png");
    return imgd;
}

Pass the ‍‍‍‍img tag to this function to get your result.

like image 148
OnlyMAJ Avatar answered Sep 28 '22 07:09

OnlyMAJ