【问题标题】:How to get a response out of chrome tab by its extension?如何通过其扩展从 chrome 选项卡中获得响应?
【发布时间】:2012-11-17 00:45:38
【问题描述】:

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

我能够创建和删除选项卡,但我不知道如何让我的 chrome 扩展程序知道选项卡有响应,以便我的 chrome 扩展程序读取该响应,将其存储在本地存储中并删除/删除镀铬标签。

【问题讨论】:

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


    【解决方案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 扩展程序; Chrome 扩展程序可以根据需要使用数据。

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

    如果您需要更多信息,请告诉我。

    【讨论】:

    • stackoverflow.com/questions/11431337/… 正在寻找类似的东西,但不确定它有多安全。
    • @Puck:我假设您希望收到一些您无法控制的 Web 请求完成的通知,如果这不是随机页面,您可以使用虚假操作来掩盖您的请求
    猜你喜欢
    • 2015-12-05
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多