Skip to content

Instantly share code, notes, and snippets.

@endtwist
Created January 18, 2012 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endtwist/1630862 to your computer and use it in GitHub Desktop.
Save endtwist/1630862 to your computer and use it in GitHub Desktop.
Paste event in Firefox
<textarea id="pasteField"></textarea>
var $pasteField = $( '#pasteField' );
function onPaste( evt, retries ) {
// No clipboardData object, so we fake it and just return the field's content, when we receive it.
var text = $pasteField.val();
$pasteField.val( '' ); // reset the field
clipboardObj = { getData: function() { return text; } };
items = [ { kind: 'string', type: 'text' } ];
// If we didn't find any content on this attempt, and we haven't tried > 3 times,
// try again in 100 ms to find content in the textarea.
if( !text.length && ( retries < 3 || typeof retries === 'undefined' ) ) {
if( typeof retries === 'undefined' )
retries = 0;
setTimeout( function() {
onPaste( evt, ++retries );
}, 100 );
return true;
}
// Now you finally have the item's content, with an API similar to that of Chrome -- at least, for plain text.
}
$pasteField.bind( 'paste', onPaste );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment