How do I get what's copied to the clipboard and put it in a variable? [duplicate]

3

It would be like this: when copying anything using CONTROL + C , I need to put that content into a variable using JavaScript. I did not find on the internet how to do this. Someone help? Detail: It is by extension. It would be for me to write what is in the clipboard in a variable so I can execute a process already done, in which it copies what is on the page and sends it by post, and after that I want to be able to pick up the content that I had stored in the variable and put it back on the clipboard (so the extension does not influence this, as it may have problem if the user loses what was copied to the clipboard

    
asked by anonymous 10.04.2015 / 03:40

1 answer

8

By pressing CONTROL + C in the browser, you will copy whatever is selected to the clipboard. Just before this copy occurs, the copy event is triggered, which you can intercept. Inside, check the value of the selection, which is what is about to be copied:

document.oncopy = function(e) {
    alert('prestes a copiar: ' + window.getSelection().toString());
}
<p>selecione algo aqui e copie</p>
    
10.04.2015 / 04:57