如何在Firefox WebExtension中查看后台脚本的console.log输出? [英] How do I see the console.log output of a background script in a Firefox WebExtension?

查看:308
本文介绍了如何在Firefox WebExtension中查看后台脚本的console.log输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的 background.js

  console.log(Message from background.js); 

这是我的 manifest.json

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

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

browser_action:{
default_title:单击


applications:{
gecko:{
id:testbed @ example .com,
strict_min_version:48.0a1
}
}
}

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

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

我甚至已经卸载了Firebug,就像其他一些帖子所建议的一样,内容脚本中的 console.log )。

解决方案

请参阅 console.log() / Browser_Consolerel =nofollow noreferrer>浏览器控制台。您可以使用键盘快捷键 Ctrl - Shift - J Cmd 打开浏览器控制台。在OSX上,或从Firefox菜单栏选择 Shift - J :工具→Web开发者→浏览器控制台

当测试版本大于或等于49的WebExtensions时,我经常滥用misfeature,导致扩展程序打开浏览器控制台。后台脚本不支持 alert()函数,但会打开浏览器控制台并在控制台中输出警报文本。 2 这样做将在大于或等于49.0的Firefox版本中工作。但是,它会在早期版本的Firefox中引发错误。



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

  // *要进行测试,请打开浏览器控制台
尝试{
// Firefox的警报不受支持。这强制浏览器控制台打开。
//这个滥用错误的功能在FF49.0b +中,而不是在FF48
alert('打开浏览器控制台')。
catch(e){
// alert()会在49以下的Firefox版本中抛出一个错误。
console.log('Alert发生错误,可能是Firefox版本< 49。 );






$ b



$ b


  1. 在Firefox 52.0a2(Developer Edition)和53.0a1(Nightly)中有一段时间,它会抛出一个神秘的错误。在这些版本的最新版本中,这已经返回到了Firefox 49的功能:打开浏览器控制台并显示传递的文本和消息(见下文2)。

  2. 除了传递给 alert()的文本之外,它还将在另一行输出:alert()在后台窗口中不受支持;请改用console.log。


Does anyone know how to see the output from a console.log() call in a background script? 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:

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

Here is my 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).

解决方案

You can see the output of console.log() from you background scripts in the Browser Console. 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.

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. The alert() function is not supported in background scripts, but will open the Browser Console and output the alert text in the console.2 Doing so will work in Firefox versions greater than or equal to 49.0. However, it throws an error in earlier versions of Firefox.

For testing, I often include in my background scripts something like:

//* For testing, open the Browser Console
try{
    //Alert is not supported in Firefox. 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. 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).
  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."

这篇关于如何在Firefox WebExtension中查看后台脚本的console.log输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆