调试 Firefox 扩展时如何检测语法错误
在开发大型 Firefox 插件时,我有时会不小心犯一个语法错误,但我的编辑器没有发现。发生这种情况时,调用
Components.utils.import("resource://modules/Foo.js")
将无法导入或返回任何内容,而不会提供任何关于在文件中查找语法错误的有用提示。当我的 导入 失败?
编辑:我解决了自己的问题,结果是我使用的代码具有对 navigator
对象的全局引用。让这个特别烦人的是,代码在浏览器中加载时可以工作(正如下面弗拉基米尔建议的那样),但在我的扩展中导入时仍然会失败。
最终我采取了一种手动二分搜索的方式:删除一半文件,然后查看导入是否仍然失败。如果是这样,那么我会删除剩余的一半并重复。一旦它没有失败,我就可以更准确地知道问题出在哪里,这使我可以继续二分搜索或手动扫描较小的区域来查找问题。
这是非常耗时的,我仍然感谢任何有关如何加快此调试过程的建议。
When working on a large Firefox plugin, I sometimes accidentally make a syntax error not caught by my editor. When this happens, then calling
Components.utils.import("resource://modules/Foo.js")
will simply fail to import or return anything, without any kind of helpful hint as to where in the file to look for the syntax error. Is there any way I can get Firefox to give me some kind of clue when my imports fail?
EDIT: I fixed my own problem, which turned out to be that I was using code which had a global reference to the navigator
object. What made this especially annoying was that the code would work when loaded in the browser (as Wladimir suggested below), but would still fail when importing in my extension.
Eventually I resorted to a sort of manual binary search: I'd delete half of the file and then see whether the import still failed. If so, then I'd delete half of what remained and repeat. As soon as it didn't fail, I had a more precise notion of where the problem was, which allowed me to either continue the binary search or scan the smaller area manually looking for the problem.
This is extremely time-consuming and I'd still appreciate any suggestions about how to speed up this debugging process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题很可能是 https://developer.mozilla.org/en/Exception_logging_in_JavaScript 并将
dom.report_all_js_exceptions
首选项设置为true
应该可以。我必须承认我还没有尝试过,因为设置此首选项会使错误控制台非常吵闹。相反,我使用了一个丑陋的 hack 并将模块作为脚本加载到本地 HTML 文件中 - 这足以向我显示语法错误,幸运的是这不是我需要经常做的事情(这只是语法错误的问题,通常会报告运行时错误)。The issue here is most likely the one described on https://developer.mozilla.org/en/Exception_logging_in_JavaScript and setting
dom.report_all_js_exceptions
preference totrue
should work. I must admit that I haven't tried that however because setting this preference makes the error console very noisy. Instead I use an ugly hack and load the module as a script in a local HTML file - this is enough to show me syntax errors and fortunately isn't something I need to do all too often (it is only an issue with syntax errors, runtime errors are reported as usually).