是否可以在Javascript中从剪贴板onclick粘贴? [英] Is it possible to paste from clipboard onclick in Javascript?

查看:64
本文介绍了是否可以在Javascript中从剪贴板onclick粘贴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该用例用于在私有Intranet上输入数据:用户从Google文档,Dropbox文本文档等中复制和粘贴,并且必须粘贴到包含许多输入的表单中.要为他们加快速度,最好是用户可以从文档中复制和粘贴,并且当他们单击输入字段时,单击鼠标左键时它会自动粘贴.

The use case is for data entry on a private intranet: Users copy and paste from google docs, dropbox text documents etc and have to paste into a form with many inputs. to speed it up for them, it would be nice if the user could copy and paste from the document, and when they click on the input field, it automatically pastes when they left click.

我已经看到了一些较旧的问题,但是看来这(出于充分的理由)存在安全风险.但是,专用Intranet上的所有用户都知道这一点,只是想节省时间.是否有任何浏览器或技术可以做到这一点?

I've seen some older questions, but it appears that this is (and for good reason) a security risk. however, all the users on the private intranet are aware of this and simply want to save time. is there any browser or technique that would allow this?

此外,我们可以使用任何支持它的浏览器或环境.然后如何实现此功能以节省时间?

also, we can use any browser or environment that supports it. How can I then achieve this functionality to save time?

推荐答案

Chrome 66中包含剪贴板API.请在此处检查jsfiddle:

The clipboard API is included in Chrome 66. Check the jsfiddle here: https://jsfiddle.net/zm490d6a/

相关代码为:

async function paste(input) {
  const text = await navigator.clipboard.readText();
  input.value = text;
}

如果您使用的是Chrome 66或更高版本,则可以使用.但是,请注意,出于安全原因,您必须授予网页访问剪贴板的权限,因此,第一次单击该页面上的输入时,它将弹出询问您访问剪贴板的权限.授予访问权限后,只要在输入中单击任何内容,便会粘贴剪贴板中的任何内容.

If you are in Chrome 66 or later this will work. However, note that you must give the webpage permission to access the clipboard for security reasons, so the first time you click on the input on that page it will popup asking your permission to access the clipboard. Once you give it access any clicks into the input will paste whatever is on your clipboard.

在这里,我只使用readText,但是您也可以将readData用于剪贴板上的图像等. https://developer.mozilla.org/en-US/docs/Web/API/剪贴板

Here I simply use readText, but you can also use readData for images etc on the clipboard. https://developer.mozilla.org/en-US/docs/Web/API/Clipboard

这篇关于是否可以在Javascript中从剪贴板onclick粘贴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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