javascript 剪切/复制/粘贴到剪贴板:Google 是如何解决的?
是的,这个问题已经被问了一遍又一遍:如何使用 javascript 在系统剪贴板中复制和粘贴?到目前为止,我只发现了部分解决方案和技巧.过去经常被问到的原因是仍然没有有效的解决方案.但是,我看到 Google Docs 现在实际上为键盘事件和按钮提供了一个可行的解决方案.所以,这是可能的,但他们是怎么做到的呢?软件沙拉文章,访问系统带有 JavaScript 的剪贴板——一个圣杯?,很好地概述了这个问题(但它已经有几年的历史了).
Yes, this question has been asked again and again: how to copy and paste from and to the system clipboard with javascript? I have found only partial solutions and hacks so far. The reason that it has been asked so often in the past is that there still is no working solution. However, I saw that Google Docs actually has a working solution now for both keyboard events as well as buttons. So, it is possible, but how do they do it? Software Salad article, Accessing the System Clipboard with JavaScript – A Holy Grail?, gives a nice overview of the problem (but it's a few years old).
简而言之:
您可以使用键盘事件 ctrl+x、ctrl+c、ctrl+v 从隐藏的文本区域复制带有准备好的数据的文本,或者在隐藏字段中捕获粘贴的文本,然后对其进行操作
you can use keyboard events ctrl+x, ctrl+c, ctrl+v to either copy text from a hidden textarea with prepared data, or catch pasted text in a hidden field and then do something with it
您可以通过 Flash 或 Java Applet 使用一些技巧将内容复制到系统剪贴板,而无需用户批准.
you can use some hack via Flash or maybe a Java Applet to copy something to the system clipboard without need for user approval.
您可以在 IE 上使用 clipboardData.setData 和在其他浏览器上使用 execCommand 的真实"解决方案,这取决于用户的批准.
you can use a "real" solution with clipboardData.setData for IE and execCommand for other browsers, which depends on approval of the user.
知道 Google 是如何解决剪贴板问题的吗?
Any idea how Google has tackled the clipboard problem?
推荐答案
[注意: 此答案在编写时是准确的,并且正确回答了 OP 的问题.但是,从那时起,技术已经发展.如果您有兴趣在您的 Web 应用程序中支持复制和粘贴,请参阅此页面上其他更新的答案.—ruak]
[Note: This answer was accurate at the time it was written, and correctly answered the OP's question. However, technology has evolved since then; if you are interested in supporting copy-and-paste in your web app, please see the other, more recent answers on this page. —ruakh]
但是,我发现 Google Docs 现在实际上为键盘事件和按钮提供了一个可行的解决方案.
However, I saw that Google Docs actually has a working solution now for both keyboard events as well as buttons.
不,它没有.并不真地.对于键盘事件,Google Docs 不做任何事情;它根本不会阻止浏览器的默认复制和粘贴功能;因此,用户可以自由复制和粘贴,而不会妨碍 Google Docs.对于按钮,Google Docs 不支持 system 剪贴板,但它自己的网络剪贴板"完全在 Google Docs 中.您不能使用工具栏按钮复制文本以粘贴到计算机上的另一个程序中,也不能粘贴从计算机上的另一个程序复制的文本.
No, it doesn't. Not really. For keyboard events, Google Docs doesn't do anything; it simply doesn't block the browser's default copy-and-paste feature; so, users can copy and paste freely without Google Docs getting in the way. For buttons, Google Docs doesn't support the system clipboard, but its own "web clipboard" that is entirely within Google Docs. You can't use the toolbar buttons to copy text for pasting into another program on your computer, or to paste text that has been copied from another program on your computer.
有关这方面的详细信息,请参阅复制和粘贴到 Google 文档中".(这是面向用户而不是面向开发人员的,但它在明确支持和不支持什么方面做得不错.)
For more information about this, see "Copying and pasting in Google Docs". (That's user-oriented rather than developer-oriented, but it does a decent job making clear what is and is not supported.)
相关文章