Bug 623591 - Add basic support for window.console [r=vingtetun]

This commit is contained in:
Mark Finkle 2011-01-06 12:42:03 -05:00
parent bf087af391
commit 39218ae8d2

View File

@ -1016,3 +1016,81 @@ var FindHandler = {
};
FindHandler.init();
var ConsoleAPIObserver = {
init: function init() {
addMessageListener("Browser:TabOpen", this);
addMessageListener("Browser:TabClose", this);
},
receiveMessage: function receiveMessage(aMessage) {
let json = aMessage.json;
switch (aMessage.name) {
case "Browser:TabOpen":
Services.obs.addObserver(this, "console-api-log-event", false);
break;
case "Browser:TabClose":
Services.obs.removeObserver(this, "console-api-log-event", false);
break;
}
},
observe: function observe(aMessage, aTopic, aData) {
let contentWindowId = content.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils).outerWindowID;
aMessage = aMessage.wrappedJSObject;
if (aMessage.ID != contentWindowId)
return;
let mappedArguments = Array.map(aMessage.arguments, this.formatResult, this);
let joinedArguments = Array.join(mappedArguments, " ");
if (aMessage.level == "error" || aMessage.level == "warn") {
let flag = (aMessage.level == "error" ? Ci.nsIScriptError.errorFlag : Ci.nsIScriptError.warningFlag);
let consoleMsg = Cc["@mozilla.org/scripterror;1"].createInstance(Ci.nsIScriptError);
consoleMsg.init(joinedArguments, null, null, 0, 0, flag, "content javascript");
Services.console.logMessage(consoleMsg);
} else {
Services.console.logStringMessage(joinedArguments);
}
},
getResultType: function getResultType(aResult) {
let type = aResult === null ? "null" : typeof aResult;
if (type == "object" && aResult.constructor && aResult.constructor.name)
type = aResult.constructor.name;
return type.toLowerCase();
},
formatResult: function formatResult(aResult) {
let output = "";
let type = this.getResultType(aResult);
switch (type) {
case "string":
case "boolean":
case "date":
case "error":
case "number":
case "regexp":
output = aResult.toString();
break;
case "null":
case "undefined":
output = type;
break;
default:
if (aResult.toSource) {
try {
output = aResult.toSource();
} catch (ex) { }
}
if (!output || output == "({})") {
output = aResult.toString();
}
break;
}
return output;
}
};
ConsoleAPIObserver.init();