HTML5 Clipboard API status - CanIUse Shows partial support

狂风中的少年 提交于 2019-12-07 13:40:06

问题


CanIUse.com shows that all major browsers have at least partial Clipboard API support while FF has full support: https://caniuse.com/#feat=clipboard

However, I cannot find any tutorials or simple examples of how to write to the clipboard using HTML5 (no flash).

Does anyone know what exactly partial support means, is this feature usable? If it only worked in Chrome/FF that would be sufficient for my needs.


回答1:


I recently did an intense research on the W3C Clipboard API (which is up to this writing still in the working draft stage) support in the following browsers (latest versions):

  1. Internet Explorer
  2. Edge
  3. Google Chrome
  4. Firefox

Chrome and Firefox both support the W3C Clipboard API. You can read and write data to the clipboard using the getData and setData methods: https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData

You can set data in different MIME types (even custom ones). Although anything different from text/plain and text/html won't be readable in other browsers, only the one you set the data in.

Edge supports the W3C Clipboard API with the restriction that you can only write data with the MIME type text/plain (or text). Anything else is going to throw an exception. But there is a hack using a contenteditable div with which you can write data with the text/html MIME type too.

The hack can be found here: https://stackoverflow.com/a/30905277/434742 (look for "UPDATE: COPY KEEPING THE TEXT FORMAT" in the answer)

Internet Explorer does not support the W3C Clipboard API. There is a custom implementation available though. But even that has the same restriction as Edge meaning you can only write data with the MIME type text/plain (or text). But the hack for Edge above also works for Internet Explorer. Moreover you can also only read data with the MIME type text/plain (or text). But there is another hack using a contenteditable div with which you can read data with the text/html MIME type too.

The hack can be found here: https://stackoverflow.com/a/6804718/434742 (look for "Solution #2" in the answer)

TL;DR: You can copy and paste text including formatting (HTML) in all browsers mentioned above. Some workarounds are needed for Edge and especially Internet Explorer though.



来源:https://stackoverflow.com/questions/24293864/html5-clipboard-api-status-caniuse-shows-partial-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!