1

I have simple Chrome extension to show temperature from Json. Background.js: how to pass response temp to ??? in popup.html????

Manifest ist OK. manifest:

{
    "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
    }
}

In popup I will show temp from URL. In 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>

In popup.js is call alarm to get temp. popup.js:

document.addEventListener('DOMContentLoaded', function() {
    var checkPageButton = document.getElementById('checkPage');
    checkPageButton.addEventListener('click', function() {


        chrome.runtime.sendMessage({greeting: "alert"}, function(response) {

            alert("response");
            document.getElementById('out').innerHTML = response.farewell; 
            document.getElementById('checkPage').innerHTML = response.farewell; 
        });


   }, false);
}, false);

In background.js is problem :-) How write data to div in popup.html? Why don't work sendResponse in callback function? 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"});
            });
        }
    }
);

Thank :-)

motorcb
  • 1,043
  • 3
  • 10
  • 19
  • possible duplicate of [Chrome Extension Message passing: response not sent](http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent) – wOxxOm Sep 11 '15 at 12:20
  • Maybe it's not the best duplicate candidate as there are [many other solutions](http://stackoverflow.com/search?q=%5Bgoogle-chrome-extension%5D+sendResponse) for this problem. – wOxxOm Sep 11 '15 at 12:23
  • What i do in all my extensions is: the popup can run chrome.extension.getBackgroundPage().foo(), and have the background page have a function foo(). – Marc Guiselin Sep 11 '15 at 14:53

0 Answers0