V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
shanlanlan
V2EX  ›  JavaScript

chrome 的事件监听断点(event listener breakpoints)能监听: alert 吗?

  •  
  •   shanlanlan · 2020-06-24 07:49:06 +08:00 · 2480 次点击
    这是一个创建于 1373 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在页面 debug 调试的时候,经常被 alert 打断,有办法直接定位到这个 alert 吗?

    例如:click 事件可以设置事件断点,那么 alert 方法可以断点吗?


    虚心请教大佬。

    6 条回复    2020-06-24 21:06:11 +08:00
    Mutoo
        1
    Mutoo  
       2020-06-24 08:02:40 +08:00   ❤️ 1
    alert 是全局函数,直接替换掉:

    origAlert = alert;
    alert = (...parmas) => {
    debugger;
    origAlert(...params)
    }

    然后断点后看 Call Stack 即可。
    rioshikelong121
        2
    rioshikelong121  
       2020-06-24 08:12:59 +08:00   ❤️ 2
    控制台 debug(window.alert) 试试
    azcvcza
        3
    azcvcza  
       2020-06-24 09:51:43 +08:00   ❤️ 1
    参照 js 高级程序设计的例子,可以给 Function prototype 绑 before 和 after,然后传入 window.alert,在 before 和 after 里调 debugger 吧
    shanlanlan
        4
    shanlanlan  
    OP
       2020-06-24 17:06:56 +08:00
    @rioshikelong121 #2 原文:“控制台 debug(window.alert) 试试”
    ======
    回复:亲测有效。
    shanlanlan
        5
    shanlanlan  
    OP
       2020-06-24 17:24:01 +08:00
    @rioshikelong121 #2 原文:“控制台 debug(window.alert) 试试”
    ======
    回复:感谢,已经成功解决了问题。定位到代码后,直接把网站的 if 判断改为了:if(true)了,调试通过。
    rioshikelong121
        6
    rioshikelong121  
       2020-06-24 21:06:11 +08:00   ❤️ 1
    @shanlanlan 嗯 没事可以看看 Chrome DevTools 的文档。 还是有很多新奇的功能。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   989 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:08 · PVG 04:08 · LAX 13:08 · JFK 16:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.