1

I am using Html5 and JQuery.

Html

<canvas style="border:1px solid grey;" id="cc" width="1000" height="800">

Javascript:

var pasteCatcher;
if (!window.Clipboard){
 if(navigator.userAgent.indexOf("Firefox")>0){
    pasteCatcher = document.createElement("div");
    pasteCatcher.setAttribute("id", "paste_ff");
    pasteCatcher.setAttribute("contenteditable", "");
    pasteCatcher.style.cssText = 'opacity:0;position:fixed;top:0px;left:0px;';
    pasteCatcher.style.marginLeft = "-20px";
    document.body.appendChild(pasteCatcher);
    pasteCatcher.focus();

    document.getElementById('paste_ff').addEventListener('DOMSubtreeModified',function(){
        if(pasteCatcher.children.length == 1){
                        var canvas = document.getElementById("cc");
                         var ctx = canvas.getContext("2d");
            img = pasteCatcher.firstElementChild.src;
            var img2 = new Image();
            img2.onload = function(){
                ctx.drawImage(img2, 0, 0);
                }
            img2.src = img;
            pasteCatcher.innerHTML = '';
            }
        },false);
    }
 }

Above code is working fine. When the clipboard has image then that will be set to canvas. IF the clipboard has text then how can i get the text and draw in canvas?

Thanks!

user13500
  • 3,817
  • 2
  • 26
  • 33
user755806
  • 6,565
  • 27
  • 106
  • 153

1 Answers1

0

As you've discovered, access to the clipboard varies across browsers for security reasons.

Even the methods used to read/write the clipboard vary.

A workaround:

Have the users paste the text into an html textarea and then read the textarea value.

The benefits of the workaround are:

  • The user must paste somewhere anyway, so let that be the textarea.
  • The textarea can be edited by the user if they desire.
  • The textarea value is not restricted for security reasons, so it works cross-browser.
markE
  • 102,905
  • 11
  • 164
  • 176