Google chrome extension Chrome扩展:在background.js中回调到popup.js

Google chrome extension Chrome扩展:在background.js中回调到popup.js,google-chrome-extension,Google Chrome Extension,我有一个简单的Chrome扩展来显示Json中的温度。 Background.js:如何将响应温度传递给???在popup.html中 没问题。 舱单: { "name": "AlarmText", "version": "0.0.1", "manifest_version": 2, "permissions": ["alarms", "http://api.openweathermap.org/data/2.5/weather?q=London"],

我有一个简单的Chrome扩展来显示Json中的温度。 Background.js:如何将响应温度传递给???在popup.html中

没问题。 舱单:

{
    "name": "AlarmText",
    "version": "0.0.1",
    "manifest_version": 2,
    "permissions": ["alarms", "http://api.openweathermap.org/data/2.5/weather?q=London"],

    "icons": { "128": "icons/icon128.png",
               "64": "icons/icon64.png",
               "32": "icons/icon32.png" },  

    "browser_action": {
        "default_title": "Alarm test",
        "default_popup": "popup.html"
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": true
    }
}
在弹出窗口中,我将显示来自URL的临时文件。在div id=“out”中,popup.html:

<!doctype html>
<html>
  <head>
    <title>popup</title>
    <script src="popup.js"></script>
    <script src="background.js"></script>
  </head>
  <body>
    <div id="out">???</div>
    <button id="checkPage">Refresh</button>
  </body>
</html>
在background.js中存在问题:-)如何将数据写入popup.html中的div?为什么不在回调函数中使用sendResponse? background.js

function getTemp(callback) {
  var xhr = new XMLHttpRequest();
  xhr.open ("GET", "http://api.openweathermap.org/data/2.5/weather?q=London", true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      // defensive check
      if (typeof callback == "function") {
        // apply() sets the meaning of "this" in the callback
        callback.apply(xhr);
      }
    }
  }
  // send the request *after* the event handler is defined 
  xhr.send();
}

//alarm
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.greeting == "alert"){
            alert("alert alarm");

            getTemp(function() { 
                responseArray = JSON.parse(this.responseText);

                //@TODO how to pass response temp to <div id="out">???</div> in popup.html????
                alert("response get temp: " + responseArray["main"]["temp"]);
                sendResponse({farewell: "goodbye"});
            });
        }
    }
);
函数getTemp(回调){ var xhr=new XMLHttpRequest(); xhr.open(“GET”http://api.openweathermap.org/data/2.5/weather?q=London“,对); xhr.onreadystatechange=函数(){ 如果(xhr.readyState==4&&xhr.status==200){ //防御性检查 if(回调类型==“函数”){ //apply()设置回调中“this”的含义 callback.apply(xhr); } } } //在定义事件处理程序*之后发送请求* xhr.send(); } //警报 chrome.runtime.onMessage.addListener( 功能(请求、发送方、发送响应){ 如果(request.greeting==“警报”){ 警报(“警报警报”); getTemp(函数(){ responseArray=JSON.parse(this.responseText); //@TODO如何在popup.html中将响应温度传递给???? 警报(“响应获取温度:+responseArray[“main”][“temp”]); sendResponse({再见:“再见”}); }); } } );
感谢:-)

可能的重复可能它不是这个问题的最佳候选重复。我在所有扩展中所做的是:弹出窗口可以运行chrome.extension.getBackgroundPage().foo(),并且后台页面有一个函数foo()。可能的重复可能它不是解决此问题的最佳重复候选。我在所有扩展中所做的是:弹出窗口可以运行chrome.extension.getBackgroundPage().foo(),并使背景页具有函数foo()。
function getTemp(callback) {
  var xhr = new XMLHttpRequest();
  xhr.open ("GET", "http://api.openweathermap.org/data/2.5/weather?q=London", true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
      // defensive check
      if (typeof callback == "function") {
        // apply() sets the meaning of "this" in the callback
        callback.apply(xhr);
      }
    }
  }
  // send the request *after* the event handler is defined 
  xhr.send();
}

//alarm
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.greeting == "alert"){
            alert("alert alarm");

            getTemp(function() { 
                responseArray = JSON.parse(this.responseText);

                //@TODO how to pass response temp to <div id="out">???</div> in popup.html????
                alert("response get temp: " + responseArray["main"]["temp"]);
                sendResponse({farewell: "goodbye"});
            });
        }
    }
);