Javascript 如何在跨浏览器中读取剪贴板数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5551338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:45:43  来源:igfitidea点击:

How to read clipboard data in cross browser?

javascriptiframecross-browserclipboardpaste

提问by Jagadesh

Possible Duplicates:
Copy / Put text on the clipboard with FireFox, Safari and Chrome
How to Copy to Clipboard in JavaScript?

可能的重复项:
使用 FireFox、Safari 和 Chrome 将文本复制/放入剪贴板
如何在 JavaScript 中复制到剪贴板?

Hi ,

你好 ,

scenario: I copied some content (either from notepad or word) and want to paste it in my iframe. On before pasting i want to manipulate the clipboard content. In IE i can do it with window.clipboardData.getData("Text");How to read the clipboard data in other browsers (FF/chrome and safari)

场景:我复制了一些内容(从记事本或 word 中)并想将其粘贴到 iframe 中。在粘贴之前,我想操作剪贴板内容。在 IE 中,我可以使用window.clipboardData.getData("Text");How to read the clipboard data in other browsers (FF/chrome and safari) 来完成

回答by Tim Down

You'll only be able to do this in most browsers when the user explicitly triggers a paste (for example, by using Ctrl-Vor the edit or context menus).

当用户明确触发粘贴(例如,通过使用Ctrl-V或编辑或上下文菜单)时,您只能在大多数浏览器中执行此操作。

In Firefox and Opera you'll need to use a hack, such as the one I outlined here: JavaScript get clipboard data on paste event (Cross browser).

在 Firefox 和 Opera 中,您需要使用一种 hack,例如我在此处概述的一种:JavaScript get clipboard data on paste event (Cross browser)

In Internet Explorer, Safari and Chrome, you can access the clipboard directly during a paste using window.clipboardDatain IE and the pasteevent's clipboardDataproperty in WebKit. More information can be found on the Apple developer site.

在 Internet Explorer、Safari 和 Chrome 中,您可以在粘贴过程中直接访问剪贴板,使用window.clipboardDataIE 和WebKit 中的paste事件clipboardData属性。更多信息可以在Apple 开发者网站上找到。

回答by V4Vendetta

Incase of Firefox

火狐浏览器

By default, JavaScript is not allowed to read or set your clipboard data for security and privacy reasons. This is because websites scripts can erase and replace what you currently have in your clipboard (data loss issue) and they can read whatever you have in your clipboard (security and privacy issue)

From Here

默认情况下,出于安全和隐私原因,不允许 JavaScript 读取或设置您的剪贴板数据。这是因为网站脚本可以擦除和替换您当前在剪贴板中的内容(数据丢失问题),并且它们可以读取您在剪贴板中的任何内容(安全和隐私问题)

从这里