有时候在爬取网站时,遇到无限debugger的情况,一种是constructor中的debugger,还有一种是eval中的debugger。可以通过hook的方式绕过无限debugger
。
-
- // 处理eval中无限debugger
- var eval_ = window.eval;
- window.eval = function (){
- let can = arguments[0]
- if(can.indexof("debugger")){
- return;
- }else{
- eval_.apply(window, arguments) // window.eval
- }
- }
-
- // 处理constructor
- // 1. 记录一下之前的Function.prototype.constructor
- var xxxx = Function.prototype.construct
-
- // 2. 给Function.prototype.constructor 设置一个新的功能
- Function.prototype.construct = function (){
- // 3. 判断参数是否包含debugger
- if(arguments[0] === 'debugger'){
- return // 4. 如果是debugger就不构建函数
- }else{
- // 5. 如果不是debugger,需要放行,继续执行原来的操作
- return xxxx.apply(this, arguments) // 只能用apply
- }
- }
-