如何传递一个保存的Javascript变量在Chrome扩展到谷歌应用引擎应用程序

How to pass a saved Javascript variable in a Chrome extension to a Google App Engine App?

本文关键字:Chrome 变量 扩展到 谷歌 应用程序 引擎 应用 Javascript 何传递 保存 一个      更新时间:2023-09-26

我有一个简单的书签应用程序,我正在开发学习谷歌应用程序引擎。此时,我将url复制并粘贴到http://ting-1.appspot.com/submit中,该http://ting-1.appspot.com/submit有一个带有url字段的表单。因为这是繁琐的我想添加一个chrome扩展。我只是改变了hello world的例子,所以现在我用下面的代码获得了标签页的url(如下所示):

  <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?

谢谢!

我使用的background.html改编自Mohamed Mansour的回答:

<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>

您使用正常的XHR (XMLHttpRequest)请求。我会把它放在你的背景页。遵循这里的示例,https://developer.mozilla.org/en-US/docs/Web/API/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);

请确保在清单中有获取该请求的权限的URL。

"permissions": [
  "tabs",
  " http://ting-1.appspot.com/submit"
],