禁用页面上的所有JavaScript事件 [英] disable all javascript events on page

查看:593
本文介绍了禁用页面上的所有JavaScript事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个Chrome扩展,禁用所有元素的事件监听器(mouseover,click,...)我不想重写noscript,这只是我需要的设置步骤。

我试过 $(body *)。unbind() .unbind (mouseover click) .off() .off(mouseover click) none none。



我做错了什么?



也可以罚款,只是禁用所有的JavaScript代码(来自页面本身)从运行在页面上,只允许我的扩展注入代码

解决方案

这不是一个完整的例子,因为我不会为你做所有的工作,但可能会带领你朝着正确的方向发展:

  function preventAll(){
var dom = document.getElementsByTagName('*');
var km = ['click','dblclick','mousedown','mousemove' ,'mouseover','mouseout','mouseup','mouseenter','mouseleave','keydown','keypress','keyup'];
for(var i = 0,l = dom.length ;(< l; i ++){
for(var n = 0,c = km.length; n dom [i] ['on'+ km [n]] = function(e){
e = e || event;
e.preventDefault();
返回false;
}
}
}
var fr = frames;
for(var i = 0,l = fr.length; i< l; i ++){
// cancell frames events here;
}
}


I am trying to write a chrome extension that disables event listeners for all elements (mouseover, click, ...) I am not trying to rewrite noscript, this is just a setup step that i need.

I have tried $("body *).unbind() and .unbind("mouseover click") and .off() and .off("mouseover click") none worked.

What am i doing wrong?

PS: it would also be fine to just disable all javascript code (coming from the page itself) from running on the page and only allow my extension injected code

解决方案

This is not a complete example, as I won't do all of your work for you, but might lead you in the right direction:

function preventAll(){
  var dom = document.getElementsByTagName('*');
  var km = ['click', 'dblclick', 'mousedown', 'mousemove', 'mouseover', 'mouseout', 'mouseup', 'mouseenter', 'mouseleave', 'keydown', 'keypress', 'keyup'];
  for(var i=0,l=dom.length; i<l; i++){
    for(var n=0,c=km.length; n<c; n++){
      dom[i]['on'+km[n]] = function(e){
        e = e || event;
        e.preventDefault();
        return false;
      }
    }
  }
  var fr = frames;
  for(var i=0,l=fr.length; i<l; i++){
    // cancell frames events here
  }
}

这篇关于禁用页面上的所有JavaScript事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆