0

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?

I am able to both create and delete the tab but I am not able to figure how to make my chrome extension know that the tab has got a response so that my chrome extension reads that response, stores it in local storage and removes/deletes the chrome tab.

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
Hick
  • 35,524
  • 46
  • 151
  • 243

1 Answers1

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? I am assuming server sends response login pass\fail to page which is newly created by chrome.newTab. If my assumption is correct this structure for your functional requirement can help.

The content script injected will look for response received and notifies Chrome Extension; Chrome Extension can use data as needed.

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
});

Let me know if you need more information.

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
  • http://stackoverflow.com/questions/11431337/sending-message-to-chrome-extension-from-a-web-page Was looking for something like that but not sure how secure this is. – Hick Nov 29 '12 at 15:10
  • @Puck: I assume you want to be notified of some web request completion for whom you do not have control to fire, if that is not a random page you can use a fake action to mask your request – Sudarshan Nov 29 '12 at 16:29