简体   繁体   English

如何收听剪贴板事件?

[英]How to listen clipboard event?

How to listen events, when user tries to copy with mouse or with Ctrl + C from browser url (locationbar). 当用户尝试使用鼠标或使用Ctrl + C从浏览器url(位置栏)进行复制时,如何监听事件。 It is should work only through javascript. 它应该只能通过javascript工作。 Sorry, but i haven't any ideas to it and i can't show any code. 抱歉,但是我对此没有任何想法,我无法显示任何代码。 The main idea it is replace value from clipboard. 其主要思想是替换剪贴板中的值。

A very quick Google ... 一个非常快速的谷歌 ...

Note: This will only work within the confines of the DOM. 注意:这仅在DOM范围内有效。 Not the address bar. 不是地址栏。

document.addEventListener('copy', function(e){
    e.clipboardData.setData('text/plain', 'Hello, world!');
    e.clipboardData.setData('text/html', '<b>Hello, world!</b>');
    e.preventDefault(); // We want our data, not data from any selection, to be written to the clipboard
});

Reference: Here 参考: 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM