2012-11-29 61 views
0

假设我打开一个新的chrome选项卡供用户登录。该选项卡由chrome扩展中的createTab打开。一旦用户登录并且服务器发送响应,我如何让我的Chrome扩展获取响应数据?如何通过扩展名从chrome选项卡获取响应?

我能够创建和删除选项卡,但我无法计算如何让我的Chrome扩展程序知道该选项卡已得到响应,以便我的Chrome扩展读取该响应,将其存储在本地存储中,删除/删除chrome选项卡。

回答

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?我假设服务器发送响应登录通过\失败,由chrome.newTab新创建的页面。如果我的假设是正确的,那么针对您的功能需求的结构可以提供帮助

注入的内容脚本将查找收到的响应并通知Chrome扩展; 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 
}); 

让我知道如果你需要更多的信息。

+0

http://stackoverflow.com/questions/11431337/sending-message-to-chrome-extension-from-a-web-page正在寻找类似的东西,但不知道这是多么安全。 – Hick

+0

@Puck:我假设你想得到一些你没有控制权的Web请求完成的通知,如果这不是一个随机页面,你可以使用一个假动作来掩盖你的请求 – Sudarshan

相关问题