命令行使用浏览器内置的 JS 调试器

通过命令行启用浏览器的远程调试端口,如GoogleChrome的--remote-debugging-port,然后利用Node.js的ChromeDevTools协议接口进行调试。在调试器中可以设置断点、检查变量、执行JavaScript命令等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

更多新鲜技术资讯,欢迎关注公众号: Linux开源先锋

了解更多开源技术,请关注以上公众号

按照以下步骤通过命令使用浏览器内置的 JavaScript 调试器:

  1. 启动浏览器,并导航到要调试的页面。

  2. 在命令行中,输入浏览器名称和调试器的启动选项,如下所示:

google-chrome --remote-debugging-port=9222

其中,“google-chrome”是浏览器名称,后面的选项“–remote-debugging-port=9222”将启用调试器并指定一个端口号。

执行命令后,应该看到类似于以下内容的输出:

DevTools listening on ws://127.0.0.1:9222/devtools/browser/c3d37839-	c0b3-4115-8351-5805e0848a2b

​ 这表示调试器已经启动,并且浏览器已经监听了指定的端口号。

  1. 打开另一个命令行窗口,然后使用以下命令调用 Node.jsChrome DevTools 协议接口:
node inspect http://localhost:9222/json

​ 其中,“9222”是在第2步中指定的端口号。

  1. 执行上述命令后,将看到一个带有调试工具的命令行界面,此时可以使用常规的调试工具,例如设置断点,检查变量、堆栈跟踪等等。

  2. 在调试过程中,可以使用控制台来执行 JavaScript 命令、查看日志消息等。

    $ node inspect http://localhost:9222/json
    < Debugger listening on ws://127.0.0.1:9229/bec93910-06f6-464a-ad2c-2197dbb793d0
    < For help, see: https://nodejs.org/en/docs/inspector
    < 
    connecting to 127.0.0.1:9229 ... ok
    < Debugger attached.
    < 
    < Waiting for the debugger to disconnect...
    < 
    debug> help
    run, restart, r       Run the application or reconnect
    kill                  Kill a running application or disconnect
    
    cont, c               Resume execution
    next, n               Continue to next line in current file
    step, s               Step into, potentially entering a function
    out, o                Step out, leaving the current function
    backtrace, bt         Print the current backtrace
    list                  Print the source around the current line where execution
                          is currently paused
    
    setBreakpoint, sb     Set a breakpoint
    clearBreakpoint, cb   Clear a breakpoint
    breakpoints           List all known breakpoints
    breakOnException      Pause execution whenever an exception is thrown
    breakOnUncaught       Pause execution whenever an exception isn't caught
    breakOnNone           Don't pause on exceptions (this is the default)
    
    watch(expr)           Start watching the given expression
    unwatch(expr)         Stop watching an expression
    watchers              Print all watched expressions and their current values
    
    exec(expr), p(expr), exec expr, p expr
                          Evaluate the expression and print the value
    repl                  Enter a debug repl that works like exec
    
    

    更多新鲜技术资讯,欢迎关注公众号: Linux开源先锋

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值