Pon*_*ono 13 javascript google-chrome google-chrome-extension
是否有一个简单的解决方案将动态聚合数据POST到新选项卡中?
chrome.tabs.create
没有'POST'选项.通常我会用
chrome.browserAction.onClicked.addListener(function (t) {
chrome.tabs.create(
{
"url" : "http://super.url",
"method" : "POST" // oops.. no option.
});
});
Run Code Online (Sandbox Code Playgroud)
kbt*_*tzr 15
您可以简单地结合这两种技术:
javascript:
在地址栏或标签href
值中添加前缀来执行JavaScript命令<a>
..
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();"});
});
Run Code Online (Sandbox Code Playgroud)
当然,这只是一个肮脏的黑客.如果你需要更好的东西,可以使用XHR对象或者更详细地说明@Xan的答案.
Xan*_*Xan 14
cvsguimaraes的答案中的代码适用于可以放入URL的短数据字符串.正如这个问题所证明的那样,并非总是如此.
Kenny Evitt的回答暗示了解决方案.我为这个问题做了一个实现,并花时间来概括它.我在这里介绍它.
我们的想法是打开与扩展程序捆绑在一起的页面(post.html),通过消息传递为其提供所需信息,并从该页面执行POST.
post.html
<html>
<head>
<title>Redirecting...</title>
</head>
<body>
<h1>Redirecting...</h1>
<!-- Decorate as you wish, this is a page that redirects to a final one -->
<script src="post.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
post.js
var onMessageHandler = function(message){
// Ensure it is run only once, as we will try to message twice
chrome.runtime.onMessage.removeListener(onMessageHandler);
// code from https://stackoverflow.com/a/7404033/934239
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", message.url);
for(var key in message.data) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", message.data[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form.submit();
}
chrome.runtime.onMessage.addListener(onMessageHandler);
Run Code Online (Sandbox Code Playgroud)
background.js(或扩展内的其他非内容脚本)
function postData(url, data) {
chrome.tabs.create(
{ url: chrome.runtime.getURL("post.html") },
function(tab) {
var handler = function(tabId, changeInfo) {
if(tabId === tab.id && changeInfo.status === "complete"){
chrome.tabs.onUpdated.removeListener(handler);
chrome.tabs.sendMessage(tabId, {url: url, data: data});
}
}
// in case we're faster than page load (usually):
chrome.tabs.onUpdated.addListener(handler);
// just in case we're too late with the listener:
chrome.tabs.sendMessage(tab.id, {url: url, data: data});
}
);
}
// Usage:
postData("http://httpbin.org/post", {"hello": "world", "lorem": "ipsum"});
Run Code Online (Sandbox Code Playgroud)
请注意双重消息:使用chrome.tabs.create
回调我们无法确定侦听器是否已准备就绪,我们也无法确定它是否已完成加载(尽管在我的测试中,它始终仍在加载).但比抱歉更安全.
归档时间: |
|
查看次数: |
10369 次 |
最近记录: |