HTML5剪贴板API状态 - CanIUse显示部分支持 [英] HTML5 Clipboard API status - CanIUse Shows partial support

查看:181
本文介绍了HTML5剪贴板API状态 - CanIUse显示部分支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CanIUse.com显示所有主流浏览器至少部分支持Clipboard API,而FF则完全支持: https ://caniuse.com/#feat=clipboard



但是,我找不到任何有关如何使用HTML5写入剪贴板的教程或简单示例(没有闪光)。



有没有人知道究竟部分支持意味着什么,这个功能是否可用?如果只能在Chrome / FF中使用,那将足以满足我的需求。

解决方案

我最近对W3C做了大量研究以下浏览器(最新版本)支持剪贴板API(仍在撰写工作草案阶段)支持:


  1. Internet Explorer

  2. Edge

  3. Google Chrome

  4. Firefox

Chrome Firefox 都支持W3C Clipboard API。您可以使用 getData setData 方法读取和写入数据到剪贴板: https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/ clipboardData



您可以设置不同MIME类型的数据(甚至是自定义类型)。虽然与 text / plain text / html 不同,但在其他浏览器中无法读取,只有你自己设置数据。



Edge 支持W3C剪贴板API,但限制条件是您只能编写MIME类型<$ c $的数据c> text / plain (或 text )。其他任何事情都会引发异常。但是有一个hack使用一个contenteditable div,你可以使用 text / html MIME类型来编写数据。



可以在这里找到黑客攻击: https://stackoverflow.com/a/30905277/434742 (查找更新:复制保留文本格式在答案中



Internet Explorer 不支持W3C剪贴板API。虽然有自定义实施。但即使这与Edge有相同的限制,也意味着您只能使用MIME类型 text / plain (或 text )。但是上面的Edge的hack也适用于Internet Explorer。此外,您还只能读取MIME类型 text / plain (或 text )的数据。但是还有另一个使用contenteditable div的hack,你也可以用 text / html MIME类型读取数据。



<可以在这里找到黑客攻击: https://stackoverflow.com/a/6804718/434742 (查找解答中的解决方案#2



TL; DR:您可以在上述所有浏览器中复制和粘贴包含格式(HTML)的文本。 Edge,特别是Internet Explorer需要一些解决方法。


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.

解决方案

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.

这篇关于HTML5剪贴板API状态 - CanIUse显示部分支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆