打开一个选项卡并在 firefox webextension 中向它发出 POST 请求
Open a tab and make a POST request to it in a firefox webextension
我尝试将使用低级 SDK API 的 firefox addon 迁移到 WebExtension,在某些时候我想 POST 数据 url 编码为新标签。
低级别API可以通过以下代码实现:
const querystring = require('sdk/querystring');
let stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
stringStream.data = querystring.stringify(params); // params is a json data
let postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
postData.addContentLength = true;
postData.setData(stringStream);
var tabBrowser = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser").gBrowser;
var selectedTabIndex = tabBrowser.tabContainer.selectedIndex;
var newTab = tabBrowser.loadOneTab("https://myurl.com/", {
inBackground: false,
postData: postData
});
tabBrowser.moveTabTo(newTab, selectedTabIndex + 1);
但我还没有找到等效的 WebExtension。
是否可以或唯一的解决方案是创建一个表单并在 js 中提交?
您真的需要 post 到选项卡还是只显示响应?这将 post 并得到一个你可以做一些事情的回应。确保您的 CORS header 也设置正确。
<meta http-equiv="Content-Security-Policy" content="default-src 'self' www.demo.com; script-src 'self'; img-src http: https: data:; style-src 'self' 'unsafe-inline'">
function ajax(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(this.responseText);
};
xhr.onerror = reject;
xhr.open('POST', url);
xhr.send();
});
}
ajax("www.demo.com/test.php?q=" + params).then(function(result) {
//Do something with result
}).catch(function(err) {
console.log("Error: " + err.message);
});
目前唯一的解决办法是在新标签中创建一个表单并提交。
在后台脚本中:
// tab creation
browser.tabs.create({ index: tab.index + 1, url: "https://myurl.com/" }, function (tab) {
// load content script submitForm.js
browser.tabs.executeScript(tab.id, { file: "submitForm.js" }, function () {
// send message to submitForl.js with parameters
chrome.tabs.sendMessage(tab.id, {url: tab.url, message: 'hello world'});
});
});
提交表单内容:
function submitForm(request, sender, sendResponse)
{
var f = document.createElement('form');
f.setAttribute('method','post');
f.setAttribute('action','https://myurl.com/form');
var f1 = document.createElement('input');
f1.setAttribute('type','hidden');
f1.setAttribute('name','url]');
f1.setAttribute('value', request.url);
f.appendChild(f1);
var f2 = document.createElement('input');
f2.setAttribute('type','hidden');
f2.setAttribute('name','content');
f2.setAttribute('value', request.message);
f.appendChild(f2);
document.getElementsByTagName('body')[0].appendChild(f);
f.submit();
}
// listen for messages and execute submitForm
chrome.runtime.onMessage.addListener(submitForm);
我尝试将使用低级 SDK API 的 firefox addon 迁移到 WebExtension,在某些时候我想 POST 数据 url 编码为新标签。
低级别API可以通过以下代码实现:
const querystring = require('sdk/querystring');
let stringStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
stringStream.data = querystring.stringify(params); // params is a json data
let postData = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
postData.addContentLength = true;
postData.setData(stringStream);
var tabBrowser = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser").gBrowser;
var selectedTabIndex = tabBrowser.tabContainer.selectedIndex;
var newTab = tabBrowser.loadOneTab("https://myurl.com/", {
inBackground: false,
postData: postData
});
tabBrowser.moveTabTo(newTab, selectedTabIndex + 1);
但我还没有找到等效的 WebExtension。
是否可以或唯一的解决方案是创建一个表单并在 js 中提交?
您真的需要 post 到选项卡还是只显示响应?这将 post 并得到一个你可以做一些事情的回应。确保您的 CORS header 也设置正确。
<meta http-equiv="Content-Security-Policy" content="default-src 'self' www.demo.com; script-src 'self'; img-src http: https: data:; style-src 'self' 'unsafe-inline'">
function ajax(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(this.responseText);
};
xhr.onerror = reject;
xhr.open('POST', url);
xhr.send();
});
}
ajax("www.demo.com/test.php?q=" + params).then(function(result) {
//Do something with result
}).catch(function(err) {
console.log("Error: " + err.message);
});
目前唯一的解决办法是在新标签中创建一个表单并提交。 在后台脚本中:
// tab creation
browser.tabs.create({ index: tab.index + 1, url: "https://myurl.com/" }, function (tab) {
// load content script submitForm.js
browser.tabs.executeScript(tab.id, { file: "submitForm.js" }, function () {
// send message to submitForl.js with parameters
chrome.tabs.sendMessage(tab.id, {url: tab.url, message: 'hello world'});
});
});
提交表单内容:
function submitForm(request, sender, sendResponse)
{
var f = document.createElement('form');
f.setAttribute('method','post');
f.setAttribute('action','https://myurl.com/form');
var f1 = document.createElement('input');
f1.setAttribute('type','hidden');
f1.setAttribute('name','url]');
f1.setAttribute('value', request.url);
f.appendChild(f1);
var f2 = document.createElement('input');
f2.setAttribute('type','hidden');
f2.setAttribute('name','content');
f2.setAttribute('value', request.message);
f.appendChild(f2);
document.getElementsByTagName('body')[0].appendChild(f);
f.submit();
}
// listen for messages and execute submitForm
chrome.runtime.onMessage.addListener(submitForm);