Javascript 捕获userscript中的keydown事件并将其隐藏在网站中
我需要在userscript中捕获一个keydown事件,并对网站本身隐藏该事件。即,网站应无法检测按键 我在Greasemonkey用户脚本中尝试了以下代码:Javascript 捕获userscript中的keydown事件并将其隐藏在网站中,javascript,greasemonkey,keyboard-events,userscripts,Javascript,Greasemonkey,Keyboard Events,Userscripts,我需要在userscript中捕获一个keydown事件,并对网站本身隐藏该事件。即,网站应无法检测按键 我在Greasemonkey用户脚本中尝试了以下代码: // ==UserScript== // @name test // @namespace www.robertnitsch.de // @description test // @include https://www.robertnitsch.de/test/ // @version 1 // @g
// ==UserScript==
// @name test
// @namespace www.robertnitsch.de
// @description test
// @include https://www.robertnitsch.de/test/
// @version 1
// @grant none
// ==/UserScript==
document.addEventListener("keydown", function(evt) {
console.log("userscript", evt);
evt.stopPropagation();
return false;
}, true);
但是,页面仍然可以检测到按键事件,这可以在我的测试网站上验证:
我正在使用a、s、d等简单键进行测试。
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('*').off('keyup keydown keypress');
});
</script>
</head>
<body>
test
</body>
</html>
$(文档).ready(函数(){
$('*').off('keyup keydown keypress');
});
测试
以上代码有效
检查并确认 是的,可以使用jQuery。对不起,这不是我想要的。我无法更改实际网站的源代码。太棒了,它可以工作!我不知道在上运行
@和stopImmediatePropagation()
:-)
// ==UserScript==
// @name test
// @namespace http://stackoverflow.com
// @version 1.0
// @description http://stackoverflow.com/questions/25631170/capture-keydown-event-in-userscript-and-hide-it-from-the-website
// @include https://www.robertnitsch.de/test/
// @author zanetu
// @grant none
// @run-at document-start
// ==/UserScript==
document.addEventListener('keydown', function(event) {
event.stopImmediatePropagation();
}, true);