0
  Message passing:


  chrome.extension.onConnect.addListener(function(port) {
      console.assert(port.name == "knockknock");
      port.onMessage.addListener(function(msg) {
        if (msg.joke == "Knock knock")
          port.postMessage({question: "Who's there?"});
        else if (msg.answer == "Madame")
          port.postMessage({question: "Madame who?"});
        else if (msg.answer == "Madame... Bovary")
          port.postMessage({question: "I don't get it."});
      });
    });

これは私の background.js です:

function login(username,password){

    console.log(username);
var xhr = new XMLHttpRequest();

xhr.open("POST", "http://localhost:3000/login/", true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
data = {"username":username,"password":password};
console.log(JSON.stringify(data));
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
    // JSON.parse does not evaluate the attacker's scripts.
    var resp = JSON.parse(xhr.responseText);
    console.log(resp);
    var lStorage = localStorage;
    localStorage.setItem("username",resp["username"]);
    localStorage.setItem("apiKey",resp["apiKey"]);
    localStorage.setItem("password",resp["password"]);
    console.log(localStorage.getItem("username"));



  }
};


}

では、XHR.onReadyState から chrome.extension.onConnect.addListener(function(port) {} を呼び出して、background.js が受け取った値を popup.js に送信するにはどうすればよいでしょうか。

XHR.onReadyRequest 内で chrome.extension.onConnect.addListener(function(port) {} を定義できないことはわかっています。したがって、API 応答を受け取ったらメッセージを投稿するにはどうすればよいですか?

4

1 に答える 1

1

あなたのケースをカバーするサンプルを実装しました。AJAX 呼び出しが完了したらいつでも popup.js に通知できます。

マニフェスト.json

{
  "name": "Demo",
  "description": "Sample Message Communication",
  "version": "1",
  "permissions": [
    "tabs","<all_urls>"
   ],
  "background": {
    "scripts": ["background.js"]
  },
  "browser_action": {
    "default_icon": "screen.png",
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

popup.html

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

popup.js

var port = chrome.extension.connect({name: "Sample Communication"});
port.postMessage("Hi BackGround");
port.onMessage.addListener(function(msg) {
        console.log("message recieved"+ msg);
});

chrome.extension.onMessage.addListener(function (msg,sender,call){
    console.log("Message recieved"+msg);
});

background.js

chrome.extension.onConnect.addListener(function(port) {
  console.log("Connected .....");
  port.onMessage.addListener(function(msg) {
        console.log("message recieved "+ msg);
        console.log("mesage"+msg);
        searchquotes();
     });
});
function searchquotes(){
console.log("Inside");
var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function(data) {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
            chrome.extension.sendMessage("Hi popup.js");
            console.log("Response is recieved");
        }
      } else {
        //callback(null);
      }
    }

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

}
window.onload = searchquotes;
于 2012-11-26T11:16:34.907 に答える