如何通过扩展程序从chrome标签中获取响应?

时间:2012-11-29 01:30:39

标签: javascript google-chrome-extension local-storage

假设我打开了一个新的Chrome标签供用户登录。该标签由chromeTab从chrome扩展程序打开。一旦用户登录,服务器发送响应,我将如何使我的chrome扩展获取响应数据?

我可以同时创建和删除标签但是我无法确定如何让我的Chrome扩展程序知道该标签有响应,以便我的Chrome扩展程序读取该响应,将其存储在本地存储中并删除/删除chrome选项卡。

1 个答案:

答案 0 :(得分:1)

Suppose I open a new chrome tab for a user to sign in. The tab is opened by createTab from the chrome extension. And once the user signs in, and server sends a response, how would I make my chrome extension get the response data?我假设服务器发送响应登录pass \ fail到chrome.newTab新创建的页面。如果我的假设是正确的,那么这个功能要求的结构可以帮助你。

注入的内容脚本将查找收到的响应并通知Chrome Extension; Chrome扩展程序可以根据需要使用数据。

enter image description here

<强> 的manifest.json

{
  "name": "Content to Extension",
  "description": "A sample AJAX call and etc",
  "version": "0.1",
  "permissions": [
    "experimental", "tabs","<all_urls>"
  ],
  "browser_action": {
    "default_icon": "icon.jpg",
    "default_popup": "popup.html"
  },
  "manifest_version": 2,
  "content_scripts":[
  {
    "matches": ["<all_urls>"],
    "js":["content.js"]
  }
  ]
}

<强> popup.html

<html>
<head>
<script src='popup.js'></script>
</head>
<body>
</body>
</html>

<强> content.js

function filtersearch(){

var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function(data) {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
            console.log("message Sent");
            chrome.extension.sendMessage("Response recived");
        }
      } else {
        //callback(null);
      }
    }

var url = 'http://www.w3schools.com/html/default.asp';
xhr.open('GET', url, true);
xhr.send();

}
window.onload = filtersearch;

<强> popup.js

chrome.extension.onMessage.addListener(function (message,sender,callback){
    console.log("Message Recieved");

    //Store in Local Storage
    localStorage.messageRecieved = message;

    // Close or remove tabs or Do every thing here
});

如果您需要更多信息,请与我们联系。