How to simulate Ctrl+v

Asked

Viewed 845 times

1

Is there any way to simulate a Ctrl+v using Javascript?

I want to include a button and an input. When clicking the button, whatever is in the clipboard should appear in the input.

The code is for an HTML page that shortens links. It has only two fields: URL and link name.

My idea is that the user copy the URL and when accessing the page, click the button to paste the content in the input.

  • Would it be on a web page or are you developing an extension for browsers? If it is web page, I would like to know the motivation of this need, seems to me something that can be solved otherwise depending on the need.

  • I explained more details in the question

  • It wouldn’t be easier to send the link via GET or POST to the other page?

  • In case it comes from a page of your domino, send to url via request is a good idea.

  • In this case, imagine that you are browsing here in the OS and want to send the link to someone. Then you copy the link, go to the shorten URL site and click on the button to paste it there. That’s the idea. So it can’t be via get

  • 1

    I said via GET if the shortener is an internal tool, if it’s any web site you can easily resolve using the provided Apis, just like twitter automatically does when you paste a link, the moment you post it converts your link to a short link, just follow the same "recipe".

Show 1 more comment

3 answers

6


I’ll reply because I don’t have access to comments. Then I can edit this reply.

Copying the contents of the clipboard is something very well protected by browsers for security reasons. Simulating the copy of memory content can cause serious problems (imagine pasting a credit card number and secretly sending via ajax). So, no, the way it was described is impossible.

However, it may be possible to simulate a "paste" event via code. Perhaps you can think of some workarounds depending on where the information comes from. You can provide more information?

  • 3

    Now you can comment! = D

2

Extensions using Webextension Apis

Extensions created using Webextension Apis can interact with the system’s clipboard using Document.execCommand(). Below is an example that would fit your case:

function paste() {
  var pasteText = document.querySelector("#inputSaida");
  pasteText.focus();
  document.execCommand("Paste");
}
    
document.querySelector("#botaoColar").addEventListener("click", paste);
<input type="text" id="inputSaida"></input>
<button id="botaoColar">Colar</button>

Here is the full API documentation.


Lack of support (or permission)...

Chrome does not support this by default, to work you need to give access/permission clipboardRead to his manifest.js and perform this function in the background, some links that can help:

  • 2

    Just for the record the AP did not state that it is an add-on.

  • Interesting, but the system is very simple, I don’t know if it pays to touch on this question of permissions. I further detailed the question. Thank you anyway.

  • I get it, the way I know and know it works is only with extensions, because then the user would accept you have this kind of access to his browser, otherwise I find it kind of hard to get...

1

I don’t see the need for so much work, even more something that implies the security of the user.

If you are using a link shortener of your own, you could simply resolve with session or localStorage or sessionStorage, can even use an HTTP upload using POST or GET of the generated link, as long as they are all on the same domain or you have "control" over that "link shortener".

If the link shortener is a third-party service (such as googl.Gl or bitly) you could simply use an API, usually link shortening services have Apis to automate the service.

Some examples:

Shortening link with googl

More "customers": https://developers.google.com/api-client-library/

Shortening link with bitly

More "customers": http://dev.bitly.com/code_libraries.html

  • The need is the client’s request, but I’ll tell him it’s not viable

  • It’s an imitation of the googl

  • @Italorodrigo and this imitation has API? If not, is there a reason why you can’t switch to bitly?

  • The site is icut.me from a friend and he wants to include this feature. Take a look.

  • @Italorodrigo because it does not use bitly until your friend provides an integration system (by REST or other type of webservice)?

Browser other questions tagged

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