open-request-in-tab.js (1764B)
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 // This file is a chrome-API-free version of the module 6 // devtools/client/netmonitor/src/utils/firefox/open-request-in-tab.js, so that 7 // it can be used in Chrome-API-free applications, such as the Launchpad. But 8 // because of this, it cannot take advantage of utilizing chrome APIs and should 9 // implement the similar functionalities on its own. 10 // 11 // Please keep in mind that if the feature in this file has changed, don't 12 // forget to also change that accordingly in 13 // devtools/client/netmonitor/src/utils/firefox/open-request-in-tab.js. 14 15 "use strict"; 16 17 loader.lazyRequireGetter( 18 this, 19 "openContentLink", 20 "resource://devtools/client/shared/link.js", 21 true 22 ); 23 24 /** 25 * Opens given request in a new tab. 26 * 27 * For POST request supports application/x-www-form-urlencoded content-type only. 28 */ 29 function openRequestInTab(url, requestHeaders, requestPostData) { 30 if (!requestPostData) { 31 openContentLink(url, { relatedToCurrent: true }); 32 } else { 33 openPostRequestInTabHelper({ 34 url, 35 data: requestPostData.postData, 36 }); 37 } 38 } 39 40 function openPostRequestInTabHelper({ url, data }) { 41 const form = document.createElement("form"); 42 form.target = "_blank"; 43 form.action = url; 44 form.method = "post"; 45 46 if (data) { 47 for (const key in data) { 48 const input = document.createElement("input"); 49 input.name = key; 50 input.value = data[key]; 51 form.appendChild(input); 52 } 53 } 54 55 form.hidden = true; 56 document.body.appendChild(form); 57 form.submit(); 58 form.remove(); 59 } 60 61 module.exports = { 62 openRequestInTab, 63 };