wc-clipboard.js 858 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /* exported wcSetClipboard, wcClearClipboard */
  2. /**
  3. * Simple text copy functions using native browser clipboard capabilities.
  4. * @since 3.2.0
  5. */
  6. /**
  7. * Set the user's clipboard contents.
  8. *
  9. * @param string data: Text to copy to clipboard.
  10. * @param object $el: jQuery element to trigger copy events on. (Default: document)
  11. */
  12. function wcSetClipboard( data, $el ) {
  13. if ( 'undefined' === typeof $el ) {
  14. $el = jQuery( document );
  15. }
  16. var $temp_input = jQuery( '<textarea style="opacity:0">' );
  17. jQuery( 'body' ).append( $temp_input );
  18. $temp_input.val( data ).select();
  19. $el.trigger( 'beforecopy' );
  20. try {
  21. document.execCommand( 'copy' );
  22. $el.trigger( 'aftercopy' );
  23. } catch ( err ) {
  24. $el.trigger( 'aftercopyfailure' );
  25. }
  26. $temp_input.remove();
  27. }
  28. /**
  29. * Clear the user's clipboard.
  30. */
  31. function wcClearClipboard() {
  32. wcSetClipboard( '' );
  33. }