michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const gcli = require("gcli/index"); michael@0: const XMLHttpRequest = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]; michael@0: michael@0: loader.lazyImporter(this, "js_beautify", "resource:///modules/devtools/Jsbeautify.jsm"); michael@0: michael@0: exports.items = [ michael@0: { michael@0: name: "jsb", michael@0: description: gcli.lookup("jsbDesc"), michael@0: returnValue:"string", michael@0: params: [ michael@0: { michael@0: name: "url", michael@0: type: "string", michael@0: description: gcli.lookup("jsbUrlDesc") michael@0: }, michael@0: { michael@0: group: gcli.lookup("jsbOptionsDesc"), michael@0: params: [ michael@0: { michael@0: name: "indentSize", michael@0: type: "number", michael@0: description: gcli.lookup("jsbIndentSizeDesc"), michael@0: manual: gcli.lookup("jsbIndentSizeManual"), michael@0: defaultValue: 2 michael@0: }, michael@0: { michael@0: name: "indentChar", michael@0: type: { michael@0: name: "selection", michael@0: lookup: [ michael@0: { name: "space", value: " " }, michael@0: { name: "tab", value: "\t" } michael@0: ] michael@0: }, michael@0: description: gcli.lookup("jsbIndentCharDesc"), michael@0: manual: gcli.lookup("jsbIndentCharManual"), michael@0: defaultValue: " ", michael@0: }, michael@0: { michael@0: name: "doNotPreserveNewlines", michael@0: type: "boolean", michael@0: description: gcli.lookup("jsbDoNotPreserveNewlinesDesc") michael@0: }, michael@0: { michael@0: name: "preserveMaxNewlines", michael@0: type: "number", michael@0: description: gcli.lookup("jsbPreserveMaxNewlinesDesc"), michael@0: manual: gcli.lookup("jsbPreserveMaxNewlinesManual"), michael@0: defaultValue: -1 michael@0: }, michael@0: { michael@0: name: "jslintHappy", michael@0: type: "boolean", michael@0: description: gcli.lookup("jsbJslintHappyDesc"), michael@0: manual: gcli.lookup("jsbJslintHappyManual") michael@0: }, michael@0: { michael@0: name: "braceStyle", michael@0: type: { michael@0: name: "selection", michael@0: data: ["collapse", "expand", "end-expand", "expand-strict"] michael@0: }, michael@0: description: gcli.lookup("jsbBraceStyleDesc2"), michael@0: manual: gcli.lookup("jsbBraceStyleManual2"), michael@0: defaultValue: "collapse" michael@0: }, michael@0: { michael@0: name: "noSpaceBeforeConditional", michael@0: type: "boolean", michael@0: description: gcli.lookup("jsbNoSpaceBeforeConditionalDesc") michael@0: }, michael@0: { michael@0: name: "unescapeStrings", michael@0: type: "boolean", michael@0: description: gcli.lookup("jsbUnescapeStringsDesc"), michael@0: manual: gcli.lookup("jsbUnescapeStringsManual") michael@0: } michael@0: ] michael@0: } michael@0: ], michael@0: exec: function(args, context) { michael@0: let opts = { michael@0: indent_size: args.indentSize, michael@0: indent_char: args.indentChar, michael@0: preserve_newlines: !args.doNotPreserveNewlines, michael@0: max_preserve_newlines: args.preserveMaxNewlines == -1 ? michael@0: undefined : args.preserveMaxNewlines, michael@0: jslint_happy: args.jslintHappy, michael@0: brace_style: args.braceStyle, michael@0: space_before_conditional: !args.noSpaceBeforeConditional, michael@0: unescape_strings: args.unescapeStrings michael@0: }; michael@0: michael@0: let xhr = new XMLHttpRequest(); michael@0: michael@0: try { michael@0: xhr.open("GET", args.url, true); michael@0: } catch(e) { michael@0: return gcli.lookup("jsbInvalidURL"); michael@0: } michael@0: michael@0: let deferred = context.defer(); michael@0: michael@0: xhr.onreadystatechange = function(aEvt) { michael@0: if (xhr.readyState == 4) { michael@0: if (xhr.status == 200 || xhr.status == 0) { michael@0: let browserDoc = context.environment.chromeDocument; michael@0: let browserWindow = browserDoc.defaultView; michael@0: let gBrowser = browserWindow.gBrowser; michael@0: let result = js_beautify(xhr.responseText, opts); michael@0: michael@0: browserWindow.Scratchpad.ScratchpadManager.openScratchpad({text: result}); michael@0: michael@0: deferred.resolve(); michael@0: } else { michael@0: deferred.resolve("Unable to load page to beautify: " + args.url + " " + michael@0: xhr.status + " " + xhr.statusText); michael@0: } michael@0: }; michael@0: } michael@0: xhr.send(null); michael@0: return deferred.promise; michael@0: } michael@0: } michael@0: ];