Logging to console from Firefox extension?

2019-02-12 22:38发布

I understand that I can log to the console when using a Firefox extension with the cfx run command.

Is there a way to log to a console after the extension has been packaged with cfx xpi? Logging to Firebug is fine if possible.

I have found two blog posts about this (here and here). Both are rather old and don't work any more.

I'm using version 1.10 of the add-on SDK and FF15.

4条回答
萌系小妹纸
2楼-- · 2019-02-12 22:52

You need to do 2 things:

  1. enable logging for addons

    • in about:config, add a new option "extensions.sdk.console.logLevel" and give it the value "all"

    • restart Firefox

  2. in Firefox open the Browser Console:

    • Tools -> Web developer -> Browser console

    • NOTE: this is different from the usual Web Console used to debug web pages

You should see addons logs there now.

查看更多
贪生不怕死
3楼-- · 2019-02-12 22:59

TLTR:

  1. Go to about:config url and create key extensions.sdk.console.logLevel with value all
  2. See log messages either in Browser console (Ctrl+Shift+J) or in terminal you started Firefox from.

cfx or its successor jpm creates this configuration key automatically in development firefox profile.

From logging documentation:

extensions.sdk.console.logLevel: if set, this determines the logging level for all installed SDK-based add-ons.

extensions.extensionID.sdk.console.logLevel, where extensionID is an add-on's Program ID. If set, this determines the logging level for the specified add-on. This overrides the global preference if both preferences are set.

查看更多
We Are One
4楼-- · 2019-02-12 23:02

Have you tried console.log() with Firebug? (I know you have tried Application.console.log() and Firebug.console.log() already)

I just tried it in the Web Console on FF16 running Firebug 1.10.4 and it seems to work: Firefox Web Console

Here are some more examples from the Firebug Wiki itself: FirebugWiki Console API

Additionally, you can write messages of different types in the console, such as: console.debug(), console.info(), console.warn(), console.error()

查看更多
迷人小祖宗
5楼-- · 2019-02-12 23:03

If you run console.log from Add-on code, it send up in the 'Messages' tab of the Error Console window:

https://www.evernote.com/shard/s1/sh/c40c509d-9f14-4103-920b-4186a426226c/53572f96a3bbe0462103a8126b8e9822/res/fcbead60-d9d0-48be-b744-3fa6701fdf1b/skitch.png

查看更多
登录 后发表回答