Skip to content

Instantly share code, notes, and snippets.

@fouber
Created June 17, 2014 17:07
Show Gist options
  • Save fouber/1a68376760209db3f3aa to your computer and use it in GitHub Desktop.
Save fouber/1a68376760209db3f3aa to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>hello world</title>
<script type="text/javascript">
//这里是防御用js,写在页面的最开始
//在这个js之后执行的任何addEventListener方法都将被监控
(function(){
var raw_fn = Element.prototype.addEventListener;
var handles = [];
Object.defineProperty(Element.prototype, 'addEventListener', {
value: function(){
//监控到事件注册,上报日志
console.log('捕获到注册的回调函数:%s', arguments[1]);
//不动声色,继续执行代码
return raw_fn.apply(this, arguments);
},
//不允许复写,这样注入的代码也不能修改我们带监控的addEventListener方法
writable: false,
configurable: false,
enumerable: true
});
})();
</script>
</head>
<body>
<h1>hello world</h1>
<script type="text/javascript">
//假设这里是注入的js
//这里用匿名函数注册了一个事件处理,但是会被监控到。
document.body.addEventListener('click', function(){
alert(1111);
}, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment