简体   繁体   English

从JavaScript登录到Firefox错误控制台

[英]Log to Firefox Error Console from JavaScript

Is it possible to add messages to the built-in error console of Firefox from JavaScript code running in web pages? 是否可以从网页中运行的JavaScript代码向Firefox的内置错误控制台添加消息?

I know that I there's Firebug, which provides a console object and its own error console, but I was looking for a quick fix earlier on and couldn't find anything. 我知道我有Firebug,它提供了一个console对象和它自己的错误控制台,但我之前正在寻找一个快速修复,但找不到任何东西。

I guess it might not be possible at all, to prevent malicious web pages from spamming the log? 我想可能根本不可能防止恶意网页发送垃圾邮件?

If you define a global function that checks for the existence of window.console, you can use Firebug for tracing and still plays nice with other browsers and/or if you turn Firebug's console tracing off: 如果您定义一个检查window.console是否存在的全局函数,您可以使用Firebug进行跟踪,并且仍可以与其他浏览器一起使用和/或如果您关闭Firebug的控制台跟踪:

debug = function (log_txt) {
    if (typeof window.console != 'undefined') {
        console.log(log_txt);
    }
}

debug("foo!");

Yes, you can =P 是的,你可以= P.

function log(param){
    setTimeout(function(){
        throw new Error("Debug: " + param)
    },0)
}

//Simple Test:
alert(1)
log('This is my message to the error log -_-')
alert(2)
log('I can do this forever, does not break')
alert(3)

Update to a real function 更新到实际功能

This is a simple hack, just for fun. 这是一个简单的黑客,只是为了好玩。

You cannot write to the console directly from untrusted JavaScript (eg scripts coming from a page). 您无法直接从不受信任的JavaScript(例如来自页面的脚本)写入控制台。 However, even if installing Firebug does not appeal to you, I'd recommend checking out Firebug Lite , which requires no installation into the browser (nor, in fact, does it even require Firefox). 但是,即使安装Firebug对您没有吸引力,我也建议您查看Firebug Lite ,它不需要安装到浏览器中(事实上,它甚至不需要Firefox)。 It's a script which you can include into any web page (even dynamically), which will give you some basic Firebug functionality (such as console.log() ). 这是一个可以包含在任何网页中的脚本(甚至是动态的),它将为您提供一些基本的Firebug功能(例如console.log() )。

window.console is undefined in Firefox 4 beta 6 even if Firebug 1.6X.0b1 is enabled and open, probably because of privilege issues that others discuss. 即使Firebug 1.6X.0b1已启用并打开,Firefox 4 beta 6中也未定义window.console,可能是因为其他人讨论的权限问题。 However, Firefox 4 has a new Tools > Web Console, and if this is open you have a window.console object and untrusted JavaScript code on the page can use console.log(). 但是,Firefox 4有一个新的工具> Web控制台,如果这是打开的,你有一个window.console对象,页面上不受信任的JavaScript代码可以使用console.log()。 The Web Console is in flux (see https://wiki.mozilla.org/Firefox/Projects/Console ), you may need to change settings named devtools.* in about:config , YMMV. Web控制台不断变化(请参阅https://wiki.mozilla.org/Firefox/Projects/Console ),您可能需要在about:config,YMMV中更改名为devtools。*的设置。

I would just install Firebug and use console.log . 我只是安装Firebug并使用console.log If you can't do that, though, you can always throw an error: 但是,如果你不能这样做,你总是会抛出一个错误:

throw "foobar";
throw new Error("bazquux");

Of course, this will break you out of the code that you're currently executing, so you can't use it for detailed logging, but if you can work around that I think it's the only way to get something logged out of the box. 当然,这会让你脱离你当前正在执行的代码,所以你不能用它来进行详细的日志记录,但如果你可以解决这个问题,我认为这是从盒子里取出一些东西的唯一方法。

AFAIK, it is not possible. AFAIK,这是不可能的。 But if you are interested in how extensions in Firefox interact with the error console, check this out . 但如果您对Firefox中的扩展如何与错误控制台交互感兴趣, 请查看此信息

This function does not require any extension nor library. 此功能不需要任何扩展名或库。 However it grants full privileges to the relevant website. 但是,它授予相关网站完全权限。 No worries since you are the one developing it, right? 不用担心,因为你是发展它的人,对吗?


// Define mylog() function to log to Firefox' error console if such a
// thing exists
function defineMyLog()
{
    // Provide a useless but harmless fallback
    mylog = function(msg) { };
    // return; // disable in production

    if (typeof(netscape) === "undefined") {
        // alert("Logging implemented only for Firefox");
        return;
    }
    // The initial auth popup can be avoided by pre-setting some magic user_pref
    //  ( "capability.principal.codebase.p0.granted", "UniversalXPConnect"), etc.
    try {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    } catch (e) { // User has denied privileges
        // alert(e.name + ": " + e.message);
        return;
    }
    ffconsoleService = Components.classes["@mozilla.org/consoleservice;1"]
                                 .getService(Components.interfaces.nsIConsoleService);
    mylog = function (msg)
    {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        ffconsoleService.logStringMessage(new Date().toLocaleTimeString() + ": " + msg);
    }
    mylog("Firefox logging function has been defined");

    // window.open("javascript:"); // this URL does not work anymore?
}

If you're interested, check out a script I wrote -- it's a "cheap" Firebug replacement that doesn't interfere with any normal console (like Safari or Chrome) but does extend it with almost all the Firebug methods: 如果您有兴趣,请查看我写的脚本 - 它是一个“便宜”的Firebug替代品,不会干扰任何普通的控制台(如Safari或Chrome),但几乎可以使用所有Firebug方法扩展它:

http://code.google.com/p/glentilities/ http://code.google.com/p/glentilities/

Look under the hood and you'll see what I mean by "cheap". 看看引擎盖下你会看到“便宜”的意思。 :-) :-)

Combine it with YUI or json.org's JSON serializers to sorta replicate console.dir. 将它与YUI或json.org的JSON序列化器结合使用,以对sorta复制console.dir进行排序。

Firebug and Firebug Lite are definitely nicer GUIs, but I use my home-grown one all the time to retain logging safely even for production code -- without constant commenting & un-commenting, Firebug和Firebug Lite绝对是更好的图形用户界面,但是我一直使用我自己开发的图形用户界面,即使对于生产代码也能安全地保存日志 - 没有持续的评论和取消评论,

I had an issue today, and notice that the Console in Firebug has different tabs, and mine was in Depuration Information, and you must select the ALL option in order to see console.log work without trowing errors! 我今天遇到了一个问题,并注意到Firebug中的控制台有不同的选项卡,而我的是Depuration Information,你必须选择ALL选项才能看到console.log工作而不会出现错误! Simple like this! 很简单! ;^) ; ^)

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

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