How do I take what’s copied on the clipboard and put it into a variable?

Asked

Viewed 2,150 times

3

It would look like this: when copying anything using CONTROL+C, I need to put this content in a variable using Javascript. I didn’t find on the internet how to do this. Anyone help? Detail: It is by extension. It would be for me to record what’s in the clipboard into a variable so I can perform a process already done, in which it copies what’s on the page and sends by post, and after that I want to be able to take the content that I had saved in the variable and put back in the clipboard (so the extension does not influence this, because it may have problem if the user loses what was copied in the clipboard

  • Do you want to do this in response to a user’s "paste" action, or just do it? I’m not sure, but I think the browers would not allow this, for security reasons (imagine if any site opened on browser could read what’s in the clipboard and send it all to any server, how much private information could leak). Fountain

  • I think I misinterpreted the question, the copying command occurred on itself browser right? I thought you copied it from somewhere and wanted to "auto-paste" the browser...

  • Hi. This is for an extension. When copying anything anywhere, I wanted to store it in a variable before running the extension script. That would be to not lose what is copied by the user, because the extension works by copying and sent by post and in case what was copied before would be lost. I hope to understand now, otherwise I update the question

  • It is good to mention this kind of thing in the question, yes, since it influences the answers (in a normal page, as I said, it cannot for security reasons; in an extension, probably can, but I have no knowledge to say for certain).

  • All right! I’m on my cell going to class now but half day I update the question

  • ready! I edited the question

Show 1 more comment

1 answer

8

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

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

  • 1

    The guy has asked this "10 times": http://answall.com/q/50083/3635

  • but I haven’t been able to do such a "backup" yet, and that’s not there in that question

  • I tried to do this but it gives problem, it no longer executes the command to select the contents of the page after 'Paste'

  • @Lucianozancan Mount a example where we can see the problem happening, and include it in the question. But by now you should know that browsers are purposefully limited in relation to the clipboard, for security reasons. Certain things cannot be done. A backup of anything that is there (for example, a set of Excel cells), does not give.

  • yes, I know! That’s why I’m having such a hard time doing this.

  • 1

    @Lucianozancan after you edited the question she changed almost totally of context, making what I said seem that I was confused or did not pay attention, this for me is a chameleon question, but ok we will the question... You want to save the Clipboard in a variable is that it? As I said in the other question while copying the Clipboard you will copy more than text, will copy the formatting too, you really want the Clipboard or you want to force copy something from the page?

  • Guilherme is right, the question has changed so much that it invalidated my answer and the marking as duplicate. Reverti to the previous state. @Lucianozancan If the end goal is the same (send the content for analysis on another site), clarify your doubts in the other question. The answers there didn’t help you?

  • Hi Uilherme and bfavaretto, I’m sorry, I really get confused. I’m new, anyway. Guilherme, I wanted to be able to save what is in the Clipboard before executing that extension script and after the script returns the saved content to the Clipboard only for the purpose of not losing what was in the Clipboard before the extension was executed. Is it possible to do this?

  • It would be an addition to another question, only not to lose what was copied, because the user could have something important in the Clipboard and when executing the extension that would be lost

  • @Lucianozancan Is that I think you should leave the clipboard there, and find another way to send the content to the remote site.

  • hmm. Do you have any idea how to do that?

  • I edited the answer to the other question, at the end there is an example using textContent, note that it will only copy the texts, the formatting will not go together, but it seems to me that this is exactly what you want.

  • I think that’s right @Guilhermenascimento, your answer already contained all the necessary not to need the Clipboard. Indeed, both this question and that suffer from the XY problem.

  • @bfavaretto I think I fell into this trap, the problem is that I think I fall down to try to prevent the AP duplicate the question (it had already duplicated a couple of times before this one), the problem is that it could not (get) transmit the situation of use and or problems arising from this use :(

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.