使用来自chrome扩展的post方法发送文本 [英] Send text with post method from chrome extension

查看:392
本文介绍了使用来自chrome扩展的post方法发送文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Chrome扩展程序发送大文本。

我试图将存储在本地存储中的大文本发送到网站使用我的Chrome扩展。我见过这个回答
并使用此代码



<$ ()返回localStorage ['txt'];}
函数fakePost(){
var form = document.createElement(form); pre>
form.setAttribute(method,post);
form.setAttribute(action,http://cvsguimaraes.altervista.org/fiddles/postcheck.php);
var params = {action:ls()};
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();
};
fakePostCode = fakePost.toString()。replace(/(\\\
| \t)/ gm,'');

chrome.browserAction.onClicked.addListener(function(t){
chrome.tabs.create({url:javascript:+ fakePostCode +; fakePost();} );
});

但ls未定义。我怎样才能发送这个文本?我不使用XmlHTTP,因为我需要在新选项卡中打开它 基本上,答案就是你不能使用描述的方法。您的大数据将无法放入网址中,并且您通过网址注入的代码无法与扩展程序代码进行交互。



您需要一种替代解决方案,但幸好它应该很容易。你可以在你的扩展中包含一个html文件来发布数据,然后打开该页面。该HTML文件作为您的扩展程序的一部分,可以访问其 localStorage 。


$ b 编辑:I已经创建了一个通用解决方案,请参阅此答案



这些内容:

post.html:

 < html> 
< head>
< / head>
< body>
< script src =post.js>< / script>
< / body>
< / html>

post.js:

  var form = document.createElement(form); 
form.setAttribute(method,post);
form.setAttribute(action,http://cvsguimaraes.altervista.org/fiddles/postcheck.php);
var params = {action:localStorage ['txt'];};
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();

background.js:

  chrome.browserAction.onClicked.addListener(function(t){
chrome.tabs.create({url:chrome.runtime.getURL(post.html)});
});


Send big text with chrome extension.

I'm trying to send big text which is stored in my local storage to a site using my chrome extension. I've seen this answer and using this code

function ls(){return localStorage['txt'];}
function fakePost() {   
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "http://cvsguimaraes.altervista.org/fiddles/postcheck.php");
var params = {action: ls()};
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();
};
fakePostCode = fakePost.toString().replace(/(\n|\t)/gm,'');

chrome.browserAction.onClicked.addListener(function (t) {
chrome.tabs.create({"url" : "javascript:"+fakePostCode+"; fakePost();"});
});

but ls is not defined. How can I send this text? And I'm not using XmlHTTP because I need to open it in new tab

解决方案

Basically, the answer would be that you cannot use the described method. Your big data won't fit into an URL, and the code you inject via a URL has no way at all to interact with extension code.

You need an alternative solution, but thankfully it should be easy. You can include an html file into your extension that will post the data, and open that page instead. That HTML file, being part of your extension, has access to its localStorage.

Edit: I've created a general solution, see this answer.

Something along those lines:

post.html:

<html>
    <head>
    </head>
    <body>
        <script src="post.js"></script>
    </body>
</html>

post.js:

var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "http://cvsguimaraes.altervista.org/fiddles/postcheck.php");
var params = {action: localStorage['txt'];};
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();

background.js:

chrome.browserAction.onClicked.addListener(function (t) {
    chrome.tabs.create({"url" : chrome.runtime.getURL("post.html")});
});

这篇关于使用来自chrome扩展的post方法发送文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆