获取用户从网页上复制的文本 [英] Obtain the text that a user is copying from a web page
问题描述
使用JavaScript,有可能检测用户何时从网页复制文本,然后将复制的文本存储在数组中(而不会覆盖CTRL + V组合键,并且不会阻止文本被复制) ?
换句话说,是否有可能在不覆盖该组合键的默认行为的情况下检测组合键?
keyup
侦听器并获取事件参数 ctrlKey
和 keyCode
,然后通过 window.getSelection()
获取选定的文本: $(window).keyup(function(e){
if(e.ctrlKey&& e.keyCode === 67){
alert(Text copied was:+ window.getSelection());
}
});
默认行为除非你在监听函数中 return false
(或者在jQuery中, e.preventDefault()
>,它实际上(和代码中)和返回false
相同)。
Using JavaScript, is it possible to detect when a user is copying text from a web page, and then store the copied text in an array (without overriding the CTRL+V key combination, and without preventing the text from being copied)?
In other words, is it possible to detect a key combination without overriding the default behavior of that key combination?
Using the keyup
listener in jQuery and getting the event parameters ctrlKey
and keyCode
, then retrieving the selected text through window.getSelection()
:
$(window).keyup(function (e) {
if (e.ctrlKey && e.keyCode === 67) {
alert("Text copied was: " + window.getSelection());
}
});
The default behaviour of the key strokes are not interfered with unless you return false
in the listening function (or, in jQuery, e.preventDefault()
, which in effect (and in code) does the same thing as return false
).
这篇关于获取用户从网页上复制的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!