I need to implement task which is quite common feature for RichTextEditors - take HTML from clipboard. Can anyone help with guide on how to solve this task?
It has to be cross platform (IE, FF, Chrome, Opera). I just started from this code:
<script type="text/javascript"> $('.historyText').live('input paste', function(e) { var paste = e.clipboardData && e.clipboardData.getData ? e.clipboardData.getData('text/plain') : // Standard window.clipboardData && window.clipboardData.getData ? window.clipboardData.getData('Text') : // MS false; alert(paste); });</script>
Both window.clipboardData and e.clipboardData are null (Chrome, Firefox).
Update: User wants to paste article content from other browser windows, and I need to get html tags.
Accessing the OS clipboard using browser JavaScript has been possible for several years using document. execCommand() .
We can also use the Clipboard API's write() method to copy arbitrary data such as rich text and images, and this function accepts only one parameter, which is an array containing the data to be written to the clipboard. The writeText() method is specialized for plain text, while write() can write any arbitrary data.
To get to your clipboard history at any time, press Windows logo key + V. From the clipboard history, you can paste and pin frequently used items by choosing an individual item from your clipboard menu.
clipboardData property holds a DataTransfer object, which can be used: to specify what data should be put into the clipboard from the cut and copy event handlers, typically with a setData(format, data) call; to obtain the data to be pasted from the paste event handler, typically with a getData(format) call.
I actually have done a lot of work on this, and just wrote a nice blog post describing how we did it in detail at Lucidchart (as a disclaimer, I work at Lucidchart). We have a JSFiddle that shows copying and pasting (tested in Firefox, Safari, Chrome, and IE9+).
The short of the answer is that you will need to get the HTML during the system paste event. In most (non-IE) browsers, this can be done with something as simple as the following:
document.addEventListener('paste', function(e) { var html = e.clipboardData.getData('text/html'); // Whatever you want to do with the html }
The problem is when you want to get HTML in IE. For whatever reason, IE doesn't make the text/html clipboard data accessible via javascript. What you have to do is let the browser paste to a contenteditable div and then get the html after the paste event is over.
<div id="ie-clipboard-contenteditable" class="hidden" contenteditable="true"></div>
var ieClipboardDiv = $('#ie-clipboard-contenteditable'); var focusIeClipboardDiv = function() { ieClipboardDiv.focus(); var range = document.createRange(); range.selectNodeContents((ieClipboardDiv.get(0))); var selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range); }; document.addEventListener('beforepaste', function() { if (hiddenInput.is(':focus')) { focusIeClipboardDiv(); } }, true); document.addEventListener('paste', function(e) { ieClipboardDiv.empty(); setTimeout(function() { var html = ieClipboardDiv.html(); // Do whatever you want with the html ieClipboardDiv.empty(); // Return focus here }, 0); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With