使用Chrome,Tampermonkey和jQuery - 我想在第三方网站上发现并删除关键事件绑定。
我试图绑定(通过它们)并使用以下方法阻止传播:
,_cancel:function(e){
//console.log('keyProcessed');
e.cancelBubble = true; // IE4+
try {
e.keyCode = 0;
} catch (e) {
} // IE5
if (window.event) {e.returnValue = false; } // IE6
if (e.preventDefault) {e.preventDefault(); } // moz/opera/konqueror
if (e.stopPropagation) {e.stopPropagation(); } // all
return false;
}
......但钥匙仍然闪着火。
我尝试使用jQuery枚举元素:
$('*').each(function(){
if(this.onkeydown){
console.log(this.tagName,this.onkeydown);
}
});
...对keydown
keyup
或keypress
我也试过
$('*').unbind();
如何枚举和终止绑定?
答案 0 :(得分:0)
修改,更新
点击button
之前,input
元素的默认操作是输入文字;点击button
,evt.preventDefault()
调用后,文字无法在元素中输入。
尝试
var events = ["keydown", "keyup", "keypress"];
$.each($("*"), function (index, elem) {
if ($._data(elem, "events") != undefined) {
// check `elem` for `keydown`, `keyup`, `keypress` `events`
if (new RegExp(events.join("|")).test(
Object.keys($._data(elem, "events")))) {
$.each(events, function (key, val) {
// do stuff at `value` having `events`
// call `evt.preventDefault` at `elem`
// having `keydown`, `keyup`, `keypress` `events` `events`
$(elem).on(val, function(evt) {
evt.preventDefault()
})
})
}
};
});
$(function () {
$("input:eq(0)").on("keyup", function (e) {
console.log(e);
});
$("input:eq(1)").on("keydown", function (e) {
console.log(e);
});
$("input:eq(2)").on("keypress", function (e) {
console.log(e);
});
$("button").on("click", function() {
var events = ["keydown", "keyup", "keypress"];
$.each($("*"), function (index, elem) {
if ($._data(elem, "events") != undefined) {
if (new RegExp(events.join("|")).test(
Object.keys($._data(elem, "events")))) {
$.each(events, function (key, val) {
$(elem).on(val, function(evt) {
evt.preventDefault()
})
})
}
};
});
})
})

input:nth-of-type(1) {
width : 50px;
height : 50px;
background : blue;
}
input:nth-of-type(2) {
display : block;
width : 50px;
height : 50px;
background : yellow;
}
input:nth-of-type(3) {
display : block;
width : 50px;
height : 50px;
background : orange;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button>click to remove keydown, keyup, keypress events below</button><br />
<input type="text" />
<input type="text" />
<input type="text" />
&#13;