Capture content to be pasted

3

I need to capture the contents 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 do I get the contents of the clipboard cross-browser?

    
asked by anonymous 25.09.2014 / 23:39

3 answers

3

The pasted content is in the value of the field itself. So you can do it like this (it worked in Chrome, did not test in other browsers):

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

Technically you are not intercepting the value before, and soon after it is pasted. But if the idea is to change this value, it should be imperceptible to the user. View demo .

    
26.09.2014 / 02:27
2

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

link

In short, this method makes the focus of the field change to another that is hidden outside the visible area, start a setTimeout to release the paste event, and in this event the setTimeout throws the value of the hidden field to the visible field. at this point you can use the value.

JSFiddle Example

    
26.09.2014 / 01:33
1

One way would be to delay the search for value a bit, I do not know if it's what you want, but it's an option:

Demo: JSFiddle

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

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

});
    
26.09.2014 / 01:38