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