Console logging from a Firefox add-on

末鹿安然 提交于 2019-12-12 08:56:42

问题


The Mozilla documentation (https://addons.mozilla.org/en-US/developers/docs/sdk/latest/dev-guide/console.html) says that I should use console.log to generate messages from an extension. Those messages are said to appear in the Firefox Error Console. But this is not the case for me. I'm using the Addon builder for the first time today, and I'd like to create an extension that switches tabs on certain events. The tabs are indeed switched, and to a tab which I expected, so my code definitely runs. But the console.log output is nowhere to see.

I have set the filter to "All". All I see are CSS warnings from the addon builder itself.

I have also installed Firebug. It doesn't show anything, too. (This works fine when using console.log from the context of a web page though.) The problem with Firebug would be that it's only enabled for one/some tab anyway, so when switching tabs, it's useless. I need a log window that's always there.

So where will the output from console.log end up?


回答1:


Go ahead and put a test console.log("something") in your addon main();

If nothing shows up in the Error Console ('Messages' tab), then maybe Firefox isn't configured to show console.log (happened recently with jetpack sdk 1.14). See: Changes to console.log behaviour in SDK 1.14 for details.

Quick and dirty summary: In about:config set extensions.sdk.console.logLevel to "all"

Although from your question:

I have set the filter to "All".

... it sounded like your were already aware of this. So it's not entirely clear what you meant by that.




回答2:


Instead of doing var aConsoleService = Cc... just stick in the following and you can use everything:

Cu.import('resource://gre/modules/devtools/Console.jsm');

can now do whatever, console.log('blah'), console.time('rawr'), console.endTime('rawr'), etc etc etc




回答3:


Just for completeness: in a bootstrapped, non-SDK-based addon, I had to add these two lines in bootstrap.js to have a faked console.log():

var aConsoleService = Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService);
var console = {log:function(str){aConsoleService.logStringMessage(str);}}

Might come in handy for somebody else, I guess..




回答4:


I guess they changed something, now console.log isn't displayed. I use console.error for debugging, it still shows up in ctrl-shift-j.

This page say something about deprecation of Error Console and using Web Console instead. It's probably related. https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIConsoleService

This doesn't require the sdk.



来源:https://stackoverflow.com/questions/15988025/console-logging-from-a-firefox-add-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!