1

Example

I have read that copying plain text from clipboardData to a content editable element isn't supported in Chrome on Android (like this post). When you run the example above on mobile, which aims to remove all formatting from pasted text, the text variable returns an empty string. This only happens in Chrome on Android. So I'd like to know if there is a workaround for that.

Example code modified from JavaScript get clipboard data on paste event (Cross browser)

$('#editableDiv').on('paste',function(e) { 
      e.preventDefault();
      var text;
      if( e.originalEvent.clipboardData ){
        text = (e.originalEvent || e).clipboardData.getData('text/plain');

        if(text === ''){

           console.log('Android Problem');  

        }

        document.execCommand('insertText', false, text);
      }
      else if( window.clipboardData ){
        text = window.clipboardData.getData('Text');
      if (window.getSelection)
        window.getSelection().getRangeAt(0).insertNode( document.createTextNode(text) );
      }
      console.log(text);
});

Updated: I have come up with a workaround by triggering a popup for users to paste text. Not ideal but could get the job done removing formatting. I'm looking for more workarounds/suggestions.

   if( e.originalEvent.clipboardData ){
    text = (e.originalEvent || e).clipboardData.getData('text/plain');
    if(text === ''){       
        text = prompt('Paste something..');  
    }
    document.execCommand('insertText', false, text);
  }
Community
  • 1
  • 1
RedGiant
  • 4,036
  • 6
  • 44
  • 117

0 Answers0