简体   繁体   English

如何在 Chrome 或 Safari 中的缩小的 JS 函数上设置断点?

[英]How to set a breakpoint on a minified JS function in Chrome or Safari?

I'd like to set a breakpoint in a "Cart.add" function in the Chrome or Safari JavaScript debuggers.我想在 Chrome 或 Safari JavaScript 调试器的“Cart.add”函数中设置断点。 Problem is, this function is defined in a large minified JS file, and doesn't exist on a line by itself.问题是,这个函数是在一个大的缩小的 JS 文件中定义的,它本身并不存在于一行中。

Some documentation says that the WebKit-based debuggers support "break" or "debug" commands in the debug console, but those don't seem to work in newer versions of the debugger.一些文档说基于 WebKit 的调试器支持调试控制台中的“break”或“debug”命令,但这些在较新版本的调试器中似乎不起作用。

Setting a breakpoint on that line of the JS file doesn't work either, since there are lots of functions on that line.在 JS 文件的那一设置断点也不起作用,因为该行有很多函数。

Any suggestions?有什么建议么?

In Chrome when you open Scripts tab you can prettify selected file by clicking on { } button ("Pretty print") at the bottom.在 Chrome 中,当您打开“ Scripts选项卡时,您可以通过单击底部的{ }按钮(“漂亮打印”)来美化所选文件。 After that you can find your line and set a breakpoint.之后,您可以找到您的行并设置断点。 The code will remain prettified with breakpoints in place after a page refresh.页面刷新后,代码将保持美化,并带有断点。

The debugger statement is probably what you're looking for. debugger语句可能就是您要查找的内容。

Evaluating the DebuggerStatement production may allow an implementation to cause a breakpoint when run under a debugger.评估DebuggerStatement产生式可能允许实现在调试器下运行时导致断点。 If a debugger is not present or active this statement has no observable effect.如果调试器不存在或不活动,则此语句没有可观察到的效果。

The production DebuggerStatement : debugger ;生产DebuggerStatement : debugger ; is evaluated as follows:评估如下:

  1. If an implementation defined debugging facility is available and enabled, then如果实现定义的调试工具可用并启用,则

    a.一种。 Perform an implementation defined debugging action.执行实现定义的调试操作。

    b.Let result be an implementation defined Completion value.让 result 是一个实现定义的 Completion 值。

  2. Else别的

    a.一种。 Let result be (normal, empty, empty).令结果为(正常,空,空)。

  3. Return result.返回结果。

The break statement is for exiting loops and switch statements and has nothing to do with debugging. break语句用于退出循环和switch语句,与调试无关。

The real solution though is to not bugger your code in the first place :)真正的解决方案是首先不要打扰您的代码:)

1) The error message should give you a link to the source code in the Sources tab. 1) 错误消息应为您提供指向 Sources 选项卡中源代码的链接。 Click on that link to get taken to the transpiled code.单击该链接以获取转译后的代码。

2) Click the "{ }" icon at the bottom of the source code in the Sources tab to format the transpiled code for easier debugging. 2) 单击 Sources 选项卡中源代码底部的“{ }”图标,对转译后的代码进行格式化以便于调试。

3)Stick a breakpoint at the line that is failing. 3)在失败的行上贴一个断点。

4) Reproduce the problem again. 4) 再次重现问题。 This time, it should break at the breakpoint before the error occurs.这一次,它应该在错误发生之前的断点处中断。

5) Examine the local variables and call stack to determine what exactly is going wrong. 5) 检查局部变量和调用堆栈以确定究竟出了什么问题。

For chrome users, you'll want to enable automatic pretty print in the experimental features.对于 chrome 用户,您需要在实验功能中启用自动漂亮打印。

启用自动漂亮打印

setting your breakpoint should work now.设置断点现在应该可以工作了。

If you have saved the webpage then beautify your js file using jsbeautifier.org which formats your entire script.如果你已经保存了网页,那么使用 jsbeautifier.org 来美化你的 js 文件,它会格式化你的整个脚本。 Then replace your js content with the beautified version.然后用美化后的版本替换你的js内容。 From here you can debug your JS easily从这里您可以轻松调试您的 JS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM