如何在javascript中手动触发`paste`事件? [英] How to trigger `paste` event manually in javascript?

查看:580
本文介绍了如何在javascript中手动触发`paste`事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个侦听 ctrl -v 粘贴事件的事件监听器。在侦听器内部,我使用 clipboardData.getData 方法从剪贴板中获取复制的数据。但是现在我需要在用户点击该按钮时执行一个按钮来执行复制逻辑。我的问题是如何以编程方式触发粘贴事件。我需要得到的主要是 clipboardData 实例。

I have an event listener which listens on ctrl - v paste event. Inside the listener I use clipboardData.getData method to get the copied data from clipboard. But now I need to implement a button when users click on that button it does the copy logic. My question is how to trigger a paste event programmatically. The main thing I need to get is the clipboardData instance.

下面的代码是我当前的粘贴事件监听器。我需要模拟 e 粘贴事件才能使按钮正常工作。

Below code is my current paste event listener. I need to mock the e paste event in order to make the button work.

myDom.on('paste',function(e) {
    e.preventDefault();
    var data = (e.originalEvent || e).clipboardData.getData('application/image');

});


推荐答案


我的问题是如何以编程方式触发粘贴事件。

My question is how to trigger a paste event programmatically.

如果浏览器没有允许的特殊设置,你就不能这样做(我不知道有哪一个允许它,并且可以在Firefox或Chrome的设置中立即找到它启用该设置的用户。如果可以,这将是一个重大的安全问题,因为您的网页可以窥探用户剪贴板的内容。这就是为什么你只能从事件对象中获取剪贴板事件的剪贴板数据。

You can't, without the browser having a special setting allowing it (I'm not aware of one that does, and can't immediately find it in Firefox or Chrome's settings) and the user enabling that setting. If you could, it would be a significant security problem, because your web page could snoop on the contents of the user's clipboard. That's why you can only get clipboard data from an event object for a clipboard event.

来自规范


11.1。隐私和剪贴板事件API

剪贴板事件API允许在剪贴板事件处理程序的上下文中运行的脚本访问系统剪贴板的副本,并且可能修改正在写入剪贴板的数据。

The Clipboard Event API allows scripts running in the context of a clipboard event handler to access a copy of the system clipboard and potentially modify the data being written to the clipboard.

用户代理应该了解保护剪贴板事件API访问的数据的以下要求:

User agents should be aware of the following requirements with regards to securing the data accessed by the Clipboard Event API:


  • 实现 DataTransfer 接口以返回剪贴板数据的对象不得在提供数据的ClipboardEvent事件处理程序之外可用。

  • Objects implementing the DataTransfer interface to return clipboard data must not be available outside the ClipboardEvent event handler that the data is provided to.

如果脚本存储对实现 DataTransfer 从ClipboardEvent事件处理程序外部使用的接口,在预期的上下文之外调用时,所有方法必须为no-ops。

If a script stores a reference to an object implementing the DataTransfer interface to use from outside the ClipboardEvent event handler, all methods must be no-ops when called outside the expected context.

实现不得让脚本甚至创建合成剪贴板ts可以访问真实的剪贴板数据(除非用户已将其配置为执行此操作)。

Implementations must not let scripts create synthetic clipboard events to get access to real clipboard data (unless the user has configured it to do so).

即使剪贴板事件API未被剪贴板权限覆盖,用户代理也可以选择为用户提供禁用此API的方法,或者配置允许哪些站点访问它。

Even though the Clipboard Event API is not covered by the Clipboard permission, user agents may choose to provide a method for the user to disable this API or to configure which sites are allowed to access it.

(我强调第三个要点)

请注意规范确实如此说除非用户已将其配置为,但再次,我不知道有一个允许用户这样做的浏览器。 (只是网站是否可以看到剪贴板事件。)

Note that the spec does say "unless the user has configured it to do so" but again, I'm not aware of a browser that lets the user do that. (Just whether web sites can see clipboard events at all.)

这篇关于如何在javascript中手动触发`paste`事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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