如何将Chrome扩展程序中保存的Javascript变量传递给Google App Engine应用程序? [英] How to pass a saved Javascript variable in a Chrome extension to a Google App Engine App?
问题描述
我有一个简单的书签应用,我正在开发它来学习Google App Engine。此时,我将该网址复制并粘贴到 http://ting-1.appspot.com/submit ,它有一个带有url字段的表单。由于这很麻烦,我想添加一个Chrome扩展。我刚刚更改了 hello world示例,以便现在获取网址此代码(如此处所述):
< script>
window.addEventListener(load,windowLoaded,false);
function windowLoaded(){
chrome.tabs.getSelected(null,function(tab){
document.getElementById('currentLink')。innerHTML = tab.url;
tabUrl = tab.url;
});
}
document.write(tabUrl)
< / script>
如何将 tabUrl
传递给 http://ting-1.appspot.com/submit ?
感谢!
更新
$ b
background.html
,我用它作为 Mohamed Mansour's answer :
<脚本>
//单击浏览器操作图标时的响应。
chrome.browserAction.onClicked.addListener(function(tab){
//获取url和标题
chrome.tabs.getSelected(null,function(tab){
tabUrl = tab.url
tabTitle = tab.title
//将数据发布到书签应用
var formData = new FormData();
formData .append(url,tabUrl);
formData.append(title,tabTitle);
formData.append(pitch,this is a note);
formData .append(user_tag_list,tag1,tag2);
var xhr = new XMLHttpRequest();
xhr.open(POST,http://ting-1.appspot.com / submithandlertest);
xhr.send(formData);
});
});
< / script>
您使用正常的XHR(XMLHttpRequest)请求。我会将其放入您的后台页面。按照此处的示例, https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest。我相信你也想提交数据,所以不要忘记。我想你也想要一个POST请求。从MDC上的例子中,你可以用这种方式来关联它:
var formData = new FormData();
formData.append(username,Groucho);
formData.append(accountnum,123456);
var xhr = new XMLHttpRequest();
xhr.open(POST,http://ting-1.appspot.com/submit);
xhr.send(formData);
请确保您的清单以获得执行该请求的权限。
permissions:[
tabs,
http:// ting-1。 appspot.com/submit
,
I have a simple bookmarking app that I am developing to learn Google App Engine. At this point I copy and paste the url into http://ting-1.appspot.com/submit which has a form that has a url field. Since this is cumbersome I thought of adding a chrome extension. I just changed the hello world example so that now I get the url of the tab with this code (as explained here):
<script>
window.addEventListener("load", windowLoaded, false);
function windowLoaded() {
chrome.tabs.getSelected(null, function(tab) {
document.getElementById('currentLink').innerHTML = tab.url;
tabUrl = tab.url;
});
}
document.write(tabUrl)
</script>
How do I pass tabUrl
to http://ting-1.appspot.com/submit?
Thanks!
Update
background.html
that I used as adapted from Mohamed Mansour's answer:
<script>
//React when a browser action's icon is clicked.
chrome.browserAction.onClicked.addListener(function(tab) {
//this gets the url and the title
chrome.tabs.getSelected(null, function(tab) {
tabUrl = tab.url
tabTitle = tab.title
//this posts the data to the bookmarking app
var formData = new FormData();
formData.append("url", tabUrl);
formData.append("title", tabTitle);
formData.append("pitch", "this is a note");
formData.append("user_tag_list", "tag1, tag2");
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest");
xhr.send(formData);
});
});
</script>
You use normal XHR (XMLHttpRequest) request. I would place that in your background page. Follow the example here, https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest. I believe you want to submit data as well, so don't forget that. I think you wanted a POST request too. From the example on MDC, you can relate it this way:
var formData = new FormData();
formData.append("username", "Groucho");
formData.append("accountnum", 123456);
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submit");
xhr.send(formData);
Make sure you have the URL in your manifest for getting permissions to do that request.
"permissions": [
"tabs",
" http://ting-1.appspot.com/submit"
],
这篇关于如何将Chrome扩展程序中保存的Javascript变量传递给Google App Engine应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!