open-request-in-tab.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. /* eslint-disable mozilla/reject-some-requires */
  5. "use strict";
  6. let { Cc, Ci } = require("chrome");
  7. const Services = require("Services");
  8. const { gDevTools } = require("devtools/client/framework/devtools");
  9. /**
  10. * Opens given request in a new tab.
  11. */
  12. function openRequestInTab(request) {
  13. let win = Services.wm.getMostRecentWindow(gDevTools.chromeWindowType);
  14. let rawData = request.requestPostData ? request.requestPostData.postData : null;
  15. let postData;
  16. if (rawData && rawData.text) {
  17. let stringStream = getInputStreamFromString(rawData.text);
  18. postData = Cc["@mozilla.org/network/mime-input-stream;1"]
  19. .createInstance(Ci.nsIMIMEInputStream);
  20. postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
  21. postData.setData(stringStream);
  22. }
  23. win.gBrowser.selectedTab = win.gBrowser.addTab(request.url, null, null, postData);
  24. }
  25. function getInputStreamFromString(data) {
  26. let stringStream = Cc["@mozilla.org/io/string-input-stream;1"]
  27. .createInstance(Ci.nsIStringInputStream);
  28. stringStream.data = data;
  29. return stringStream;
  30. }
  31. module.exports = {
  32. openRequestInTab,
  33. };