Firefox附加组件访问所有控制台日志信息

我正在开发一个Firefox附加组件 . 作为附加组件的一部分,我打开一个带有新页面的选项卡,一旦页面完全加载,我想访问Web控制台日志信息(css / javascript错误.....

加载页面后如何访问所有控制台日志信息?

回答(1)

2 years ago

首先,您必须获取要从中获取消息的窗口的innerID .

使用sdk,您可以使用 var innerID = require("sdk/window/utils").getInnerId(require("sdk/view/core").viewFor(require("sdk/windows").activeWindow))

或者更干净:

var utils = require("sdk/window/utils");
var viewFor = require("sdk/view/core").viewFor;
var windows = require("sdk/windows");


var innerID = utils.getInnerId(viewFor(windows.activeWindow));

然后,您希望观察该窗口的控制台通知:

var system = require("sdk/system/events");

system.on('console-api-log-event', onMessage);

function onMessage(subject) {
  subject = subject.wrappedJSObject;

  if (subject.innerID != innerID) {
    return null; //ignore this console log, because it is for a window/thing that I don't care about
  }

  if (subject.level != "log") {
    return null; // ignore anything but console.log, like: console.info, console.error, console.debug, etc.
  }

  var message = subject.arguments[0]; // first string argument to console.log()
  // do something with the console.log("") message
}