javascript 如何调试 Firefox 扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6855359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:07:08  来源:igfitidea点击:

How to debug Firefox extension

javascriptfirefoxfirefox-addonxul

提问by Alex K

I've been into Firefox extension development recently, and ran into some issues:

我最近一直在做 Firefox 扩展开发,遇到了一些问题:

So, in browser.xul i defined these lines:

所以,在 browser.xul 中我定义了这些行:

<overlay id="sample" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script src="jquery.js" />
    <script src="global.js" />
</overlay>

So, in global.js i have access to all jQuery stuff, and trying to load a simple script there:

因此,在 global.js 中,我可以访问所有 jQuery 内容,并尝试在那里加载一个简单的脚本:

var inner = null;
var o = function () {
    var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    return {
        init : function () {
            alert('here loading inner..');
            $.get('http://www.example.com/script.js', function(d) {
                alert('loaded inner script!');
                inner = d;
                gBrowser.addEventListener("load", function () {
                    alert('onload');
                }, false);
            }).error(function(e) { alert('error loading inner..'); setTimeout(o.init,1000); });
            $(this).ajaxError(function() { alert('ajaxError'); });
        }
    }
}
window.addEventListener("load", o.init, false);

But nor i receive a "loaded inner script", nor a "error loading inner" alert.. And i don't see the error console to log any errors from the extension... I assume the $.getis silently failing due to some restrictions maybe, but is there a proper way to debug the errors normally? The error console is silent for the extension, it only shows errors from the web pages

但是我也没有收到“加载的内部脚本”,也没有收到“错误加载内部”警报..而且我没有看到错误控制台来记录扩展中的任何错误......我认为$.get由于某些限制,它正在默默地失败也许,但是有没有一种正确的方法可以正常调试错误?扩展的错误控制台是静默的,它只显示来自网页的错误

采纳答案by Matthew Wilson

If you look at the article https://developer.mozilla.org/en/Setting_up_extension_development_environmentit suggests setting up some preferences, including

如果您查看文章https://developer.mozilla.org/en/Setting_up_extension_development_environment它建议设置一些首选项,包括

javascript.options.showInConsole= true. Logs errors in chrome files to the Error Console.

javascript.options.showInConsole= 真的。将 chrome 文件中的错误记录到错误控制台。

回答by Matthew Gertner

In general it can be problematic using JQuery in a XUL page since it assumes that the document is an HTMLDOM rather than an XMLDOM and that the windowis a HTMLwindow rather than a XULwindow. If I were you I'd use the subscript loaderfor this. To debug you can use Venkmanalthough it is a bit flakey and I often resort to just dump()statements to the console instead.

通常,在 XUL 页面中使用 JQuery 可能会出现问题,因为它假定文档是HTMLDOM 而不是XMLDOM,并且windowHTML窗口而不是XUL窗口。如果我是你,我会为此使用下标加载器。要调试,您可以使用Venkman,尽管它有点古怪,而且我经常只求助于dump()控制台的语句。

Update:see my comment below about the Browser Toolbox.

更新:请参阅下面关于Browser Toolbox 的评论。