Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const { Cc, Ci, Cu } = require("chrome"); |
michael@0 | 8 | const gcli = require("gcli/index"); |
michael@0 | 9 | const XMLHttpRequest = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]; |
michael@0 | 10 | |
michael@0 | 11 | loader.lazyImporter(this, "js_beautify", "resource:///modules/devtools/Jsbeautify.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | exports.items = [ |
michael@0 | 14 | { |
michael@0 | 15 | name: "jsb", |
michael@0 | 16 | description: gcli.lookup("jsbDesc"), |
michael@0 | 17 | returnValue:"string", |
michael@0 | 18 | params: [ |
michael@0 | 19 | { |
michael@0 | 20 | name: "url", |
michael@0 | 21 | type: "string", |
michael@0 | 22 | description: gcli.lookup("jsbUrlDesc") |
michael@0 | 23 | }, |
michael@0 | 24 | { |
michael@0 | 25 | group: gcli.lookup("jsbOptionsDesc"), |
michael@0 | 26 | params: [ |
michael@0 | 27 | { |
michael@0 | 28 | name: "indentSize", |
michael@0 | 29 | type: "number", |
michael@0 | 30 | description: gcli.lookup("jsbIndentSizeDesc"), |
michael@0 | 31 | manual: gcli.lookup("jsbIndentSizeManual"), |
michael@0 | 32 | defaultValue: 2 |
michael@0 | 33 | }, |
michael@0 | 34 | { |
michael@0 | 35 | name: "indentChar", |
michael@0 | 36 | type: { |
michael@0 | 37 | name: "selection", |
michael@0 | 38 | lookup: [ |
michael@0 | 39 | { name: "space", value: " " }, |
michael@0 | 40 | { name: "tab", value: "\t" } |
michael@0 | 41 | ] |
michael@0 | 42 | }, |
michael@0 | 43 | description: gcli.lookup("jsbIndentCharDesc"), |
michael@0 | 44 | manual: gcli.lookup("jsbIndentCharManual"), |
michael@0 | 45 | defaultValue: " ", |
michael@0 | 46 | }, |
michael@0 | 47 | { |
michael@0 | 48 | name: "doNotPreserveNewlines", |
michael@0 | 49 | type: "boolean", |
michael@0 | 50 | description: gcli.lookup("jsbDoNotPreserveNewlinesDesc") |
michael@0 | 51 | }, |
michael@0 | 52 | { |
michael@0 | 53 | name: "preserveMaxNewlines", |
michael@0 | 54 | type: "number", |
michael@0 | 55 | description: gcli.lookup("jsbPreserveMaxNewlinesDesc"), |
michael@0 | 56 | manual: gcli.lookup("jsbPreserveMaxNewlinesManual"), |
michael@0 | 57 | defaultValue: -1 |
michael@0 | 58 | }, |
michael@0 | 59 | { |
michael@0 | 60 | name: "jslintHappy", |
michael@0 | 61 | type: "boolean", |
michael@0 | 62 | description: gcli.lookup("jsbJslintHappyDesc"), |
michael@0 | 63 | manual: gcli.lookup("jsbJslintHappyManual") |
michael@0 | 64 | }, |
michael@0 | 65 | { |
michael@0 | 66 | name: "braceStyle", |
michael@0 | 67 | type: { |
michael@0 | 68 | name: "selection", |
michael@0 | 69 | data: ["collapse", "expand", "end-expand", "expand-strict"] |
michael@0 | 70 | }, |
michael@0 | 71 | description: gcli.lookup("jsbBraceStyleDesc2"), |
michael@0 | 72 | manual: gcli.lookup("jsbBraceStyleManual2"), |
michael@0 | 73 | defaultValue: "collapse" |
michael@0 | 74 | }, |
michael@0 | 75 | { |
michael@0 | 76 | name: "noSpaceBeforeConditional", |
michael@0 | 77 | type: "boolean", |
michael@0 | 78 | description: gcli.lookup("jsbNoSpaceBeforeConditionalDesc") |
michael@0 | 79 | }, |
michael@0 | 80 | { |
michael@0 | 81 | name: "unescapeStrings", |
michael@0 | 82 | type: "boolean", |
michael@0 | 83 | description: gcli.lookup("jsbUnescapeStringsDesc"), |
michael@0 | 84 | manual: gcli.lookup("jsbUnescapeStringsManual") |
michael@0 | 85 | } |
michael@0 | 86 | ] |
michael@0 | 87 | } |
michael@0 | 88 | ], |
michael@0 | 89 | exec: function(args, context) { |
michael@0 | 90 | let opts = { |
michael@0 | 91 | indent_size: args.indentSize, |
michael@0 | 92 | indent_char: args.indentChar, |
michael@0 | 93 | preserve_newlines: !args.doNotPreserveNewlines, |
michael@0 | 94 | max_preserve_newlines: args.preserveMaxNewlines == -1 ? |
michael@0 | 95 | undefined : args.preserveMaxNewlines, |
michael@0 | 96 | jslint_happy: args.jslintHappy, |
michael@0 | 97 | brace_style: args.braceStyle, |
michael@0 | 98 | space_before_conditional: !args.noSpaceBeforeConditional, |
michael@0 | 99 | unescape_strings: args.unescapeStrings |
michael@0 | 100 | }; |
michael@0 | 101 | |
michael@0 | 102 | let xhr = new XMLHttpRequest(); |
michael@0 | 103 | |
michael@0 | 104 | try { |
michael@0 | 105 | xhr.open("GET", args.url, true); |
michael@0 | 106 | } catch(e) { |
michael@0 | 107 | return gcli.lookup("jsbInvalidURL"); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | let deferred = context.defer(); |
michael@0 | 111 | |
michael@0 | 112 | xhr.onreadystatechange = function(aEvt) { |
michael@0 | 113 | if (xhr.readyState == 4) { |
michael@0 | 114 | if (xhr.status == 200 || xhr.status == 0) { |
michael@0 | 115 | let browserDoc = context.environment.chromeDocument; |
michael@0 | 116 | let browserWindow = browserDoc.defaultView; |
michael@0 | 117 | let gBrowser = browserWindow.gBrowser; |
michael@0 | 118 | let result = js_beautify(xhr.responseText, opts); |
michael@0 | 119 | |
michael@0 | 120 | browserWindow.Scratchpad.ScratchpadManager.openScratchpad({text: result}); |
michael@0 | 121 | |
michael@0 | 122 | deferred.resolve(); |
michael@0 | 123 | } else { |
michael@0 | 124 | deferred.resolve("Unable to load page to beautify: " + args.url + " " + |
michael@0 | 125 | xhr.status + " " + xhr.statusText); |
michael@0 | 126 | } |
michael@0 | 127 | }; |
michael@0 | 128 | } |
michael@0 | 129 | xhr.send(null); |
michael@0 | 130 | return deferred.promise; |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | ]; |