jsb.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "use strict";
  5. const { Cc, Ci, Cu } = require("chrome");
  6. const l10n = require("gcli/l10n");
  7. const XMLHttpRequest = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"];
  8. loader.lazyImporter(this, "Preferences", "resource://gre/modules/Preferences.jsm");
  9. loader.lazyImporter(this, "ScratchpadManager", "resource://devtools/client/scratchpad/scratchpad-manager.jsm");
  10. loader.lazyRequireGetter(this, "beautify", "devtools/shared/jsbeautify/beautify");
  11. exports.items = [
  12. {
  13. item: "command",
  14. runAt: "client",
  15. name: "jsb",
  16. description: l10n.lookup("jsbDesc"),
  17. returnValue:"string",
  18. params: [
  19. {
  20. name: "url",
  21. type: "string",
  22. description: l10n.lookup("jsbUrlDesc")
  23. },
  24. {
  25. group: l10n.lookup("jsbOptionsDesc"),
  26. params: [
  27. {
  28. name: "indentSize",
  29. type: "number",
  30. description: l10n.lookup("jsbIndentSizeDesc"),
  31. manual: l10n.lookup("jsbIndentSizeManual"),
  32. defaultValue: Preferences.get("devtools.editor.tabsize", 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: l10n.lookup("jsbIndentCharDesc"),
  44. manual: l10n.lookup("jsbIndentCharManual"),
  45. defaultValue: " ",
  46. },
  47. {
  48. name: "doNotPreserveNewlines",
  49. type: "boolean",
  50. description: l10n.lookup("jsbDoNotPreserveNewlinesDesc")
  51. },
  52. {
  53. name: "preserveMaxNewlines",
  54. type: "number",
  55. description: l10n.lookup("jsbPreserveMaxNewlinesDesc"),
  56. manual: l10n.lookup("jsbPreserveMaxNewlinesManual"),
  57. defaultValue: -1
  58. },
  59. {
  60. name: "jslintHappy",
  61. type: "boolean",
  62. description: l10n.lookup("jsbJslintHappyDesc"),
  63. manual: l10n.lookup("jsbJslintHappyManual")
  64. },
  65. {
  66. name: "braceStyle",
  67. type: {
  68. name: "selection",
  69. data: ["collapse", "expand", "end-expand", "expand-strict"]
  70. },
  71. description: l10n.lookup("jsbBraceStyleDesc2"),
  72. manual: l10n.lookup("jsbBraceStyleManual2"),
  73. defaultValue: "collapse"
  74. },
  75. {
  76. name: "noSpaceBeforeConditional",
  77. type: "boolean",
  78. description: l10n.lookup("jsbNoSpaceBeforeConditionalDesc")
  79. },
  80. {
  81. name: "unescapeStrings",
  82. type: "boolean",
  83. description: l10n.lookup("jsbUnescapeStringsDesc"),
  84. manual: l10n.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. let xhr = new XMLHttpRequest();
  102. let deferred = context.defer();
  103. xhr.onreadystatechange = function() {
  104. if (xhr.readyState == 4) {
  105. if (xhr.status == 200 || xhr.status == 0) {
  106. let result = beautify.js(xhr.responseText, opts);
  107. ScratchpadManager.openScratchpad({text: result});
  108. deferred.resolve();
  109. } else {
  110. deferred.reject("Unable to load page to beautify: " + args.url + " " +
  111. xhr.status + " " + xhr.statusText);
  112. }
  113. };
  114. }
  115. try {
  116. xhr.open("GET", args.url, true);
  117. xhr.send(null);
  118. } catch(e) {
  119. return l10n.lookup("jsbInvalidURL");
  120. }
  121. return deferred.promise;
  122. }
  123. }
  124. ];