Created
July 31, 2013 21:21
-
-
Save Gozala/6126308 to your computer and use it in GitHub Desktop.
execute command with GCLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"] | |
.getService(Ci.nsIWindowMediator); | |
Cu.import("resource://gre/modules/devtools/gcli.jsm", {}); | |
let { CommandUtils } = Cu.import("resource:///modules/devtools/DeveloperToolbar.jsm", {}); | |
let { require } = Cu.import("resource://gre/modules/devtools/Require.jsm", {}); | |
let { Requisition } = require("gcli/cli"); | |
let { Promise: { defer } } = Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js", {}); | |
let gcliExec = (command) => { | |
let chromeWindow = command.chromeWindow || | |
windowMediator.getMostRecentWindow("navigator:browser"); | |
let contentWindow = command.contentWindow || | |
chromeWindow.gBrowser.selectedTab.linkedBrowser.contentWindow; | |
let environment = CommandUtils.createEnvironment(chromeWindow.document, | |
contentWindow.document); | |
let requisition = new Requisition(environment); | |
let output = requisition.exec(command); | |
return output.promise.then(function() { | |
return output.data | |
}, function() { | |
throw Error(output.data) | |
}) | |
} | |
gcliExec({ | |
command: "tilt toggle", | |
args: {} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Status of the changes outlined above:
A few extra notes worth mentioning:
I don't think it's a good idea to express command failure in the state of the promise for 2 reasons:
That given the code above becomes:
I am considering moving CommandUtils into gcli.jsm, in which case this would become:
I'm intending on taking further tweaks on a case by case basis because it's not totally clear what is beneficial to hide.