更多新鲜技术资讯,欢迎关注公众号: Linux开源先锋
了解更多开源技术,请关注以上公众号
按照以下步骤通过命令使用浏览器内置的 JavaScript 调试器:
-
启动浏览器,并导航到要调试的页面。
-
在命令行中,输入浏览器名称和调试器的启动选项,如下所示:
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
这表示调试器已经启动,并且浏览器已经监听了指定的端口号。
- 打开另一个命令行窗口,然后使用以下命令调用
Node.js
的Chrome DevTools
协议接口:
node inspect http://localhost:9222/json
其中,“9222”是在第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开源先锋