简体   繁体   English

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

[英]How do I see the console.log output of a background script in a Firefox WebExtension?

Does anyone know how to see the output from a console.log() call in a background script?有谁知道如何在后台脚本中查看console.log()调用的输出? I can see the output from the same in a content script.我可以在内容脚本中看到相同的输出。 Here is a simple script that I am testing this with:这是我正在测试的一个简单脚本:

Here is my background.js :这是我的background.js

console.log("Message from background.js");

Here is my manifest.json :这是我的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"
        }
    }
}

I've also tried this in the background script:我也在后台脚本中尝试过这个:

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

I have even uninstalled Firebug as some other posts have suggested, but that made no difference either (note that the console.log in content scripts works).我什至按照其他一些帖子的建议卸载了 Firebug,但这也没有区别(请注意,内容脚本中的console.log有效)。

For a more general answer about seeing extension output in the console, please see my answer to: Google Chrome / Firefox do not see extension output in console .有关在控制台中看到扩展输出的更一般的答案,请参阅我的回答: Google Chrome / Firefox do not see extension output in console

Add-on Debugger 附加调试器

This is what you should be using to view console output from scripts running in the background context of your WebExtension.这是您应该用来查看在 WebExtension 的后台上下文中运行的脚本的控制台输出的内容。 This includes background scripts, scripts running in popups, options pages, and any other page loaded from the extension as the main URL for a tab, or iframe.这包括后台脚本、在弹出窗口中运行的脚本、选项页面以及从扩展程序加载的任何其他页面,作为选项卡或 iframe 的主 URL。 You can access the Add-on Debugger though about:debugging ➞Inspect (use the "Inspect" button that's associated with the WebExtension you are debugging; there's a separate button for each extension).您可以通过about:debugging ➞Inspect 访问附加调试器(使用与您正在调试的 WebExtension 相关联的“Inspect”按钮;每个扩展都有一个单独的按钮)。 This will open a new tab with the debugger.这将打开一个带有调试器的新选项卡。 You can then click on the Console tab within that browser tab.然后,您可以单击该浏览器选项卡中的控制台选项卡。 This console will display only content from the WebExtension which you are inspecting.此控制台将仅显示您正在检查的 WebExtension 中的内容。

Browser Console 浏览器控制台

The Browser Console no longer shows output from WebExtensions background pages by default .默认情况下,浏览器控制台不再显示来自 WebExtensions 背景页面输出。 You can have it show output from all WebExtensions by selecting to display "Show Content Messages", which is available from the popup that opens when you click on the gear-like symbol "⚙️" in the upper right of the window, just to the right of "Requests".您可以通过选择显示“显示内容消息”来显示所有WebExtensions 的输出,当您单击窗口右上角的齿轮状符号“⚙️”时,可以从打开的弹出窗口中找到它,就在“请求”的权利。

You can see the output of console.log() from you background scripts in the Browser Console .您可以在Browser Console中的后台脚本中看到console.log()的输出。 You can open the Browser Console by either using the keyboard shortcut, Ctrl - Shift - J , or Cmd - Shift - J on OSX, or from the Firefox menu bar: Tools➞Web Developer➞Browser Console.您可以使用键盘快捷键Ctrl - Shift - JCmd - Shift - J(在 OSX 上)或从 Firefox 菜单栏:工具➞Web 开发人员➞浏览器控制台打开浏览器控制台。

When testing WebExtensions in versions greater than or equal to 49, 1 I routinely abuse a misfeature to cause the extension to open the Browser Console.在大于或等于 49 的版本中测试 WebExtensions 时, 1我经常滥用错误功能导致扩展程序打开浏览器控制台。 The alert() function is not supported in background scripts, but will open the Browser Console and output the alert text in the console.后台脚本不支持alert()函数,但会打开浏览器控制台并在控制台中输出警报文本。 2 Doing so will work in Firefox versions greater than or equal to 49.0. 2这样做将适用于大于或等于 49.0 的 Firefox 版本。 However, it throws an error in earlier versions of Firefox.但是,它会在早期版本的 Firefox 中引发错误。

For testing, I often include in my background scripts something like:为了测试,我经常在我的后台脚本中包含以下内容:

//* 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.');
}
//*

  1. For a while in Firefox 52.0a2 (Developer Edition) and 53.0a1 (Nightly), it would instead throw a cryptic error.在 Firefox 52.0a2(开发者版)和 53.0a1(Nightly)中有一段时间,它会抛出一个神秘的错误。 In the most recent builds of these versions, this has been returned to the functionality seen in Firefox 49: opening the Browser Console and displaying passed text and the message in 2 (below).在这些版本的最新版本中,这已恢复到 Firefox 49 中的功能:打开浏览器控制台并显示传递的文本和消息 2(如下)。
  2. In addition to the text passed to alert() , it will also output on a separate line: "alert() is not supported in background windows; please use console.log instead."除了传递给alert()的文本外,它还将在单独的行中输出:“后台窗口不支持alert();请改用console.log。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM