javascript剪切/复制/粘贴到剪贴板:Google是如何解决它? [英] javascript cut/copy/paste to clipboard: how did Google solve it?

查看:296
本文介绍了javascript剪切/复制/粘贴到剪贴板:Google是如何解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,这个问题一再被问:如何使用javascript从系统剪贴板复制和粘贴?我发现只有部分解决方案和黑客到目前为止。
过去被问过的原因是,仍然没有可行的解决方案。但是,我发现Google文档实际上对于键盘事件和按钮都有一个有效的解决方案。所以,这是可能的,但他们怎么做呢?软件沙拉文章, 访问系统剪贴板与JavaScript - 圣杯? ,给出了一个很好的概述的问题(但它是几岁)。






  • 您可以使用键盘事件ctrl + x,ctrl + c,ctrl + v从隐藏文本区域复制文本或者在隐藏字段中抓取粘贴的文本,然后对其执行某些操作。


  • 您可以通过Flash或Java Applet复制


  • 您可以使用clipboardData.setData用于IE和execCommand用于其他浏览器的真实解决方案




任何想法Google如何处理剪贴板问题?

解决方案

[注意:这个答案在写入时是正确的,并正确回答了OP的问题。然而,技术已经演变了;如果您有兴趣在您的网络应用程序中支持复制和粘贴,请参阅此页面上的其他更新的答案。 — ruakh]







但我看到Google文件对于键盘事件和按钮。


不,它不。不是真的。对于键盘事件,Google文档不会执行任何操作;它不会阻止浏览器的默认复制和粘贴功能;因此,用户可以自由复制和粘贴,而无需使用Google文档。对于按钮,Google文档不支持系统剪贴板,但它自己的网络剪贴板完全在Google文档中。您无法使用工具栏按钮复制文本,以便粘贴到计算机上的另一个程序,或粘贴从计算机上其他程序复制的文本。



有关详情,请参阅在Google中复制和粘贴文档。 (这是以用户为导向的,而不是面向开发人员,但是它能做一个体面的工作,明确是什么和不支持。)


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).

In short:

  • 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

  • you can use some hack via Flash or maybe a Java Applet to copy something to the system clipboard without need for user approval.

  • you can use a "real" solution with clipboardData.setData for IE and execCommand for other browsers, which depends on approval of the user.

Any idea how Google has tackled the clipboard problem?

解决方案

[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]


However, I saw that Google Docs actually has a working solution now for both keyboard events as well as buttons.

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.

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.)

这篇关于javascript剪切/复制/粘贴到剪贴板:Google是如何解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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