gpt4 book ai didi

javascript - 如何在 javascript 中手动触发 `paste` 事件?

转载 作者:太空狗 更新时间:2023-10-29 15:53:08 27 4
gpt4 key购买 nike

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

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

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 的设置中找到它)用户启用该设置。如果可以,那将是一个严重的安全问题,因为您的网页可以窥探用户剪贴板的内容。这就是为什么您只能从剪贴板事件的事件对象中获取剪贴板数据的原因。

来自 the specification :

11.1. Privacy and the Clipboard Event 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.

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

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

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

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

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.

(我强调第三个要点)

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

关于javascript - 如何在 javascript 中手动触发 `paste` 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51395393/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com