Chrome扩展程序开发 - 发布到新标签 [英] Chrome Extension Development - POST to new tab
本文介绍了Chrome扩展程序开发 - 发布到新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的解决方案将POST动态聚合数据转换为新标签?
chrome.tabs.create
没有'POST'选项。通常我会用
pre $ ch $。 (
{
url:http://super.url,
method:POST// oops .. no option。
});
});
解决方案
您可以简单地组合这两种技术:
- 您可以通过在地址栏中添加
javascript:
前缀来执行JavaScript命令,或者在<< a>
标记的c $ c> href 值。
- 只有使用JavaScript,您才可以创建一个表单元素并填充数据然后发布。
。
function fakePost(){
var form = document.createElement(form);
form.setAttribute(method,post);
form.setAttribute(action,http://cvsguimaraes.altervista.org/fiddles/postcheck.php);
var params = {userId:2,action:delete};
for(var key in params){
var hiddenField = document.createElement(input);
hiddenField.setAttribute(type,hidden);
hiddenField.setAttribute(name,key);
hiddenField.setAttribute(value,params [key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form); //不知道这一步是否必要
form.submit();
};
// minify function
fakePostCode = fakePost.toString()。replace(/(\\\
| \t)/ gm,'');
chrome.browserAction.onClicked.addListener(function(t){
chrome.tabs.create({url:javascript:+ fakePostCode +; fakePost();} );
});
当然,这只是一个肮脏的破解。如果你需要更好的东西,你可以使用 XHR对象或精心制作更像是@ Xan的答案。
Is there an easy solution to POST dynamically aggregated data into a new tab?
chrome.tabs.create
does not have a 'POST' option. Normally I would use
chrome.browserAction.onClicked.addListener(function (t) {
chrome.tabs.create(
{
"url" : "http://super.url",
"method" : "POST" // oops.. no option.
});
});
解决方案
You can simply combine these two techniques:
- You may execute JavaScript commands by adding
javascript:
prefix at your address bar or inhref
values of<a>
tags. - Only with JavaScript, you can create a form element and fill it with your data then POST it.
.
function fakePost() {
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "http://cvsguimaraes.altervista.org/fiddles/postcheck.php");
var params = {userId: 2, action: "delete"};
for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form); // Not sure if this step is necessary
form.submit();
};
//minify function
fakePostCode = fakePost.toString().replace(/(\n|\t)/gm,'');
chrome.browserAction.onClicked.addListener(function (t) {
chrome.tabs.create({"url" : "javascript:"+fakePostCode+"; fakePost();"});
});
Of course, that's just a dirty hack. If you need something better you can use a XHR Object or elaborate something more like @Xan's answer below.
这篇关于Chrome扩展程序开发 - 发布到新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文