toolkit/devtools/gcli/commands/jsb.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/gcli/commands/jsb.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,133 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const { Cc, Ci, Cu } = require("chrome");
    1.11 +const gcli = require("gcli/index");
    1.12 +const XMLHttpRequest = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"];
    1.13 +
    1.14 +loader.lazyImporter(this, "js_beautify", "resource:///modules/devtools/Jsbeautify.jsm");
    1.15 +
    1.16 +exports.items = [
    1.17 +  {
    1.18 +    name: "jsb",
    1.19 +    description: gcli.lookup("jsbDesc"),
    1.20 +    returnValue:"string",
    1.21 +    params: [
    1.22 +      {
    1.23 +        name: "url",
    1.24 +        type: "string",
    1.25 +        description: gcli.lookup("jsbUrlDesc")
    1.26 +      },
    1.27 +      {
    1.28 +        group: gcli.lookup("jsbOptionsDesc"),
    1.29 +        params: [
    1.30 +          {
    1.31 +            name: "indentSize",
    1.32 +            type: "number",
    1.33 +            description: gcli.lookup("jsbIndentSizeDesc"),
    1.34 +            manual: gcli.lookup("jsbIndentSizeManual"),
    1.35 +            defaultValue: 2
    1.36 +          },
    1.37 +          {
    1.38 +            name: "indentChar",
    1.39 +            type: {
    1.40 +              name: "selection",
    1.41 +              lookup: [
    1.42 +                { name: "space", value: " " },
    1.43 +                { name: "tab", value: "\t" }
    1.44 +              ]
    1.45 +            },
    1.46 +            description: gcli.lookup("jsbIndentCharDesc"),
    1.47 +            manual: gcli.lookup("jsbIndentCharManual"),
    1.48 +            defaultValue: " ",
    1.49 +          },
    1.50 +          {
    1.51 +            name: "doNotPreserveNewlines",
    1.52 +            type: "boolean",
    1.53 +            description: gcli.lookup("jsbDoNotPreserveNewlinesDesc")
    1.54 +          },
    1.55 +          {
    1.56 +            name: "preserveMaxNewlines",
    1.57 +            type: "number",
    1.58 +            description: gcli.lookup("jsbPreserveMaxNewlinesDesc"),
    1.59 +            manual: gcli.lookup("jsbPreserveMaxNewlinesManual"),
    1.60 +            defaultValue: -1
    1.61 +          },
    1.62 +          {
    1.63 +            name: "jslintHappy",
    1.64 +            type: "boolean",
    1.65 +            description: gcli.lookup("jsbJslintHappyDesc"),
    1.66 +            manual: gcli.lookup("jsbJslintHappyManual")
    1.67 +          },
    1.68 +          {
    1.69 +            name: "braceStyle",
    1.70 +            type: {
    1.71 +              name: "selection",
    1.72 +              data: ["collapse", "expand", "end-expand", "expand-strict"]
    1.73 +            },
    1.74 +            description: gcli.lookup("jsbBraceStyleDesc2"),
    1.75 +            manual: gcli.lookup("jsbBraceStyleManual2"),
    1.76 +            defaultValue: "collapse"
    1.77 +          },
    1.78 +          {
    1.79 +            name: "noSpaceBeforeConditional",
    1.80 +            type: "boolean",
    1.81 +            description: gcli.lookup("jsbNoSpaceBeforeConditionalDesc")
    1.82 +          },
    1.83 +          {
    1.84 +            name: "unescapeStrings",
    1.85 +            type: "boolean",
    1.86 +            description: gcli.lookup("jsbUnescapeStringsDesc"),
    1.87 +            manual: gcli.lookup("jsbUnescapeStringsManual")
    1.88 +          }
    1.89 +        ]
    1.90 +      }
    1.91 +    ],
    1.92 +    exec: function(args, context) {
    1.93 +      let opts = {
    1.94 +        indent_size: args.indentSize,
    1.95 +        indent_char: args.indentChar,
    1.96 +        preserve_newlines: !args.doNotPreserveNewlines,
    1.97 +        max_preserve_newlines: args.preserveMaxNewlines == -1 ?
    1.98 +                              undefined : args.preserveMaxNewlines,
    1.99 +        jslint_happy: args.jslintHappy,
   1.100 +        brace_style: args.braceStyle,
   1.101 +        space_before_conditional: !args.noSpaceBeforeConditional,
   1.102 +        unescape_strings: args.unescapeStrings
   1.103 +      };
   1.104 +
   1.105 +      let xhr = new XMLHttpRequest();
   1.106 +
   1.107 +      try {
   1.108 +        xhr.open("GET", args.url, true);
   1.109 +      } catch(e) {
   1.110 +        return gcli.lookup("jsbInvalidURL");
   1.111 +      }
   1.112 +
   1.113 +      let deferred = context.defer();
   1.114 +
   1.115 +      xhr.onreadystatechange = function(aEvt) {
   1.116 +        if (xhr.readyState == 4) {
   1.117 +          if (xhr.status == 200 || xhr.status == 0) {
   1.118 +            let browserDoc = context.environment.chromeDocument;
   1.119 +            let browserWindow = browserDoc.defaultView;
   1.120 +            let gBrowser = browserWindow.gBrowser;
   1.121 +            let result = js_beautify(xhr.responseText, opts);
   1.122 +
   1.123 +            browserWindow.Scratchpad.ScratchpadManager.openScratchpad({text: result});
   1.124 +
   1.125 +            deferred.resolve();
   1.126 +          } else {
   1.127 +            deferred.resolve("Unable to load page to beautify: " + args.url + " " +
   1.128 +                             xhr.status + " " + xhr.statusText);
   1.129 +          }
   1.130 +        };
   1.131 +      }
   1.132 +      xhr.send(null);
   1.133 +      return deferred.promise;
   1.134 +    }
   1.135 +  }
   1.136 +];

mercurial