0

I'm using the following event that fires when a user copies data from the page to the clipboard. But, I can't seem to get the content of the copied data. Is it possible?

$(document).on('copy', function(e){ 
    console.log(e);
});

I've used Chrome's inspector to inspect the object e. There's a property called origionalEvent that has a property origionalText. This only seems to contain the first line of the copied text.

Maybe I'm missing something obvious but I can't seem to figure out how to get the data copied.

Is it possible?

love2node
  • 347
  • 1
  • 3
  • 14

2 Answers2

2

If content has been selected to copy, this will get you the selected content at the time of the copy event:

$(document).on('copy', function(e){ 
    console.log(window.getSelection().toString());
    });
grateful
  • 1,128
  • 13
  • 25
1

Try it :

<html>
    <head></head>
    <body>
        <p>This is test</p>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            $("p").on("copy",function(){
                var sel = document.getSelection();
                alert(sel);
            })
        </script>
    </body>
</html>
Ehsan
  • 12,655
  • 3
  • 25
  • 44