如何在Firefox WebExtension中查看后台脚本的console.log输出?

Gra*_*ham 5 javascript firefox firefox-addon firefox-developer-tools firefox-addon-webextensions

有谁知道如何console.log()在后台脚本中查看调用的输出?我可以在内容脚本中看到相同的输出.这是一个简单的脚本,我正在测试它:

这是我的background.js:

console.log("Message from background.js");
Run Code Online (Sandbox Code Playgroud)

这是我的manifest.json:

{
    "name": "TestBed",
    "manifest_version": 2,
    "version": "1.0",

    "background": {
        "scripts": ["background.js"]
    },

    "browser_action": {
        "default_title": "Click"
    },

    "applications": {
        "gecko": {
            "id": "testbed@example.com",
            "strict_min_version": "48.0a1"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我也在后台脚本中试过这个:

chrome.browserAction.onClicked.addListener(function() {
    console.log('Message from background.js onclicked handler');
});
Run Code Online (Sandbox Code Playgroud)

我甚至像其他一些帖子所建议的那样卸载了Firebug,但这也没有任何区别(注意console.log内容脚本有效).

Mak*_*yen 6

有关在控制台中查看扩展程序输出的更一般答案,请参阅我的答案:Google Chrome/Firefox在控制台中看不到扩展程序输出.

您可以console.log()浏览器控制台中查看后台脚本 的输出.您可以使用OSX上的键盘快捷键Ctrl- Shift- JCmd- Shift- J或Firefox菜单栏:Tools➞WebDeveloper➞ 浏览器控制台打开浏览器控制台.

当版本测试WebExtensions大于或等于49,1我经常被滥用的不良特性,使扩展到打开浏览器控制台.alert()后台脚本不支持该功能,但会打开浏览器控制台并在控制台中输出警报文本.2这样做适用于大于或等于49.0的Firefox版本.但是,它在早期版本的Firefox中引发了错误.

为了测试,我经常在我的后台脚本中包含以下内容:

//* For testing, open the Browser Console
try {
    //alert() is not actually supported in Firefox WebExtension background scripts.
    //This forces the Browser Console open.
    //This abuse of a misfeature works in FF49.0b+, not in FF48.
    alert('Open the Browser Console.');
} catch(e) {
    //alert() throws an error in Firefox versions below 49.
    console.log('alert() threw an error. Probably Firefox version < 49.');
}
//*
Run Code Online (Sandbox Code Playgroud)
  1. 在Firefox 52.0a2(开发人员版)和53.0a1(夜间版)中有一段时间,它会引发一个神秘的错误.在这些版本的最新版本中,这已经返回到Firefox 49中看到的功能:打开浏览器控制台并在2(下面)中显示传递的文本和消息.
  2. 除了传入的文本之外alert(),它还将在单独的行中输出:"后台窗口不支持"alert();请改用console.log."

  • 我碰巧打开了正确的控制台,因为我总是在重新加载 JavaScript 更改后检查那里是否有任何错误。但对于 `console.log` 我仍然必须检查“**显示内容消息**”(firefox 版本 70)。 (3认同)