How do I programatically open the Developer Tools' Console in a Firefox addon?

房东的猫 提交于 2019-12-10 18:23:36

问题


I have a simple context menu extension that logs an AngularJS scope to unsafeWindow.console:

require("sdk/context-menu").contextMenu.Item({
    label: "Inspect Angular scope",
    context: contextMenu.PageContext(),
    contentScript: 'self.on("click", function(node) {' +
    '    if (unsafeWindow.angular) {' +
    '        unsafeWindow.console.log(unsafeWindow.angular.element(node).scope());' +
    '        self.postMessage(true);' +
    '    } else {' +
    '        unsafeWindow.alert("No Angular scope available");' +
    '    }' +
    '});',
    onMessage: function() {
        // Open Web Console
    }
});

The logging part works, but I need to fill in the blank for // Open Web Console, where I want to automatically open the Web Developer Tools, with the Console tab selected so that the user will see what just got logged.

How can this be done using the Firefox Addon SDK?

Can I also programmatically show the full object in the Developer Tools sidebar as if the logged object were clicked inside the Web Console?


回答1:


Cool question, yes this can be opened programtically. I'm a recent fan of angular and am using it in all my html5 apps for firefox.

This is how to open the devtools into the web console:

var {Cu} = require('chrome');
let { devtools } = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
let myDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
let gBrowser = myDOMWindow.gBrowser;
let tt = devtools.TargetFactory.forTab(gBrowser.selectedTab);
gDevTools.showToolbox(tt, "webconsole").then(function(toolbox) {
    let inspector = toolbox.getCurrentPanel();
    console.log('inspector:', inspector);
    /*
      if (this.isRemote) {
          myDOMWindow.messageManager.sendAsyncMessage("debug:inspect", {}, {
              node: this.target
          });
          inspector.walker.findInspectingNode().then(nodeFront => {
              inspector.selection.setNodeFront(nodeFront, "browser-context-menu");
          });
      } else {
          inspector.selection.setNode(this.target, "browser-context-menu");
      }
      */
});

Heres documentation from the firefox codebase on this showToolbox function: https://dxr.mozilla.org/mozilla-central/source/browser/devtools/framework/gDevTools.jsm#378



来源:https://stackoverflow.com/questions/32528437/how-do-i-programatically-open-the-developer-tools-console-in-a-firefox-addon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!