0

I want to copy the image itself not any text or related src.

I've made a jsfiddle example https://jsfiddle.net/pvuefca7/1/

You can play around and see that copying text works fine but for the image it does not. i think it has something to do with <img> not being a node or something along those lines. i'm asking if there is any way of selecting a specific image and coping it. the document.execCommand('Copy'); command will copy an image if the image is the only element, IE opening an image in a new tab and running the command in the console. but i need a way of coping an image when its not the only element.

Wonder
  • 31
  • 2
  • 8
  • 1
    http://stackoverflow.com/questions/33175909/copy-image-to-clipboard – Jeremy Jackson Dec 07 '16 at 19:13
  • This doesnt copy the image but the alt text – Wonder Dec 07 '16 at 19:24
  • What is expected result of pasting copied image? Where would image be pasted? – guest271314 Dec 07 '16 at 19:33
  • The exact expected result of pasting an image you right clicked and pressed 'copy image' - i want the image it self – Wonder Dec 07 '16 at 19:37
  • @Wonder Yes, where will copied image be pasted? At an `html` `document`? At user filesystem? What are you trying to achieve? – guest271314 Dec 07 '16 at 19:39
  • I have made a java app that reads captcha from an image and returns text, the thing is that now it uses screenshot and screenshots are not high enough quality and the results are less accurate, i have found that by using a copied image that is stored in the clipboard the results are almost always accurate. so i need a js script that copies to the clipboard the image. Also, as this is a captcha image there are security checks that do not allow me to simply download the png the only way is screenshot or copying to the clipboard – Wonder Dec 07 '16 at 19:46
  • Is the image at jsfiddle the image that should be copied to clipboard? – guest271314 Dec 07 '16 at 19:52
  • its just an example.. i need a function that is able to copy an image. for now, yes, this image will do – Wonder Dec 07 '16 at 19:53
  • @Wonder, the image does get copied to the clipboard in your plunker. The problem is you have to make the other application to support pasting of the copied image. If you try to paste the copied image in a ms doc, you will see the image there. – Anthony C Dec 07 '16 at 20:57
  • How is copied image passed to `java` application? – guest271314 Dec 07 '16 at 21:06
  • @AnthonyC No, it does not get pasted even when using ms paint, i dont believe the image is really copied. – Wonder Dec 08 '16 at 04:16
  • @guest271314 Java can read your clipboard contents – Wonder Dec 08 '16 at 04:16
  • @Wonder, works for me in ms doc. http://imgur.com/a/tpEfx . like i said earlier, the application has to have support to read the copied image from clipboard. As you have tested, ms paint doesn't support that. – Anthony C Dec 08 '16 at 04:44

1 Answers1

2

You can request image as Blob at click of button; set .value of <textarea> element to data URI of image within FileReader load event following call to .readAsDataURL(); select .value of textarea; prompt user to press CTRL+C; at copy event handler set .value of textarea at event.clipboardData

<div>
  <img id="image" width="100" src="https://placehold.it/100x100?text=✔">
  <button onclick="copyElement('image');">Copy image</button>
</div>
<script>
function copyElement(id) {
  var element = document.getElementById(id);
  var text = document.createElement("textarea");

  document.oncopy = function(e) {
    e.clipboardData.setData("text/plain", text.value);
    console.log(e.clipboardData.getData("text/plain"));
  }

  fetch(element.src.replace(/^(http:|https:)/, location.protocol))
    .then(function(response) {
      return response.blob()
    })
    .then(function(blob) {
      var reader = new FileReader();
      reader.onload = function() {
        document.body.appendChild(text);
        text.value = reader.result;
        text.select();
        alert("Press CTRL+C to copy image to clipboard");
      }
      reader.readAsDataURL(blob)
    })
}

</script>
guest271314
  • 1
  • 15
  • 104
  • 177
  • I will try this later on, i have to go to work now. just one more question if thats ok, i will probably confirm this later but if you could answer before that would be great. will this function make a new request for the image? because as i told you this involves captcha that regenerates every time its requested. – Wonder Dec 08 '16 at 04:20
  • 1
    @Wonder Yes, the image could be requested again, depending on browser cache settings. Not sure how to try without the actual captcha and page environment that you are describing? – guest271314 Dec 08 '16 at 04:25
  • I've tried it and it doesn't work, as i mentioned before the captcha regenerates with every new request - this mean that i will get a different captcha then the one displayed in the registeration page. i've placed your code with a captcha link http://codepen.io/anon/pen/QGxmeq You see, if you press the copy button twice you get two different results even though the captcha picture it self never changes. – Wonder Dec 08 '16 at 17:21
  • 1
    @Wonder The image is not server with `Access-Control-Allow-Origin` header – guest271314 Dec 08 '16 at 17:27
  • yes, i know i'm using a cors chrome extention that allows different origins, you can use this or you can just try it out on their website in the openaccount page – Wonder Dec 08 '16 at 17:31
  • 1
    @Wonder If you are using a chrome extension to allow different origins you can also use a chrome extension to capture the response body of all requests and read the image data directly from the captured response. – guest271314 Dec 08 '16 at 17:35
  • The use of an extension is only necessary because i exported the captcha to codepen.. you dont need this extension on the original website. – Wonder Dec 08 '16 at 17:54
  • 1
    @Wonder Yes, though using an extension would allow reading of response body from requests made at the `document`. – guest271314 Dec 08 '16 at 18:05
  • do you know of such extension? – Wonder Dec 08 '16 at 18:16
  • 1
    https://developer.chrome.com/extensions/webRequest, http://stackoverflow.com/questions/11593853/intercept-http-request-body-from-chrome-extension – guest271314 Dec 08 '16 at 18:21
  • Well, thank you for your time, i'll give it a try :) – Wonder Dec 08 '16 at 18:23