Capture content to be pasted

Asked

Viewed 794 times

3

I need to capture the content of a text that will be pasted into an input, before the glue happens.

I’ve already captured the collage event:

$(input).on("paste", function (evt) {
    /* evt contém tudo sobre o evento menos o conteúdo */
});

How to get the contents of the clipboard cross-browser?

3 answers

3


The pasted content is in itself value field. So you can do so (worked on Chrome, not tested on other browsers):

var input = $('input');
input.on("paste", function (evt) {
    input.val('huehue ' + input.val());
});

Technically you’re not intercepting the value before, but right after it’s pasted. But if the idea is to change this value, it should be imperceptible to the user. See demo.

2

I found an answer in the international STOF that teaches a gambiarra to do this, apparently, for security reasons, only this way, otherwise the sites could copy the transfer area of all visitors.

https://stackoverflow.com/questions/2176861/javascript-get-clipboard-data-on-paste-event-cross-browser

In short, this method changes the focus of the field to another that is hidden outside the visible area, startar a setTimeout to release the paste event and in this setTimeout event play the value of the hidden field to the visible field. it is now possible to use the value.

Example in Jsfiddle

1

One way would be to delay a little the search for value, I do not know if it is well what you want, but it is as an option:

Demo: Jsfiddle

$('input').on("paste", function (evt) { 
    setTimeout(function () {
        var dados = $('input').val();
        $('input').val('Aguarde...');

        console.log(dados);
    },100);

});

Browser other questions tagged

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